Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

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

Re: st: Carry forward an observation within a time frame

From   David Kantor <>
Subject   Re: st: Carry forward an observation within a time frame
Date   Wed, 09 Oct 2013 11:32:31 -0400

Dear Benigno,

At 10:45 AM 10/9/2013, you wrote:
Dear David:

Thank you for this detailed response. But my problem is that I need to carry forward to a given row with a missing CD4 the closest in time CD4 value, as long as it is within the previous 4 months (the last 120 days is enough precision for my purposes).

Correct me if I'm thinking about this wrong, but with your approach, a value could be propagated down even if the "seed" for that value occurred more than 4 months earlier. I have added a few rows (artificially) to illustrate. Look at row 10. It occurs within 4 month of row[_n-1], but I don't want it filled out with that value, because the last time we actually had a non-missing value was in row 3, which is more than 4 months prior to row 10.

The approach I was trying was to try to define a "spell" as one that begins with a missing CD4 AND for which the row above has a non-missing value (easily done). But implementing the solution from there has proven challenging.

        patid   date            CD4     desired
1.      1007    5-May-55        .       .
2.      1007    1-Jan-00        .       .
3.      1007    3-Apr-02        5       5
4.      1007    8-Apr-02        .       5
5.      1007    11-Apr-02       .       5
6.      1007    13-May-02       .       5
7.      1007    30-May-02       .       5
8.      1007    30 Jun-02       .       5
9.      1007    30-Jul-02       .       5
10.     1007    30-Aug-02       .       .

Indeed, that is a slightly more-involved problem than I understood; I didn't read your original question with adequate attention. You don't want to carry a value too long; it should expire after 120 days.
Try this:
gen int seeddate = date if ~mi(CD4)
by patid (date): carryforward seeddate, replace
by patid (date): carryforward CD4 if date-seeddate < 120, replace /* or gen(newvar) */


*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index