Loop over different data files in a folder
Today
Hi Guys!
I am trying to create a merged file for some selected variables in a panel survey. Some people might be familiar with this data set. The data are recorded with different prefix as a b c d .... representing different wave (years). For instance, the variables are recorded thus according to the different year of collection:
ahid axhwght alewght alrwght aregion aregion2 ............. in separate file called ahhsamp
bhid bxhwght blewght blrwght bregion bregion2 ..............in separate file called bhhsamp
chid cxhwght clewght clrwght cregion cregion2 ...............in separate file called chhsamp
dhid dxhwght dlewght dlrwght dregion dregion2...............in separate file called dhhsamp
and so on till it reaches r
I want to loop over these different files in the same folder and all the files are containing the variables I need but are bearing different prefix as shown. I tried this code:
local files : dir "C\Users\Desktop\stata8\BHPS" files "*hhsamp.dta"
global dir1 "C:\Users\Ebenezer\Desktop\stata8\new merge"
foreach w in a d e f g h i j k l m n o p q r {
use `w'hid `w'xhwght `w'lewght `w'lrwght`w'region `w'region2 using file in `files', clear
renpfix `w'
gen wave = strpos("abcdefghijklmnopqr","`w'")
sort hid wave
lab dat "hhsamp1-18, long format"
save $dir1/hhsamp_junk1-18.dta", replace
}
I might not be entirely clear to some extent but I think you might understand what I am trying to do here. I will appreciate your help.
Thanks in advance.
Today
Hi Guys!
I am trying to create a merged file for some selected variables in a panel survey. Some people might be familiar with this data set. The data are recorded with different prefix as a b c d .... representing different wave (years). For instance, the variables are recorded thus according to the different year of collection:
ahid axhwght alewght alrwght aregion aregion2 ............. in separate file called ahhsamp
bhid bxhwght blewght blrwght bregion bregion2 ..............in separate file called bhhsamp
chid cxhwght clewght clrwght cregion cregion2 ...............in separate file called chhsamp
dhid dxhwght dlewght dlrwght dregion dregion2...............in separate file called dhhsamp
and so on till it reaches r
I want to loop over these different files in the same folder and all the files are containing the variables I need but are bearing different prefix as shown. I tried this code:
local files : dir "C\Users\Desktop\stata8\BHPS" files "*hhsamp.dta"
global dir1 "C:\Users\Ebenezer\Desktop\stata8\new merge"
foreach w in a d e f g h i j k l m n o p q r {
use `w'hid `w'xhwght `w'lewght `w'lrwght`w'region `w'region2 using file in `files', clear
renpfix `w'
gen wave = strpos("abcdefghijklmnopqr","`w'")
sort hid wave
lab dat "hhsamp1-18, long format"
save $dir1/hhsamp_junk1-18.dta", replace
}
I might not be entirely clear to some extent but I think you might understand what I am trying to do here. I will appreciate your help.
Thanks in advance.
Comment