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: Changing values future time points conditional on value from prior time point
From
"Kaufmann, Christopher N." <[email protected]>
To
"[email protected]" <[email protected]>
Subject
RE: st: Changing values future time points conditional on value from prior time point
Date
Mon, 24 Feb 2014 21:17:19 -0500
Thank you, Jeph and Nick. I really appreciate your help. This works!
All the best,
-Chris
________________________________________
From: [email protected] [[email protected]] On Behalf Of Nick Cox [[email protected]]
Sent: Monday, February 24, 2014 8:44 PM
To: [email protected]
Subject: Re: st: Changing values future time points conditional on value from prior time point
Note in addition to Jeph's good advice that you have the -if- command
and the -if- qualifier mixed up, presumably due to previous exposure
to some other language. With
if <condition> <action>
there is no tacit loop over all observations. On the contrary, the
command is executed just once. For more, see
http://www.stata.com/support/faqs/programming/if-command-versus-if-qualifier/
Nick
[email protected]
On 24 February 2014 22:17, Jeph Herrin <[email protected]> wrote:
> this should do
>
> bys id (time) : replace c_heartcond=1 if c_heartcond[_n-1]==1
>
>
> cheers,
> Jeph
>
>
>
> On 2/24/2014 4:50 PM, Kaufmann, Christopher N. wrote:
>>
>> I am working with data from a longitudinal cohort study with five time
>> points. The dataset is formatted in a "long" format. I would like to change
>> values of a variable for future time points conditional on the value of the
>> variable in a prior time point. Below is an example of the structure of my
>> dataset in its current form:
>>
>> ID Time c_heartcond
>> 1 1 0
>> 1 2 1
>> 1 3 0
>> 1 4 1
>> 1 5 0
>> 2 1 0
>> 2 2 0
>> 2 3 1
>> 2 4 1
>> 2 5 0
>>
>> ID = participant identification number
>> Time = time points of the study
>> c_heartcond = participant reported that they have a heart condition since
>> the last time they were interviewed (1 = yes, 0 = no)
>>
>> As you can see, a person can report a heart condition at one time point,
>> but report that they didn't have a heart condition the next time point. I
>> would like to change the c_heartcond variable so that all future time points
>> after the first that the subject reported the heart condition have a value
>> of 1. For example:
>>
>> ID Time c_heartcond
>> 1 1 0
>> 1 2 1
>> 1 3 1
>> 1 4 1
>> 1 5 1
>> 2 1 0
>> 2 2 0
>> 2 3 1
>> 2 4 1
>> 2 5 1
>>
>> I have written code to do this. See below:
>>
>> *Fixing comorbidity:
>> foreach var of varlist c_heartcond c_stroke c_hypertension c_pain
>> c_arthritis c_cancer c_diabetes c_incontin c_memorydis {
>> if c_order_expand == 1 & `var' == 1 {
>> foreach x1 in 2 3 4 5 {
>> replace `var' = 1 if c_order_expand == `x1'
>> }
>> }
>> if c_order_expand == 2 & `var' == 1 {
>> foreach x2 in 3 4 5 {
>> replace `var' = 1 if c_order_expand == `x2'
>> }
>> }
>> if c_order_expand == 3 & `var' == 1 {
>> foreach x3 in 4 5 {
>> replace `var' = 1 if c_order_expand == `x3'
>> }
>> }
>> if c_order_expand == 4 & `var' == 1 {
>> foreach x4 in 5 {
>> replace `var' = 1 if c_order_expand == `x4'
>> }
>> }
>> else {
>> replace `var' = `var' if c_order_expand == 5
>> }
>> }
>>
>>
>> But when browsing through the data after running this code, I notice that
>> there are instances where this data is not formatted correctly. For
>> example, the data might look like this after running my code:
>>
>> ID Time c_heartcond
>> 3 1 1
>> 3 2 0
>> 3 3 0
>> 3 4 0
>> 3 5 0
>>
>
*
* 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/