I would like to create variables within a dataset using parts of the filename. Is this possible? Does STATA store the filename in memory somewhere?
The filename is in the following format:
identifier_date_form.txt
I would like to create 3 variables in the file: identifier, date, form.
This will be part of a loop that loops over thousands of files, thus I will not be calling/loading each file by hand.
I don't know if this is helpful or not, but separately I am able to define the variables using the filename command.
The code to do that:
filelist, dir("C:\Users\dir\temp") pat("*.txt") save("txt_datasets.dta")
use txt_datasets.dta, replace
split filename, p(_)
gen id=filename1
gen date=filename2
gen form=filename3
keep filename date form id
save txt_datasets, replace
I just do not know how to load/call/identify the filename once the file is in use within the loop.
Thanks in advance.
The filename is in the following format:
identifier_date_form.txt
I would like to create 3 variables in the file: identifier, date, form.
This will be part of a loop that loops over thousands of files, thus I will not be calling/loading each file by hand.
I don't know if this is helpful or not, but separately I am able to define the variables using the filename command.
The code to do that:
filelist, dir("C:\Users\dir\temp") pat("*.txt") save("txt_datasets.dta")
use txt_datasets.dta, replace
split filename, p(_)
gen id=filename1
gen date=filename2
gen form=filename3
keep filename date form id
save txt_datasets, replace
I just do not know how to load/call/identify the filename once the file is in use within the loop.
Thanks in advance.
Comment