Hi!
I have 19 files, and want Stata to create a tempfile for each one, but named differently.
How may I create a tempfile named according to the actual value of a local in a loop?
What I do is a filesearch for some datasets, change them somehow (mycode), and then (via some regexr) create a local containing the tempfile name, respectively.
Here's some (pretty nonsense) example for the sake of illustration. This is - I guess - a more a general question, and not so much depending on specific data, anyway:
With
, I can see, all but one line are executed well - why not the
? The error message states
, below tempfile `"`tmp'"' the (correct) traced result is
, though!
I have 19 files, and want Stata to create a tempfile for each one, but named differently.
How may I create a tempfile named according to the actual value of a local in a loop?
What I do is a filesearch for some datasets, change them somehow (mycode), and then (via some regexr) create a local containing the tempfile name, respectively.
Here's some (pretty nonsense) example for the sake of illustration. This is - I guess - a more a general question, and not so much depending on specific data, anyway:
Code:
input filename
"StudyX_spWork_R_12-0-1.dta"
end
save "StudyX_spWork_R_12-0-1.dta"
clear
input filename
"StudyX_spTravel_T_12-0-1.dta"
end
save "StudyX_spTravel_T_12-0-1.dta"
clear
input filename
"StudyX_spSports_S_12-0-1.dta"
end
save "StudyX_spSports_S_12-0-1.dta"
clear
input filename
"StudyX_Z_12-0-1.dta"
end
save "StudyX_Z_12-0-1.dta"
qui filesearch *_sp*, dir($pathin)
foreach file in `r(filenames)' {preserve
di "create tempfile of `file'"
qui use $pathin`file', clear
qui include $pathdo/mycode
local tmp = regexr(regexr("`file'", "^StudyX_", ""), "_12-0-1.dta$", "")
di "designated name for tempfile: `tmp'"
tempfile `"`tmp'"'
save $pathout/`tmp', replace
restore
}
Code:
set trace on
Code:
save $pathout/`tmp', replace
"_spWork invalid name
tempfile `"spWork"'

.)
Comment