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]

From |
Nick Cox <njcoxstata@gmail.com> |

To |
"statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |

Subject |
Re: st: Changing values future time points conditional on value from prior time point |

Date |
Tue, 25 Feb 2014 01:44:06 +0000 |

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 njcoxstata@gmail.com On 24 February 2014 22:17, Jeph Herrin <info@flyingbuttress.net> 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/

**Follow-Ups**:**RE: st: Changing values future time points conditional on value from prior time point***From:*"Kaufmann, Christopher N." <ckaufman@jhsph.edu>

**References**:**st: Changing values future time points conditional on value from prior time point***From:*"Kaufmann, Christopher N." <ckaufman@jhsph.edu>

**Re: st: Changing values future time points conditional on value from prior time point***From:*Jeph Herrin <info@flyingbuttress.net>

- Prev by Date:
**Re: st: Assigning values to a number of rows of a matrix** - Next by Date:
**RE: st: Assigning values to a number of rows of a matrix** - Previous by thread:
**Re: st: Changing values future time points conditional on value from prior time point** - Next by thread:
**RE: st: Changing values future time points conditional on value from prior time point** - Index(es):