Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
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 <[email protected]>
To
[email protected]
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 11:16:09 +0000
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
<[email protected]> 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/