Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Is a "merge, if" possible?


From   Phil Schumm <pschumm@uchicago.edu>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Is a "merge, if" possible?
Date   Tue, 20 Oct 2009 17:29:18 -0500

On Oct 20, 2009, at 4:47 PM, Polis, Chelsea B. wrote:
So, from within the dataset with three variables, I want to write something like:
merge study_id using "C:\Round 1.dta", if lastneground==1
merge study_id using "C:\Round 2.dta", if lastneground==2
and so on through round 10.

But, merge doesn't seem to be able to be combined with an if statement. Are there any elegant ways of doing this, or must I do it piecemeal (creating what I need in 10 separate datasets and appending)?

On Oct 20, 2009, at 4:58 PM, Nick Cox wrote:
The help for -merge- would tell you if -if- were allowed, and it is not.

<snip>

-append- can readily combine several datasets, unless you are not in Stata 11, in which case you should have told us that.


To add to what Nick said, this can be done in 2 lines of code. The first appends the Round 1, Round 2, etc. files, and uses the - generate()- option to create a new variable called lastneground containing the values 1, 2, etc. according to the source file. The second uses -merge- to perform a one-to-one merge with your subject- level file using the keys study_id and lastneground; here, you'll want to specify -keep(match)- (or perhaps -keep(match using)-) to retain one record per subject.


-- Phil

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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