Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: error when generating variable in nested foreach


From   Nick Cox <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   st: RE: error when generating variable in nested foreach
Date   Thu, 10 Nov 2011 11:35:21 +0000

. help capture 

Nick 
n.j.cox@durham.ac.uk 

Minsoo Kim

I am using nested foreach to read multiple csv. files and append
these. On top of this I would like to generate additional variable
that contains the information from its file name:

In order to explain the problem that I have been struggling with, I
would first provide the simplified version of my code:

insheet using dummy.csv, clear
save master.dta, replace

foreach yr of numlist 2009 2010 {
foreach mth of newlist Jan Feb Mar Apr {
foreach age of numlist 40 50 {		
capture noisily insheet using `mth'`yr'_`age'.csv, clear					
capture noisily save add.dta, replace
capture replace year = `yr'
capture replace mth = `mth'
append using master.dta
save master.dta, replace
}
}
}

If I don't include the two lines of 'replace' command then it works fine,

however, if I additioinally try to 'replace' the values based on the
information from file name,

then it gives me the following error:

file Jan2009_40.csv not found
no variables defined
file master.dta not found

I guess the problem comes from the fact that csv.files exists only for
some, not all, of the possible combinations of yr(2009,2010), mth(Jan,
Feb, Mar, Apr), and age(40,50).

And the actual code has much more bigger set of possible combination.

What I would like to get at the end is to append all the files and
create additional columns of information of yr, mth, age,
correspondingly.

I have been trying to look up the previously asked questions, but I
couldn't get the clue yet.

At this moment, any comment or suggestion will be greatly appreciated
for me to notice the part that I am missing.

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index