Hello friends,
I am trying to use a loop in which a changing macro number variable refers to different variables. My variables are years, yr1971, yr 1972, yr 1973, and so forth. But Stata does not recognize the variables in the method I used (code below).
Thank you.
This is the relevant code segmant (followed by the stata output):
count
local obs = `r(N)'
local qobs = `r(N)'/4
foreach d of numlist 1(1)`qobs' {
local x = (`obs'+4-(`d'*4))
local begyr = 1971
local endyr = 1978
foreach f of varlist yr1971-yr2019 {
local yrnum = 1
foreach g of numlist `begyr'(`begyr')`endyr' {
replace tempNR = yr`g'[`j'] in `yrnum'
local yrnum = `yrnum'+1
}
local begyr = `begyr'+1
local endyr = `endyr'+1
}
}
. count
464
. local obs = `r(N)'
. local qobs = `r(N)'/4
. foreach d of numlist 1(1)`qobs' {
2. local x = (`obs'+4-(`d'*4))
3. local begyr = 1971
4. local endyr = 1978
5. foreach f of varlist yr1971-yr2019 {
6. local yrnum = 1
7. foreach g of numlist `begyr'(`begyr')`endyr' {
8. replace tempNR = yr`g'[`j'] in `yrnum'
9. local yrnum = `yrnum'+1
10. }
11. local begyr = `begyr'+1
12. local endyr = `endyr'+1
13. }
14. }
yr1971 not found
r(111);
end of do-file
r(111);
.
I am trying to use a loop in which a changing macro number variable refers to different variables. My variables are years, yr1971, yr 1972, yr 1973, and so forth. But Stata does not recognize the variables in the method I used (code below).
Thank you.
This is the relevant code segmant (followed by the stata output):
count
local obs = `r(N)'
local qobs = `r(N)'/4
foreach d of numlist 1(1)`qobs' {
local x = (`obs'+4-(`d'*4))
local begyr = 1971
local endyr = 1978
foreach f of varlist yr1971-yr2019 {
local yrnum = 1
foreach g of numlist `begyr'(`begyr')`endyr' {
replace tempNR = yr`g'[`j'] in `yrnum'
local yrnum = `yrnum'+1
}
local begyr = `begyr'+1
local endyr = `endyr'+1
}
}
. count
464
. local obs = `r(N)'
. local qobs = `r(N)'/4
. foreach d of numlist 1(1)`qobs' {
2. local x = (`obs'+4-(`d'*4))
3. local begyr = 1971
4. local endyr = 1978
5. foreach f of varlist yr1971-yr2019 {
6. local yrnum = 1
7. foreach g of numlist `begyr'(`begyr')`endyr' {
8. replace tempNR = yr`g'[`j'] in `yrnum'
9. local yrnum = `yrnum'+1
10. }
11. local begyr = `begyr'+1
12. local endyr = `endyr'+1
13. }
14. }
yr1971 not found
r(111);
end of do-file
r(111);
.
Comment