Hello,
I am a beginner in Stata and have been through many threads on here but have been unable to find the exact help I'm looking for. I hope this is not a repetitive question.
I have one big folder with 32 subfolders inside it. Each of the subfolders has this name format: "eic2015_01_dta", "eic2015_02_dta", and so on until 32.
Within each subfolder, I have 2 dta files and 2 do files. The dta file names are formatted as such: "Tr_persona01.dta" and "Tr_vivienda01.dta", the number corresponding to the subfolder it is in. The do files are named "personas.do" and "viviendas.do". They are the same do files in every subfolder (this is just how it downloaded from the original source).
For each subfolder, I am trying to do several operations. First, I want to run the "personas.do" in the persona dta file and save that as a new dta file, then I want to run the "viviendas.do" on the viviendas dta file and save that as a new data file, then I want to merge the new dta files.
My latest attempts have gone something like this (this is just for the first part, where I run the do file in the dta file and save):
However, it keeps returning the error: file ./local/Tr_vivienda*.dta not found.
I'm stuck. Am I supposed to be setting a cd that I'm not doing? Am I supposed to be doing a second loop within the loop for each file? How do I tell stata that the name for each dta file will depend on the subfolder it is in?
Please be gentle, this is my first time using loops!
Thank you in advance!
I am a beginner in Stata and have been through many threads on here but have been unable to find the exact help I'm looking for. I hope this is not a repetitive question.
I have one big folder with 32 subfolders inside it. Each of the subfolders has this name format: "eic2015_01_dta", "eic2015_02_dta", and so on until 32.
Within each subfolder, I have 2 dta files and 2 do files. The dta file names are formatted as such: "Tr_persona01.dta" and "Tr_vivienda01.dta", the number corresponding to the subfolder it is in. The do files are named "personas.do" and "viviendas.do". They are the same do files in every subfolder (this is just how it downloaded from the original source).
For each subfolder, I am trying to do several operations. First, I want to run the "personas.do" in the persona dta file and save that as a new dta file, then I want to run the "viviendas.do" on the viviendas dta file and save that as a new data file, then I want to merge the new dta files.
My latest attempts have gone something like this (this is just for the first part, where I run the do file in the dta file and save):
Code:
cd "." local subfolders : dir "." dirs "eic2015_*_dta" foreach folder in local subfolders { use "./`folder'/Tr_vivienda*.dta" do viviendas.do save "./`folder'/Tr_vivienda_`folder'.dta", replace }
I'm stuck. Am I supposed to be setting a cd that I'm not doing? Am I supposed to be doing a second loop within the loop for each file? How do I tell stata that the name for each dta file will depend on the subfolder it is in?
Please be gentle, this is my first time using loops!
Thank you in advance!
Comment