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: Re: Coding Question
From
Phil Schumm <[email protected]>
To
[email protected]
Subject
Re: st: Re: Coding Question
Date
Sun, 14 Aug 2011 11:38:12 -0500
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/