[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

From |
"Nuno" <liststata@gmail.com> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
st: Running a program in a variable number of variables |

Date |
Tue, 17 Jul 2007 06:56:25 +0100 |

Hello everyone, I was wondering if someone could help me with a programming issue. I need to import several csv files that might have a variable number of variables. Given the structure of the files to be imported, the variables will be labels v1, v2,..., vn and string. The code I'm using will then keep variables v1, v2 and vi at each cycle. I've written the following program (for simplicity, the code I'm sending is just for a file to be imported, not for the complete set of files): insheet using "C:\Documents and Settings\Nuno\Desktop\test.csv", clear cd c:\ foreach i of numlist 3/4 { preserve keep v1 v2 v`i' drop if v1=="" drop if length(v1)>7 gen year=v`i' if v1=="TYPE" destring year, replace force gen name=v2[_n-1] if year~=. gen dscd=v1[_ n-1] if v1=="TYPE" replace name=name[_ n-1] if name=="" replace dscd=dscd[_ n-1] if dscd=="" replace year=year[_ n-1] if year==. drop if length(v1)<=6 rename v1 vars rename v`i' data drop v2 save file`i'.dta restore } As you can see, in the foreach cycle I had to introduce the number start/end of cycles manually. Is there a way to get this information directly for the number of variables that are first imported? I would then do the cycle "foreach i of numlist 3/(total_number_of_variables-2). I could use -varlist- instead of -numlist- but I'm not sure how to define that starts in v3 and ends at vn. Best, Nuno * * For searches and help try: * http://www.stata.com/support/faqs/res/findit.html * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

- Prev by Date:
**st: St: postfile append?** - Next by Date:
**st: RE: Running a program in a variable number of variables** - Previous by thread:
**st: Insertion of variable name at cursor** - Next by thread:
**st: RE: Running a program in a variable number of variables** - Index(es):

© Copyright 1996–2017 StataCorp LLC | Terms of use | Privacy | Contact us | What's new | Site index |