Announcement

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

  • Generating start and stop time variables

    Dear Stata users,

    I wonder if there is an easy way to generate 'start' and 'stop' days from baseline from using long format data.

    Dataex:

    PHP Code:
    ----------------------- copy starting from the next line -----------------------
    [
    CODE]
    Example generated by -dataex-. For more infotype help dataex
    clear
    input byte id str9
    (medication days_from_baselinebyte event
    "D-penicil" "0"         0
    "D-penicil" ".48187494" 0
    "D-penicil" ".99660498" 0
    "D-penicil" "2.0342789" 0
    "D-penicil" "2.7707808" 1
    "placebo"   "0"         0
    "placebo"   ".54484725" 0
    "placebo"   "1.070529"  0
    "placebo"   "2.1054649" 0
    "placebo"   "3.0062425" 0
    "placebo"   "3.9836819" 0
    "placebo"   "4.1205783" 0
    end
    [/CODE]
    ------------------ 
    copy up to and including the previous line ------------------ 


    I would like to create the following data.
    PHP Code:


    id medication   start         stop    event
    3 D
    -penicil   0          .48187494  0
    3 D
    -penicil   .48187494  .99660498  0
    3 D
    -penicil   .99660498 2.0342789   0   
    3 D
    -penicil  2.0342789  2.7707808   1
    5 placebo   0            .54484725  0
    5 placebo    .54484725  1.070529    0
    5 placebo   1.070529    2.1054649   0
    5 placebo   2.1054649   3.0062425   0
    5 placebo   3.0062425   3.9836819   0
    5 placebo   3.9836819   4.1205783   0 


    Many thanks,
    Oyun

  • #2


    Code:
    destring start, replace 
    
    bysort id (start) : gen stop = start[_n+1] 
    
    by id : drop if _n == _N 
    
    bysort id (start): replace event = 1 if _n == _N
    However, why is start string in the first place? See #8 in https://www.statalist.org/forums/for...ing-a-variable for some of the reasons why a variable that should be numeric has been created as string.

    It is a small point but please use CODE delimiters (# button in the forum interface), not PHP Code delimiters.

    Comment


    • #3
      Thanks very much Nick.
      It seems that something went wrong with 'start'. My apologies for the inconvenience. I will use CODE delimiters in the future.

      Comment

      Working...
      X