I have a loop in Stata referring to a local macro which I use to run several actions (create a file, run script associated with each file etc.).
Everything is fine but I would like to change the order in which the files are read in the loop, which by default is alphabetical.
My loop looks as follows:
foreach file of local myfiles {
noisily display as text "some text"
}
Besides the display
command, the script continues with my code.
Is there a way to customize the order in which the loop addresses the files stored in the local macro?
I would like to achieve this without changing the file names.
The following works for me:
local myfiles myfile1.dta myfile2.dta myfile3.dta myfile4.dta
tokenize `myfiles'
foreach x in `3' `1' `4' `2' {
display "`x'"
}
myfile3.dta
myfile1.dta
myfile4.dta
myfile2.dta
Another way is the following:
local myfiles myfile1.dta myfile2.dta myfile3.dta myfile4.dta
local mf myfile2.dta myfile3.dta
local myfiles `mf' `: list myfiles - mf'
display "`myfiles'"
myfile2.dta myfile3.dta myfile1.dta myfile4.dta