Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

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

st: error when generating variable in nested foreach

From   Minsoo Kim <>
Subject   st: error when generating variable in nested foreach
Date   Thu, 10 Nov 2011 03:00:20 -0600

Dear Statalist,

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,

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:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index