Hi Statalist Users,
I read a text delimited file using the standard command:
> import delimited "G:\Shared drives\Bank Stock Prices\Match\Gary\All_States_Features\AK_Features_ 20191101.txt", varnames(1) encoding(UTF-8)
The command worked as anticipated.
Now, I want to import data for all 50 US states plus some territories, which I save and merge. To do this, I wrote the loop.
foreach x in AK AL AR AS AZ CA CO CT DC DE FL FM GA GU HI IA ID IL IN KS KY LA MA MD ME MH MI MN MO MP MS MT NC ND NE NH NJ NM NV NY OH OK OR PA PR PW RI SC SD TN TX UM UT VA VI VT WA WI WV WY {
local ST "G:\Shared drives\Bank Stock Prices\Match\Gary\All_States_Features""`x'""_Featu res_20191101.txt"
display "`x'"
display "`ST'"
import delimited "`ST'", varnames(1) encoding(UTF-8)
* I have some commands here that save the information that I need as as DTA files. After the loop, I will append them to each other.
clear
}
When I run this code, it returns.
**********************
. foreach x in AK AL AR AS AZ CA CO CT DC DE FL FM GA GU HIIA ID IL IN KS KY LA MA MD ME MH MI MN MO MP
> MS MT NC ND NE NH NJ NM NV NY OH OK OR PA PR PW RI SC SD TN TX UM UT VA VI VT WA WI WV WY {
2. local ST "G:\Shared drives\Bank Stock Prices\Match\Gary\All_States_Features""`x'""_Featu res_201911
> 01.txt"
3. display "`x'"
4. display "`ST'"
5. import delimited ""`ST'"" , varnames(1) encoding(UTF-8)
6. clear
7. }
AK
G:\Shared drives\Bank Stock Prices\Match\Gary\All_States_Features\AK_Features_ 20191101.txt
using required
r(100);
end of do-file
*****************
The display commands tell me that STATA put `x' correctly into the file name, but the import command isn't reading the file name correctly. Can anyone help me get the syntax correct in the important command in my loop.
Thanks
Gary
Related Posts with Looping through Import Delimited command using locals
Stata Graph - scheme burd not foundHi I am trying to use the following command to formate my Stata graph. grstyle init grstyle color…
Alternatives to using split and then reshape long?I am trying to separate out data into rows, but without using split and reshape. Consider the follow…
Multiplying with missing valuesI want to generate a new variable showing total hours of care per month. I generate this variable ba…
vertical line graph two wayDear all 1) I'd like to put a vertical line on the x-axis in my Stata Graph. I need this line to sh…
Multiple imputation and stsum by groupsI have a dataset with multiple imputations in which I have performed Cox regression: mi stset outda…
Subscribe to:
Post Comments (Atom)
0 Response to Looping through Import Delimited command using locals
Post a Comment