Announcement

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

  • Convert SAS code into Stata code for Time-to-event analysis

    Hello,

    I am trying to convert a SAS code provided in data documentation to Stata code and tried the following, but do not know what loop I should use as I keep getting errors or all observations dropped:
    Code:
    *Sort the dataset by RANDID in descending order and then by PERIOD.
    gsort RANDID -PERIOD
    *Initialize variables
    gen NEWEVNT = .
    gen ENDTIME = .
    gen EXMTIME = .

    The instruction were as follows:

    For time-dependent analysis, or a counting process style of input, the user would have to subset the population to those free of disease at all exams and event data would have to be modified to reflect when the event occurred relative to the examinations.

    Consider the following SAS code which would modify the dataset to a counting process style of input for an analysis on the Hospitalized MI-Fatal CHD endpoint. The variable NEWEVNT is modified from MI_FCHD so that the event indicator is ‘1' only once for each participant. The variables TIME and ENDTIME define the interval the subject is at risk:


    Code:
    proc sort data=ANALYSIS;
        by RANDID descending PERIOD;
    data ANALYSIS;
        set ANALYSIS;
        by RANDID;
        NEWEVNT=MI_FCHD;
        retain EXMTIME;
        if first.RANDID then do;
            ENDTIME=TIMEMIFC;
            EXMTIME=TIME;
        end;
        else do;
            NEWEVNT=0;
            ENDTIME=EXMTIME;
            EXMTIME=TIME;
        end;
    proc sort data=ANALYSIS;
        by RANDID PERIOD;
    run;
    Thank you

  • #2
    You forgot to include the first thee lines of SAS code shown in that document:
    Code:
    DATA ANALYSIS; SET WORK; IF PREVCHD=0;
    Anyway, try something like the following.
    Code:
    quietly drop if prevchd
    bysort RANDID (period): generate byte new_event = cond(_n == _N, mi_fchd, 0)
    by RANDID: generate int end_time = cond(_n == _N | new_event, timemifc, time[_n+1])
    Stata is case-sensitive and so capitalization of variable names in the Stata code above follows that shown in the listings in the document, not that of the SAS code.

    Full DO-file and log file attached if you're interested.
    Attached Files

    Comment


    • #3
      Thank you so much, Joseph! I spent so long trying to figure this out. Tried by RANDID gen, forval, foreach and such. Did not know about cond for generating variables.

      Comment


      • #4
        Originally posted by Sadiah Ashraf View Post
        Did not know about cond for generating variables.
        Yeah, it's a convenience.

        You can simplify the Stata code even further by omitting the | new_event condition in the last line of code. It's redundant and I'm not sure why I put it in.
        Code:
        quietly drop if prevchd
        bysort RANDID (period): generate byte new_event = cond(_n == _N, mi_fchd, 0)
        by RANDID: generate int end_time = cond(_n == _N, timemifc, time[_n+1])
        is all you need.

        Comment

        Working...
        X