Announcement

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

  • Create 2 new variables from existing variables: one date, and one with value equal to another variable's name

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(study_id red yellow green)
     1 16044 16532     .
     2     .     . 21254
     3     . 19516 19213
     4 18359     . 18890
     5     .     . 18438
     6 22042 17578     .
     7     .     . 20036
     8     . 20179     .
     9     .     . 15718
    10 18874 22474 19873
    end
    format %td red
    format %td yellow
    format %td green
    Here's what I need to do: generate 6 new variables.
    date1 and color1 will be the date and corresponding variable name (color) for one of the date values here. There should be no missing values for these 2 new variables because every row has at least 1 date in it.
    date2 and color2 will be the date and corresponding variable name (color) for the second date value, for those who have one. Those individuals who only had 1 date value (n=4)will be have missing values for these 2 new variables.
    date3 and color3 will be the date and corresponding variable name (color) for the third date value, for those who have it. Only 1 individual has 3 dates so this variable will have 9 missing values.

    Chronological order doesn't really matter, because I'm going to eventually reshape it to long anyway. So I don't care if date1 is the earliest date or date3 is earliest or if it's in some random order.

    I have no idea how to do this so any help would be much appreciated.

  • #2
    Something like this? :

    Code:
    //Current date:
    
    lis
    
         +----------------------------------------------+
         | study_id         red      yellow       green |
         |----------------------------------------------|
      1. |        1   05dec2003   06apr2005           . |
      2. |        2           .           .   11mar2018 |
      3. |        3           .   07jun2013   08aug2012 |
      4. |        4   07apr2010           .   20sep2011 |
      5. |        5           .           .   25jun2010 |
         |----------------------------------------------|
      6. |        6   07may2020   16feb2008           . |
      7. |        7           .           .   09nov2014 |
      8. |        8           .   01apr2015           . |
      9. |        9           .           .   13jan2003 |
     10. |       10   04sep2011   13jul2021   30may2014 |
         +----------------------------------------------+
    
    
    //You wanted:
    
    ren (red yellow green) (date1 date2 date3)
    
    reshape long date, i(study_id) j(col)
    clonevar color=col
    lab define c 1 "Red" 2 "Yellow" 3 "Green"
    lab val color c
    replace color =. if date==.
    
    reshape wide date color, i(study_id) j(col)
    
     lis, clean noobs
    
        study_id       date1   color1       date2   color2       date3   color3  
               1   05dec2003      Red   06apr2005   Yellow           .        .  
               2           .        .           .        .   11mar2018    Green  
               3           .        .   07jun2013   Yellow   08aug2012    Green  
               4   07apr2010      Red           .        .   20sep2011    Green  
               5           .        .           .        .   25jun2010    Green  
               6   07may2020      Red   16feb2008   Yellow           .        .  
               7           .        .           .        .   09nov2014    Green  
               8           .        .   01apr2015   Yellow           .        .  
               9           .        .           .        .   13jan2003    Green  
              10   04sep2011      Red   13jul2021   Yellow   30may2014    Green

    Roman

    Comment

    Working...
    X