Announcement

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

  • Help with some general housekeeping - creating a new variable based on longitudinal data using loops

    Hi All

    I have 4 variables; cancer1, cancer2, cancer3 & cancer4, which show the year of diagnosis of cancer (for one up to 4 possible cancer diagnosis). A year of diagnosis indicates that a patient has had cancer, otherwise missing.

    Based on the above 4 variables, I'd like to create 5 new variables (all binary, 0/1, where 1 indicates a cancer diagnosis):

    cancer82 - which will indicate if a patient received a cancer diagnosis up to & including 1982
    cancer89 - which will indicate if a patient received a cancer diagnosis between 1983 and 1989
    cancer99 - which will indicate if a patient received a cancer diagnosis between 1990 and 1999
    cancer09 - which will indicate if a patient received a cancer diagnosis between 2000 and 2009
    cancer15 - which will indicate if a patient received a cancer diagnosis between 2010 and 2015

    An example of the dataset is below:

    id cancer1 cancer2 cancer3 cancer4
    1 2000
    2
    3 2015
    4
    5
    6 2011 2013
    7 1988 2001
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28 2009
    29
    30
    31
    32
    33
    34
    35
    36 1989
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51 2000
    52 2004
    53
    54 1999


    Any ideas how to go about the above using loops (to take care of any subject who might have had more than one diagnosis?

    Many Thanks!

    /Amal

  • #2
    Code:
    local previous = 0 
    
    foreach v in 1982 1989 1999 2009 2015 { 
        
         gen cancer`v' = 0 
         forval j = 1/4 { 
               replace cancer`v' = inrange(cancer`j', `previous', `v') if cancer`v' == 0 
         } 
         local previous = `v' + 1 
    }

    Comment


    • #3
      Hi Nick - Thanks for the quick reply (as always!)

      I don't entirely understand the syntax you shared above:

      Code:
       
       foreach v in 1982 1989 1999 2009 2015 {            gen cancer`v' = 0
      Is the above meant to generate 4 separate variables cancer1982, cancer1989 etc? I thought I would have to specify the original variables (cancer1, cancer2, c ancer3, cancer4) to start with?

      Many Thanks
      /Amal

      Comment


      • #4
        Indeed. That is what the code you cite does, except that it creates 5 variables not 4. The inner loop uses the pre-existing variables.

        Comment


        • #5
          Thanks

          I get an error message: 'unexpected end of file' when I run the above code.

          /Amal

          Comment


          • #6
            You didn't give a data example in the form we asked for, which was one reason I didn't try my code on your data.

            With some engineering I get this code for a data example, which I trust matches your intent (people with missings on all cancer variables won't yield interesting or useful results). Then I applied the code I gave, with just one tweak because there are two variables in the data example, not four.

            Executive summary: My code works on your data. What you did differently I can't tell.

            You've been posting here for a while, but you're neglecting basics in your questions: Please give data examples we can use. Please show the exact code you used if it didn't work.

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input float id int(cancer1 cancer2)
             1 2000    .
             3 2015    .
             6 2011 2013
             7 1988 2001
            28 2009    .
            36 1989    .
            51 2000    .
            52 2004    .
            54 1999    .
            end
            
            
            local previous = 0 
            
            foreach v in 1982 1989 1999 2009 2015 { 
                
                 gen cancer`v' = 0 
                 forval j = 1/2 { 
                       replace cancer`v' = inrange(cancer`j', `previous', `v') if cancer`v' == 0 
                 } 
                 
                 local previous = `v' + 1 
            }
            
            list 
            
                 +-------------------------------------------------------------------------------+
                 | id   cancer1   cancer2   can~1982   can~1989   can~1999   can~2009   can~2015 |
                 |-------------------------------------------------------------------------------|
              1. |  1      2000         .          0          0          0          1          0 |
              2. |  3      2015         .          0          0          0          0          1 |
              3. |  6      2011      2013          0          0          0          0          1 |
              4. |  7      1988      2001          0          1          0          1          0 |
              5. | 28      2009         .          0          0          0          1          0 |
                 |-------------------------------------------------------------------------------|
              6. | 36      1989         .          0          1          0          0          0 |
              7. | 51      2000         .          0          0          0          1          0 |
              8. | 52      2004         .          0          0          0          1          0 |
              9. | 54      1999         .          0          0          1          0          0 |
                 +-------------------------------------------------------------------------------+

            Comment


            • #7
              Perfect thanks Nick!

              It worked well

              Comment

              Working...
              X