I need to Convert the following SAS code to STATA code. I'm hoping someone on this forum will be able to do this relatively easily. I never quite got the hang of do loops in SAS and i am very new to STATA.
* Creating spells and startm and endm (start month and end month);
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;
* Creating spells and startm and endm (start month and end month);
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;