Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Looping for dictionary file


From   Joseph Coveney <jcoveney@bigplanet.com>
To   Statalist <statalist@hsphsun2.harvard.edu>
Subject   Re: st: Looping for dictionary file
Date   Fri, 28 Sep 2007 10:06:16 -0700

Rijo John wrote [excerpted]:

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
}


--------------------------------------------------------------------------------

Try -save , emptyok- before the loop.

Also, pull your constant local macro variables outside of the loop; you
don't need to redefine them each pass through it.

And you don't need to -sort- in order to -append-, or to -clear- each pass
through the loop. Double quotes are needed when you have spaces in the path.

Joseph Coveney

clear *
save `outfile', emptyok
local dict D:/Paper/country.dct
local outfile D:/Paper/country.dta
forvalues i = 1/10 {
infile using `dict' if level==2, using(D:\Data\state`i'.txt) clear
append using `outfile'
save `outfile', replace
}
sort fsu hamgroup sstratum hldno
save `outfile', replace
exit


*
* 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/




© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index