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 at the end of May, and its replacement, statalist.org is already up and running.


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

st: RE: repeated measures


From   Nick Cox <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   st: RE: repeated measures
Date   Tue, 7 Sep 2010 21:07:15 +0100

The more difficult part of this is an FAQ, as at 

FAQ     . . . . . . . . . . . . . . . . . . . . . . . Replacing missing values
        2/03    How can I replace missing values with previous or
                following nonmissing values?
                http://www.stata.com/support/faqs/data/missing.html

The easier part of this is an application of -by()-, as at 

SJ-2-1  pr0004  . . . . . . . . . . Speaking Stata:  How to move step by: step
        Q1/02   SJ 2(1):86--102                                  (no commands)
        explains the use of the by varlist : construct to tackle
        a variety of problems with group structure, ranging from
        simple calculations for each of several groups to more
        advanced manipulations that use the built-in _n and _N

To copy lab results backward in time, try this

gen negvisitdate = - visitdate 
bysort patient (negvisitdate) : replace sbp = sbp[_n-1] if missing(sbp) 
bysort patient (negvisitdate) : replace dm = dm[_n-1] if missing(dm)
bysort patient (negvisitdate) : replace glu = glu[_n-1] if missing(glu)

Now 

drop negvisitdate 
bysort patient (visitdate) : keep if _n == 1 

Alternatively, David Kantor has a utility -carryforward- at SSC. But something like the code here should suffice if your example is indicative. 

Nick 
n.j.cox@durham.ac.uk 

Michael Eisenberg

I have a problem I hope you can help with that involves cleaning a
dataset for analysis.

I have a dataset with about 10K men.  Several men had multiple clinic
visits, so that there are about 12K observations.  There is also some
lab data that I'll need that was not obtained until follow up visits.

I would like to only analyze data from the earliest visit and the
first available lab data.

Can stata do this?


Turn this

patient     visitdate    sbp       dm          glu
1              1/1/09       140       .             .
1              1/4/09       128       .            202
1              2/1/09       131      1            .

2              4/1/09       160      .             341
2              4/4/09       144       .            180
2              5/1/09       170      1            .

3              6/1/09       119       .             .
3              6/4/09       107       .            .
3              7/1/09       124      .            96

4              9/1/09       104       1          110
4              9/4/09       155       .            .
4             10/1/09       .         .              .

Into this

patient     visitdate    sbp       dm          glu
1              1/1/09       140      1             202
2              4/1/09       160      1            341
3              6/1/09       119       .            96
3              9/1/09       104      1            110


*
*   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