Reputation: 207
I have a list of files with similar names, for example:
I am going to run a simple script:
foreach year in 2002 2003 2004 2005 2006 2007 2008 2009 2011 2012 2013 2014 2015 {
use "$input\`year'_file.dta ", clear
keep v1 v2 v3
tab v1, gen (v1_dummy)
gen year = 'x'
save "$output\`year'_newfile.dta ", replace
}
However, I would like for one of the files to be created the respective year variable.
How can I add a variable year = 'year'
?
Upvotes: 0
Views: 545
Reputation:
You do not define the local macro x
anywhere.
The following should work:
foreach year in 2002 2003 2004 2005 2006 2007 2008 2009 2011 2012 2013 2014 2015 { use "$input\`year'_file.dta ", clear keep v1 v2 v3 tab v1, gen (v1_dummy) gen year = `year' save "$output\`year'_newfile.dta ", replace }
For a specific year only:
foreach year in 2002 2003 2004 2005 2006 2007 2008 2009 2011 2012 2013 2014 2015 { use "$input\`year'_file.dta ", clear keep v1 v2 v3 tab v1, gen (v1_dummy) if `year' == 2005 gen year = `year' save "$output\`year'_newfile.dta ", replace }
Upvotes: 2