Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: -foreach- and -forvalues-


From   Ulrich Kohler <ukohler@sowi.uni-mannheim.de>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: -foreach- and -forvalues-
Date   Wed, 18 Dec 2002 09:47:27 +0000

Katsuhide Isa  wrote
> Hello,
>
> I'm trying to merge a collection of dta files and create
> new time-variables at the same time.
> Specifically, I'd like to execute the following two routines
> simultaneously:
>
> ----------routine 1----------
> foreach file in personnel_1997 personnel_1998 personnel_1999
> personnel_2000  personnel_2001 {
>    use "`file'"
>    sort id
>    save "`file'", replace
>    use personnel  /*master file*/
>    sort id
>    capture confirm variable _merge
>    if _rc {
>    continue
>    }
>    else {
>    drop _merge
>    }
>    merge id using "`file'"
>    save personnel, replace
> }
>
> ----------routine 2----------
> forvalues i = 1997/2001 {gen year`i' = `i''}
>
> ----------
>
> Is it possible to execute the above two routines
> combining -foreach- and -forvalues- simultaneously?
> For now, as I don't have a good idea, I repeatedly
> use the following simple routine as for routine 2:
>
> ----------routine 2'----------
> use personnel_1997, clear
> gen year1997 = 1997
> save personnel_1997_temp, replace
> (and so on)
>
> ----------


How about:

-----------------------------------
forvalues i = 1997/2001 {
     use "personnel_`i'"
     sort id
     gen year = `i'
     save "using", replace
     use personnel  
     sort id
     capture drop  _merge
     merge id using "using""
     save personnel, replace
}
erase "using.dta"
--------------------------------------

This combines your two loops and simplifies the code. Note that this 
generates only one variable for year, as this stores the same information and 
saves some bandwith. Moreover I store the using data temporarily with a 
different filename. This keeps your original data as it is, which is 
necassary if you want to rerun the code.

Personally I prefer doing this in two loops. In this case it is not necassary 
to reload the master file over and over again:

------------------------------------------
* Prepare using data 
forvalues i = 1997/2001 {
     use "personnel_`i'"
     sort id
     gen year = `i'
     save "using_`i', replace
}

* Load Master Data
use personnel  
sort id

* Merge Data
forvalues i = 1997/2001 {
	merge id using "using_`i'"
        drop _merge
        erase using_`i'.dta        /* Clean up things */
}

save personnel_new, replace
------------------------------------------------

Finally you might want to check out -mmerge- on SSC which makes merging much 
easier.


hope this helps
uli

-- 
ukohler@sowi.uni-mannheim.de
http://www.sowi.uni-mannheim.de/lesas

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