Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down at the end of May, and its replacement, **statalist.org** is already up and running.

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

From |
Rahul Ahluwalia <Rahul_Ahluwalia@isb.edu> |

To |
"statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |

Subject |
RE: st: Panel data - Finding a match for an observation in one variable, from among some values of a different variable, in a previous time period |

Date |
Tue, 27 Nov 2012 17:16:03 +0530 |

That answer's pretty close to the approach I used, but because a previous reply in Statalist(I think by Maarten) suggested that looping over observations was a last resort idea, I presumed it wasn't a good way of doing it. Thank you! -----Original Message----- From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox Sent: Tuesday, November 27, 2012 4:46 PM To: statalist@hsphsun2.harvard.edu Subject: Re: st: Panel data - Finding a match for an observation in one variable, from among some values of a different variable, in a previous time period You don't show us your code so we have no way of knowing whether any solutions we come up with are any use to you. But for this kind of problem, I would just loop over observations. gen match_count = . gen match_id = . forval i = 1/`=_N' { qui if (test_flag[`i']) { su id if (comp_value == match_value[`i] & (year == year[`i'] - 1) & (batch == batch[`i']), meanonly replace match_id = r(min) in `i' replace match_count = r(N) in `i' } } Here I take -id- to be numeric. The lowest matching -id- is used; the counting of matching -id-s is easier than knowing what to do with any -id-s that match. For this technique see SJ-7-3 pr0033 . . . . . . . . . . . . . . Stata tip 51: Events in intervals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox Q3/07 SJ 7(3):440--443 (no commands) tip for counting or summarizing irregularly spaced events in intervals Nick On Tue, Nov 27, 2012 at 10:45 AM, Rahul Ahluwalia <Rahul_Ahluwalia@isb.edu> wrote: > First of all, thank you. I've found many of the solutions posted here to be very helpful. I'll try and explain my current problem as best I can. I have some very inefficient and inelegant code that gets me what I need, but I would appreciate knowing the 'proper' way of doing this. > > I have the following variables : year, id, comp_value, match_value, > batch, test_flag. The data is in an unbalanced panel using year and > id. test_flag indicates when an id enters the panel. I need to do the > following - whenever test_flag == 1, I need to check that > observation's match_value with the comp_value of all observations in > the previous year that belong to the same batch and make note of the > id. e.g > > > year id comp_value match_value batch test_flag match_id > 1999 1 505 76 9 0 . > 1999 2 500 89 8 0 . > 1999 3 505 98 8 0 . > 2000 1 55 10 9 0 . > 2000 2 500 40 9 0 . > 2000 4 500 505 8 1 . > 2001 5 0 55 8 1 . > > The code I need would compare the match_value of id 4, year 2000, i.e 505, check it against the comp_value of all observations from year 1999 and the same batch(8) and change match_id to 3. The code would also check year 2001 and id 5 but would not cause any change in match_id. What would be really great is if another variable, say, match_count would tell me if there are multiple matches. * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/ * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/

**Follow-Ups**:**Re: st: Panel data - Finding a match for an observation in one variable, from among some values of a different variable, in a previous time period***From:*Nick Cox <njcoxstata@gmail.com>

**References**:**st: Panel data - Finding a match for an observation in one variable, from among some values of a different variable, in a previous time period***From:*Rahul Ahluwalia <Rahul_Ahluwalia@isb.edu>

**Re: st: Panel data - Finding a match for an observation in one variable, from among some values of a different variable, in a previous time period***From:*Nick Cox <njcoxstata@gmail.com>

- Prev by Date:
**Tobit regression [was: Re: st: RE: Panel data - Finding a match for an observation in one variable, from among some values of a different variable, in a previous time period]** - Next by Date:
**Re: st: example about choice experiment datasheet** - Previous by thread:
**Re: st: Panel data - Finding a match for an observation in one variable, from among some values of a different variable, in a previous time period** - Next by thread:
- Index(es):