I have datasets like this:
C:\temp\SalesFigures FY13.dta
C:\temp\SalesFigures FY14.dta
C:\temp\SalesFigures FY15.dta
etc.
Each file contains sales data from 50 states. I often need to run a block of code for just some of the states in these files. I specify those states in a file called StatesToRun.dta (e.g., AK, CA, WA) and use a foreach command to loop through each state. I also use a macro to specify the FY .dta file I want to use.
For example:
* Specify file to run.
local FY "FY14"
* Run code only for the states I list in StatesToRun.dta.
use "C:/temp/StatesToRun.dta", clear
levelsof state, local(statelist)
foreach MyState of local statelist
{
use "C:/temp/SalesFigures 'FY'.dta", clear
keep if state == `"`MyState'"'
* etc. ...
}
THE NEED
I sometimes need to run my code for several of the FY files in C:\temp. So I'd like to create a loop for that, too. For example, if I wanted to run the code for AK, CA, and WA, for the FY14 and FY15 .dta files, I'd enter "AK", "CA", and "WA" for state in StatesToRun.dta, and "FY14" and "FY15" for a variable I could call "FY" in StatesToRun.dta. I'm just not sure how to incorporate this second variable into the loop. I read you can nest foreach statements, but I'm not sure if that's the best approach.
Being rather new to Stata, this is my best guess:
* Run code only for the states and FYs I list in StatesToRun.dta.
use "C:/temp/StatesToRun.dta", clear
levelsof state, local(statelist)
levelsof FY, local(FYlist)
foreach MyState of local statelist {
foreach MyFY of local FYlist {
use "C:/temp/SalesFigures 'MyFY'.dta", clear
keep if state == `"`MyState'"'
* etc. ...
}
}
Am I on the right path?
foreach.AK CA WAin a dataset just to take them out again. Why not type them directly?