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

st: Re: merging


From   Christopher F Baum <[email protected]>
To   [email protected]
Subject   st: Re: merging
Date   Wed, 18 Dec 2002 08:39:29 -0500

--On Wednesday, December 18, 2002 2:33 -0500 K. Ida wrote:

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)

- ----------
Why do you want the time variables to be 'year1997', 'year1998' and so on? It would seem obvious that 'year1997' == 1997, or perhaps 1 if it is a dummy for year, but it would seem sufficient to have a single variable 'year' which contains the year of each chunk of data.

Further if these files are unit records -- e.g. one record per employee per year -- don't you want to append them, rather than merging them? Unless you have different variable names in the various files, merging them will not likely be what you want (and if you do, it is even less likely to be what you want -- it will create a block-diagonal structure with mostly missing data). -append- would create a panel in 'personnel' with a t-indicator of year. Something like

forv i=1997/2001 {
use personnel_`i',clear
gen year = `i'
save personnel_`i',replace
}
use personnel,clear
forv i=1997/2001 {
append using personnel`i'
}
save personnel,replace

Note, btw, that

capture confirm variable _merge
if _rc {
continue
}
else {
drop _merge
}

can be replaced by

capture drop _merge


Kit
________________________________________________________________________
Christopher F Baum, Boston College Economics, Chestnut Hill MA 02467 USA
[email protected] http://fmwww.bc.edu/ec-v/baum.fac.html
*
* 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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index