Hello all,
I need to manipulate the date variable in a few specific .dta files within a folder, and they are ordered by year. All of the file names begin with "extract_" and are followed by the respective year.
(e.g., extract_2010
extract_2011
extract_2012, etc.)
What I would like to do is run a loop of commands for some of those files, and a slightly different one for the rest. The code I am using currently grabs all of the files in the folder, but I would actually like to run the code pasted below on only the first 7 years (2010-2016) .
I know that I can select files with certain prefixes by entering the beginning portion of the filename when I specify which files my local dataset should contain (for instance "extract_*.dta"). I'm wondering if I can modify this in some way to only place the first 7 files (2010-2016) in the local dataset?
Please see below.
cd "E:\file_location"
local datasets : dir . files "*.dta"
foreach file of local datasets {
use "`file'", clear
tostring incdate, gen (date)
gen date2 = date(date, "YMD")
format %tdnn/dd/CCYY date2
drop date incdate
rename date2 (incident_date)
order incident_date, after (incnum)
egen unique_incident_id = concat(ori incnum), punct(" ")
order unique_incident_id, after (incnum)
save "`file'", replace
}
Thank you kindly for your time and attention to this question.
I need to manipulate the date variable in a few specific .dta files within a folder, and they are ordered by year. All of the file names begin with "extract_" and are followed by the respective year.
(e.g., extract_2010
extract_2011
extract_2012, etc.)
What I would like to do is run a loop of commands for some of those files, and a slightly different one for the rest. The code I am using currently grabs all of the files in the folder, but I would actually like to run the code pasted below on only the first 7 years (2010-2016) .
I know that I can select files with certain prefixes by entering the beginning portion of the filename when I specify which files my local dataset should contain (for instance "extract_*.dta"). I'm wondering if I can modify this in some way to only place the first 7 files (2010-2016) in the local dataset?
Please see below.
cd "E:\file_location"
local datasets : dir . files "*.dta"
foreach file of local datasets {
use "`file'", clear
tostring incdate, gen (date)
gen date2 = date(date, "YMD")
format %tdnn/dd/CCYY date2
drop date incdate
rename date2 (incident_date)
order incident_date, after (incnum)
egen unique_incident_id = concat(ori incnum), punct(" ")
order unique_incident_id, after (incnum)
save "`file'", replace
}
Thank you kindly for your time and attention to this question.

Comment