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: Need help converting values into factor variable
From
Nick Cox <[email protected]>
To
[email protected]
Subject
Re: st: Need help converting values into factor variable
Date
Sat, 1 Dec 2012 12:53:53 +0000
sum() create running sum (see -help sum()-)
year != year[_n-1] year not equal to previous year
expression evaluated as 1 if true and 0 if false
see
FAQ . . . . . . . . . . . . . . . . . . . . . . . True and false in Stata
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
2/03 What is true and false in Stata?
http://www.stata.com/support/faqs/data-management/true-and-false/
This works also for the _first_ observation for each patient as the
previous year is then evaluated as missing (which is fine; missing is
not any of 2009 ... 2012)
inrange(year, 2009, 2012) year in range 2009 to 2012 (may be
superfluous if you _don't_ have other years -- but you did not say
that)
Nick
On Sat, Dec 1, 2012 at 12:44 PM, Michael Stewart
<[email protected]> wrote:
> I am trying to decipher the code written by you(sort, I am by no means
> and stata expert )
> Can you explain what this code is doing; especially the argument (
> sum(year != year[_n-1] &
>> inrange(year, 2009, 2012))
On Sat, Dec 1, 2012 at 7:17 AM, Nick Cox <[email protected]> wrote:
>> This is not "Statlist"!
>>
>> You should show us your code that you tried to get an explanation of
>> what you are getting wrong.
>>
>> bysort patient (year) : gen flag = sum(year != year[_n-1] &
>> inrange(year, 2009, 2012))
>> by patient : replace flag = flag[_N]
>>
>> Nick
>>
>> On Sat, Dec 1, 2012 at 11:55 AM, Michael Stewart
>> <[email protected]> wrote:
>>
>>> Dear Statlist reader ,
>>> I am having trouble converting my values into factor variables and am
>>> looking for help. My data looks as follows where "patientid" is pt
>>> identification number and "year_elevated" is year in which a
>>> particular lab value was elevated
>>>
>>>
>>> patientid year_elevated
>>> 111 2009
>>> 111 2009
>>> 222 2009
>>> 222 2011
>>> 222 2012
>>> 333 2009
>>> 333 2012
>>> 444 2011
>>> 444 2012
>>> 555 2009
>>> 555 2010
>>> 555 2011
>>> 555 2012
>>>
>>> I want to create a factor variable (y )with values 1= ,2 , 3 and 4
>>> where for each distinct patientid
>>> y=1 if year_elevated has anyone of ( 2009 or 2010 or 2011 or 2012) values
>>> y=2 if year_elevated has any two of ( 2009 , 2010 , 2011 , 2012) values
>>> y=3 if year_elevated has any three of ( 2009 , 2010 , 2011 , 2012) values
>>> y=4 if year_elevated has all four of ( 2009 , 2010 , 2011 , 2012) values
*
* 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/