I am trying to change variable names in about 60 files after which I would like to append them and create one large file.
Each file has the following naming structure avgyyyymm.dta and all of them are stored in the same folder. The files all have exactly the same number of observations and variables (a value for satellite night lights and the latitude and longitude)
In addition to the existing variables, I would like to add two variables: year (yyyyy) and month (mm) based on the file name.
Finally, I would like to append all of these files into one master dataset.
I am getting stuck trying to figure out the correct syntax to save the files with the new variable names in a new folder. The do file below is of two parts. The first part involves changing the variable names and the second part involves appending. I am having problems with with correct way to save the altered files in a new folder in the first part.
Any help would be appreciated. Thanks.
*****
local filelist : dir "G:\resampled" files "avg*.dta"
foreach f of local filelist {
use "`f'"
gen year=substr("`f'",4,4)
gen month=substr("`f'",8,2)
save using "G:\resampled_temp\`f'", replace
}
cd "G:\resampled_temp"
! dir avg*.dta /a-d /b >lightmasterlist.txt
*create an empty file that will be used to append everything else
file open myfile using lightmasterlist.txt, read /*reads the first line from the masterlist of files*/
file read myfile line
use `line' /*opens the dataset on the first line*/
save master_lightsdata, replace
file read myfile line
while r(eof)==0 { /* while you're not at the end of the file */
append using `line'
file read myfile line
}
file close myfile
save master_lightsdata, replace
Each file has the following naming structure avgyyyymm.dta and all of them are stored in the same folder. The files all have exactly the same number of observations and variables (a value for satellite night lights and the latitude and longitude)
In addition to the existing variables, I would like to add two variables: year (yyyyy) and month (mm) based on the file name.
Finally, I would like to append all of these files into one master dataset.
I am getting stuck trying to figure out the correct syntax to save the files with the new variable names in a new folder. The do file below is of two parts. The first part involves changing the variable names and the second part involves appending. I am having problems with with correct way to save the altered files in a new folder in the first part.
Any help would be appreciated. Thanks.
*****
local filelist : dir "G:\resampled" files "avg*.dta"
foreach f of local filelist {
use "`f'"
gen year=substr("`f'",4,4)
gen month=substr("`f'",8,2)
save using "G:\resampled_temp\`f'", replace
}
cd "G:\resampled_temp"
! dir avg*.dta /a-d /b >lightmasterlist.txt
*create an empty file that will be used to append everything else
file open myfile using lightmasterlist.txt, read /*reads the first line from the masterlist of files*/
file read myfile line
use `line' /*opens the dataset on the first line*/
save master_lightsdata, replace
file read myfile line
while r(eof)==0 { /* while you're not at the end of the file */
append using `line'
file read myfile line
}
file close myfile
save master_lightsdata, replace
Comment