Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: Re: Coding Question


From   john metcalfe <johnzmetcalfe@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Re: Coding Question
Date   Sun, 14 Aug 2011 21:40:49 +0200

Thank you very much.
John

On 8/14/11, Phil Schumm <pschumm@uchicago.edu> wrote:
> On Aug 14, 2011, at 11:22 AM, Steven Samuels wrote:
>> Even shorter:
>
> <snip>
>
>> reshape long meas, i(id) j(order)
>> bys id: gen convert = meas ==1 & meas[_n-1]==0
>> bys id: gen revert = meas == 0 & meas[_n-1] ==1
>> preserve
>> collapse (sum) n_convert=convert n_revert=revert, by(id)
>> gen both = n_convert*n_revert >0
>> list id n_convert n_revert both
>> restore
>
>
> You definitely want the data in long form here, but note that the code above
> will fail to capture a switch if it spans a missing value (e.g., 0 -> . ->
> 1).  Here is an alternative that does not have this limitation:
>
>     reshape long meas, i(id) j(order)
>     drop if mi(meas)
>     ass inlist(meas,0,1)
>     isid id order, so
>     egen conversion = max(meas>meas[_n-1] & id==id[_n-1]), by(id)
>     egen reversion  = max(meas<meas[_n-1] & id==id[_n-1]), by(id)
>     gen byte both = conversion & reversion
>
>
> -- 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/
>
*
*   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   |   Site index