Reputation: 83
I'm stuck trying to do a loop to read many files that have no correlated names, but I want to save
them with correlated names.
This is an example of what I have:
paths:
"example_84745.dta"
"example_74632.dta"
"example_18390.dta"
So, I want to read all of them and save them like
"example_1.dta"
"example_2.dta"
"example_3.dta"
What I'm trying to do is to work with local macros, like this:
local path_1 = "example_84745.dta"
local path_2 = "example_74632.dta"
local path_3 = "example_18390.dta"
forvalues i = 1(1)3{
display "path_`i'"
use "path_`i'", clear
save "example_`i'"
}
But it is not working. It prints path_1 is not found
.
I really appreciate your comments.
Upvotes: 0
Views: 149
Reputation: 37208
You're looping over 1 2 3
and asking to display path_1 path_2 path_3
and then use them, but you need to spell out that they are in turn local macro names.
local path_1 = "example_84745.dta"
local path_2 = "example_74632.dta"
local path_3 = "example_18390.dta"
forvalues i = 1(1)3{
display "`path_`i''"
use "`path_`i''", clear
save "example_`i'"
}
Local macros in Stata can be used like variables in many programming languages, but it is not customary to regard them as variables. In Stata, a variable is (is only) a column in a dataset.
Upvotes: 1