Hi list,
I am trying to use a simple loop function. My raw data (ASCII file)
is split into 10 files, one for each state. I need to extract certain
variables from each of these state raw data files and store them in
one file for the country as a whole. So I want the data from frist
state to be saved to country.dta file and then append the successive
state files to the same country.dta file.
I run the following loop and I am getting the solution. However, there
is a small problem. At present, as I have written it, the loop runs
from state 2 to 10 only. I need to run the sate1 extraction first.
Then save it as country.dta and then use the following program to run
the remaining loop to create the final data. But is there someway I
can avoide the first step. If I run the loop from 1/10 without running
1 separately, it will give me the error saying country.dta doesnt
exist while I try to append. Can someone help me to figure out a
better way to loop it?
forvalues i = 2/10{
local raw_data "D:\Data\state`i'.txt"
local dict "D:\Paper\country.dct"
local outfile "D:\Paper\country.dta"
infile using `dict' if level==2, using (`raw_data') clear
sort fsu hamgroup sstratum hldno
*save "D:\Paper\country.dta", replace
append using "D:\Paper\country.dta"
sort fsu hamgroup sstratum hldno
save `outfile', replace
clear
}
Thanks in advance,
--RM John.
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/