Announcement

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

  • SAS to STATA

    I'm hoping someone can help me recreate the following SAS code in STATA.

    if first.studyid_adult then do;
    spelnmbr=1;
    duration=1;
    startm=sm;
    end;
    else duration + 1;
    obplus=_n_ + 1;

  • #2
    sort studyid_adult
    gen x = _n

    bys studyid_adult (x) : replace spelnmbr=1 if _n ==1
    bys studyid_adult (x) : replace duration=1 if _n ==1
    bys studyid_adult (x) : replace startm=1 if _n ==1

    bys studyid_adult (x) : replace duration=duration+1 if _n > 1
    bys studyid_adult (x) : replace obplus=obplus+1 if _n > 1

    Comment


    • #3
      Originally posted by Laur Hous View Post
      I'm hoping someone can help me recreate the following SAS code in STATA.

      if first.studyid_adult then do;
      spelnmbr=1;
      duration=1;
      startm=sm;
      end;
      else duration + 1;
      obplus=_n_ + 1;
      Some of this is a little vague without a full description of what the block of code is intended to do. Rasool provided some code in #2, and we can condense that a little further.

      -obplus- simply creates an index value for all observations. This can be done by:

      Code:
      gen obplus = _n
      -duration- does something similar, but indexes observations within groups defined by -studyid_adult-.

      Code:
      sort studyid_adult
      bys studyid_adult (obplus) : gen duration = _n
      It also looks like you are counting spells, but it's not clear how those are defined or its relation to startm and sm, so I'm ignoring those for now. You may consider user-contributed commands to identify spells, for example -tsspell- (SSC) to make the process simpler than it would be in SAS.

      Comment


      • #4
        Thanks Leonardo and Rasool. This is part of some SAS code for survival analysis i am trying to duplicate in STATA. I could use help interpreting the rest of the code if you have the time. I got stuck at the creating spells section (which wasn't very far into the program). I created my own Survival Analysis code in STATA, but i'm not getting the same results that this SAS program gets. We differ by about two months in the final results so i figured i must have set up my data differently than this program does.


        Here is the full SAS code:

        libname save 'C:\Users\houstonl\Box\For Laurie\CCDBG-Phase II\data\SAS7BDAT files';


        * setting up the data to create family spells;
        proc sort data=save.subsidy_vars2;
        by studyid_adult sm;
        run;

        * this next code includes some variable creation;
        data all;
        set save.subsidy_vars2;

        if sm = 19997 then period = 1;
        if sm = 20028 then period = 2;
        if sm = 20058 then period = 3;
        if sm = 20089 then period = 4;
        if sm = 20120 then period = 5;
        if sm = 20148 then period = 6;
        if sm = 20179 then period = 7;
        if sm = 20209 then period = 8;
        if sm = 20240 then period = 9;
        if sm = 20270 then period = 10;
        if sm = 20301 then period = 11;
        if sm = 20332 then period = 12;
        if sm = 20362 then period = 13;
        if sm = 20393 then period = 14;
        if sm = 20423 then period = 15;
        if sm = 20454 then period = 16;
        if sm = 20485 then period = 17;
        if sm = 20514 then period = 18;
        if sm = 20545 then period = 19;
        if sm = 20575 then period = 20;
        if sm = 20606 then period = 21;
        if sm = 20636 then period = 22;
        if sm = 20667 then period = 23;
        if sm = 20698 then period = 24;
        run;
        data newall;
        set all;
        if sm = 19997 then leftcen = 1;
        else leftcen = 0;
        run;
        * Creating spells and startm and endm;
        data allmos;
        retain spelnmbr duration startm;
        retain lastdata 24;
        set newall nobs=nobs;
        by studyid_adult;

        if first.studyid_adult then do;
        spelnmbr=1;
        duration=1;
        startm=sm;
        end;
        else duration + 1;
        obplus=_n_ + 1;

        if _n_< nobs then
        set newall (keep=studyid_adult studyid_child sm period
        rename=(studyid_adult=studyid_adultx studyid_child=studyid_childx sm=smx period=periodx))
        point=obplus;

        if studyid_adult=studyid_adultx and periodx=period+1 and _n_<= nobs then do;
        *Next obs is in the same spell;
        status=0; exit=0;
        output;
        end;

        else do; *This obs is end of spell or censor;
        if period=lastdata then do;
        status=0; exit=0;
        end;
        else do;
        status=1; exit=1;
        output;
        *set up for next spell;
        spelnmbr+1;
        duration=0;
        end;
        end;
        run;
        * Change spelnmbr to 0 for the spell that includes left-censored cases;
        data mom_spell;
        set allmos;
        by studyid_adult;
        if first.studyid_adult then do;
        if leftcen=1 then chng=1;
        else chng=0;
        retain chng;
        end;
        if chng=1 then spelnmbr+(-1);
        run;
        data endcens (keep=studyid_adult studyid_child end_cens leftcengroup allelsegroup);
        set mom_spell;
        by studyid_adult;
        if last.studyid_adult then do;
        if sm eq 20698 then end_cens=1; else end_cens=0;
        if spelnmbr=0 then leftcengroup=1; else leftcengroup=0;
        if spelnmbr=>1 then allelsegroup=1; else allelsegroup=0;
        output;
        end;
        run;
        data save.familyspell_merge;
        merge mom_spell (in=a) endcens;
        by studyid_adult;
        if a then output;
        run;
        * check for number of parents in a given spell;
        data firstspell;
        set save.familyspell_merge;
        if spelnmbr=1;
        run;
        proc sort data=firstspell nodupkey; by studyid_adult; run;


        * Duration using newORscc data ~ last observation of first spell using KaplanMeier method;
        proc lifetest data=save.familyspell_merge
        intervals=1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
        17 18 19 20 21 22 23 24;
        time duration*end_cens(1);
        where spelnmbr=2;
        run;

        * Duration using newORscc data ~ last observation of first spell using KaplanMeier method;
        proc lifetest data=save.familyspell_merge
        intervals=1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
        17 18 19 20 21 22 23 24;
        time duration*end_cens(1);
        where spelnmbr=2;
        run;

        * AFT;
        proc lifereg noprint data=save.familyspell_merge;
        model duration*end_cens(1)= /dist=lnormal;
        where spelnmbr=2;
        output out=spell_out(keep=q_spellold std_q_spellold _prob_)
        quantiles=0.25 0.5 0.75
        p=q_spellold
        std=std_q_spellold;
        run;
        proc print data=spell_out (obs=100); run;

        * case studies;
        proc print data=familyspell_merge; var studyid_adult sm period spelnmbr duration exit end_cens; where studyid_adult=4747; run;
        proc print data=save.familyspell_merge; var studyid_adult sm spelnmbr duration exit end_cens erdc; where studyid_adult=31513; run;
        proc print data=save.familyspell_merge; var studyid_adult sm spelnmbr duration exit end_cens erdc; where studyid_adult=36657; run;
        proc print data=save.familyspell_merge; var studyid_adult sm spelnmbr duration exit end_cens erdc; where studyid_adult=41925; run;
        proc print data=save.familyspell_merge; var studyid_adult sm spelnmbr duration exit end_cens erdc; where studyid_adult=42463; run;



        Comment

        Working...
        X