Hello everyone,
I am not a "good" Stata user, and I am struggling to use some complicated datasets.
I'm trying to import in Stata multiple text files (from the same folder) and save them as .dta files.
The next step would be to append them.
I am using the following code:
cd ""
local gold: dir . files "*.txt"
foreach file of local gold {
import delimited `file' , varnames(1)
local outfile = subinstr("`file'",".txt","",.)
save "`outfile'" , replace
clear
}
This code works fine I just have one issue.
Within the "gold" folder there are 229 text files.
They are named like this:
19_050_gold_extract_x_001
19_050_gold_extract_x_002
...
19_050_gold_extract_x_005
and so on.
Ideally when I save them in .dta I would like to keep only the "x_005" part of the file, is it possible? If yes, how?
Then I would like to append the 5 files together.
Within the same folder there are files that also need appending but seperately.
Essentially, files that have identical "x" part of the filename need to be appended together.
For example: groupping all the x files "19_050_gold_extract_x_00*" together (5 in total), the y files "19_050_gold_extract_y_00*" (25 in total), the z files "19_050_gold_extract_z_00*"(10 in total), etc.
I know how to do it manually and 1 by 1, but these are just a subset of files so ideally I would like something faster.
To sum up:
1) is there a way to get rid of the "19_050_gold_extract_" part of the filename when saving the .dta files?
2) is it possible to append files selectively on their filename?
I hope all that makes sense!
Thank you in advance,
Louisa
I am not a "good" Stata user, and I am struggling to use some complicated datasets.
I'm trying to import in Stata multiple text files (from the same folder) and save them as .dta files.
The next step would be to append them.
I am using the following code:
cd ""
local gold: dir . files "*.txt"
foreach file of local gold {
import delimited `file' , varnames(1)
local outfile = subinstr("`file'",".txt","",.)
save "`outfile'" , replace
clear
}
This code works fine I just have one issue.
Within the "gold" folder there are 229 text files.
They are named like this:
19_050_gold_extract_x_001
19_050_gold_extract_x_002
...
19_050_gold_extract_x_005
and so on.
Ideally when I save them in .dta I would like to keep only the "x_005" part of the file, is it possible? If yes, how?
Then I would like to append the 5 files together.
Within the same folder there are files that also need appending but seperately.
Essentially, files that have identical "x" part of the filename need to be appended together.
For example: groupping all the x files "19_050_gold_extract_x_00*" together (5 in total), the y files "19_050_gold_extract_y_00*" (25 in total), the z files "19_050_gold_extract_z_00*"(10 in total), etc.
I know how to do it manually and 1 by 1, but these are just a subset of files so ideally I would like something faster.
To sum up:
1) is there a way to get rid of the "19_050_gold_extract_" part of the filename when saving the .dta files?
2) is it possible to append files selectively on their filename?
I hope all that makes sense!
Thank you in advance,
Louisa
Comment