Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 views
in Technique[技术] by (71.8m points)

loops - Can I name files recursively in Stata?

I am creating 1 test file for each year from 2001 to 2010. I define some variables for my 2001 file, and then, from 2002 onwards, I want Stata to merge it with the file from the previous year. My minimum working example looks like this:

set obs 100
g year=2001
g casenum=_n
g yob=1973
save file_2001, replace

forv n=2002(1)2010{
clear
set obs 110
g casenum=_n
g year=`n'
merge 1:1 casenum using file_`n-1', keepusing (yob)
save file_`n', replace
}

The error I get is file_2002.dta not found. Does anyone have any suggestions?

question from:https://stackoverflow.com/questions/65927622/can-i-name-files-recursively-in-stata

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In this line:

merge 1:1 casenum using file_`n-1', keepusing (yob)

There is no local macro named n-1. This should be `=`n'-1'


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...