Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • generating a new var and replacing its values with the previous year's values

    Hi everyone,
    A sample of my data looks as follows (my current data ). As you can see, the person 1's starting health status (health_1) is H (healthy) in 2000 then becomes U (unhealthy) in 2002 and again H (healthy) in 2004. I would like to generate a new health variable (e.g., health_2) and replace it with the respondent health status in the previous wave.
    Thanks.

    Nader

    *My current data
    Code:
    clear all
    input id year str6 (health_1)
    1 2000 "H"
    1 2002 "U"
    1 2004 "H"
    2 2000 "H"
    2 2002 "H"
    3 2000 "H"
    3 2002 "H"
    3 2004 "H"
    end
    list
    *My goal
    Code:
    clear all
    input id year str6 (health_1 health_2)
    1 2000 "H" ""
    1 2002 "U" "H"
    1 2004 "H" "U"
    2 2000 "H" "H"
    2 2002 "H" "H"
    3 2000 "H" "H"
    3 2002 "H" "H"
    3 2004 "H" "H"
    4 2005 ""  "new sample"
    end
    list

  • #2
    Well, your proposed result is not consistent with your statement of what you want. How can health_2 be "H" for id 2 in 2000: there is no previous wave, so it should be missing. The same goes for id 3.

    The following code gives what you said you wanted, not what you showed:
    Code:
    by id (year), sort: gen health_2 = health_1[_n-1]
    Note: The above assumes that no id ever skips a wave.

    Comment

    Working...
    X