Announcement

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

  • Adding extra years to an existing date variable

    Dear Statalist,

    I have a date variable created the following way:

    Code:
    gen PartDat_gli = .
    replace PartDat_gli = PartDat_NT2BLQ1 if copd_HUNT==1
    replace PartDat_gli = PartDat_NT3BLQ1 if copd_HUNT==2

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long(PartDat_NT2BLQ1 PartDat_NT3BLQ1) float PartDat_gli
    13444 17079     .
    13114 17293     .
    13619 17675     .
    13040     .     .
    13537     .     .
    13065     .     .
    13300 17289     .
    13661 17640     .
    13522 17538     .
    13116 17342     .
    13468     .     .
    13394     .     .
    13212 17448     .
    13117 17385     .
    13425     .     .
    13670 17674     .
    13377     .     .
    13403     .     .
    13654 17689     .
    13459 17274     .
    13535 17580 17580
    13647     .     .
    13207 17140 13207
        . 17651     .
        . 17644     .
    13236 17498     .
    13124 17282     .
    13082 17475     .
        . 17324 17324
    13388 17237     .
        . 17336     .
        . 17553     .
    13201     . 13201
    13216 17489     .
    13675 17644     .
        . 17189     .
    13480 17672     .
    13677 17660     .
    13472 17667     .
    13614     .     .
    13065 17343     .
        . 17632     .
        . 17623     .
    13403     .     .
    13675 17688     .
    13543 17230     .
    13569     .     .
    13683     .     .
    13457     .     .
    13440     . 13440
    13293     .     .
    13678 17660     .
    13055 17321     .
    13430 17092     .
    13165 17395     .
    13115     .     .
    13135     .     .
    13293     .     .
        . 17391     .
    13079 17447     .
        . 17707     .
    13193     .     .
    13461 17317     .
    13317 17238     .
    13123     .     .
    13206     .     .
    13591 17630 17630
    13475 17566     .
        . 17491     .
        . 17328     .
    13032     .     .
    13152     .     .
    13289 17589     .
    13499 17505     .
        . 17406     .
    13212     .     .
    13130 17205     .
    13191     . 13191
    13310 17283     .
    13221 17447     .
    13234 17492     .
    13038 17407     .
    13565     .     .
        . 17660     .
    13255     .     .
    13416     .     .
        . 17315     .
        . 17632     .
    13212     .     .
    13534     .     .
    13375     .     .
        . 17290     .
    13235 17174     .
        . 17475     .
    13424 17120     .
    13052     .     .
        . 17269     .
    13628     . 13628
    13212 17496     .
    13289     .     .
    end
    format %tdD_m_Y PartDat_NT2BLQ1
    format %tdD_m_Y PartDat_NT3BLQ1

    It is the participation date for individuals with COPD who participated in either of/both the first (NT2) or the second (NT3) cycle of a population study (their final PartDat_gli depends on when they were diagnosed with COPD, that's why some have the date from NT3 despite participating in both cycles). I use the date variable to -stset- before -stcox-.
    I now want to run a sensitivity analysis excluding the first 3 years of the follow-up time. How could I add 3 years for each participant to this date variable, taking into account the different participation dates? I tried to have a look at earlier threads in the forum, but I couldn't find a solution to my exact problem.

    Best regards,
    Sigrid Vikjord











    Last edited by Sigrid Vikjord; 01 Mar 2019, 02:06.

  • #2
    How could I add 3 years for each participant to this date variable
    There is no participant id in your data example. How do you identify a participant in your data?

    Comment


    • #3
      For the purposes of your question, after reading in your example data
      Code:
      . format %tdD_m_Y PartDat_gli
      
      . list if PartDat_gli !=. , clean abbreviate(16)
      
             PartDat_NT2BLQ1   PartDat_NT3BLQ1   PartDat_gli  
       21.         21 Jan 97         18 Feb 08     18 Feb 08  
       23.         28 Feb 96         05 Dec 06     28 Feb 96  
       29.                 .         07 Jun 07     07 Jun 07  
       33.         22 Feb 96                 .     22 Feb 96  
       50.         18 Oct 96                 .     18 Oct 96  
       67.         18 Mar 97         08 Apr 08     08 Apr 08  
       78.         12 Feb 96                 .     12 Feb 96  
       98.         24 Apr 97                 .     24 Apr 97
      So is your objective to increase the values of PartDat_gli by 3 years? If so, this is going to pose a problem for any February 29, because three years later there is no February 29. Perhaps you would be content to increase the values of PartDat_gli by 3*365 + 1 days, because the odds are 3:1 that a 3-year span will include a leap year day. If so, this will do.
      Code:
      . generate PartDat_gli3 = PartDat_gli + 365*3 + 1
      (92 missing values generated)
      
      . format %tdD_m_Y PartDat_gli3
      
      . list if PartDat_gli !=. , clean abbreviate(16)
      
             PartDat_NT2BLQ1   PartDat_NT3BLQ1   PartDat_gli   PartDat_gli3  
       21.         21 Jan 97         18 Feb 08     18 Feb 08      18 Feb 11  
       23.         28 Feb 96         05 Dec 06     28 Feb 96      28 Feb 99  
       29.                 .         07 Jun 07     07 Jun 07      07 Jun 10  
       33.         22 Feb 96                 .     22 Feb 96      22 Feb 99  
       50.         18 Oct 96                 .     18 Oct 96      19 Oct 99  
       67.         18 Mar 97         08 Apr 08     08 Apr 08      09 Apr 11  
       78.         12 Feb 96                 .     12 Feb 96      12 Feb 99  
       98.         24 Apr 97                 .     24 Apr 97      24 Apr 00
      Or we could get more accurate by adding another line of code. I think (but have not confirmed) that February 29 2000 would become February 28 2003.
      Code:
      . replace PartDat_gli3 = PartDat_gli3-1 if day(PartDat_gli3)!=day(PartDat_gli)
      (2 real changes made)
      
      . list if PartDat_gli !=. , clean abbreviate(16)
      
             PartDat_NT2BLQ1   PartDat_NT3BLQ1   PartDat_gli   PartDat_gli3  
       21.         21 Jan 97         18 Feb 08     18 Feb 08      18 Feb 11  
       23.         28 Feb 96         05 Dec 06     28 Feb 96      28 Feb 99  
       29.                 .         07 Jun 07     07 Jun 07      07 Jun 10  
       33.         22 Feb 96                 .     22 Feb 96      22 Feb 99  
       50.         18 Oct 96                 .     18 Oct 96      18 Oct 99  
       67.         18 Mar 97         08 Apr 08     08 Apr 08      08 Apr 11  
       78.         12 Feb 96                 .     12 Feb 96      12 Feb 99  
       98.         24 Apr 97                 .     24 Apr 97      24 Apr 00

      Comment

      Working...
      X