Hi all,
i am working with a dataset containing information about pregnancies from 1995.
I need to only select women who had not given birth before entering the cohort, but still include all her pregnancies in the study period.
data have;
id pregnancy_start pregnancy_number data want
1 01APR1995 2 (exclude
2 01APR2008 1 (keep)
2 01APR2012 2 (keep)
3 01Jun1996 3 (exclude)
4 01MAY1997 1 (keep)
data have; input id start :date9. number; format start yymmdd10.; datalines; 1 01APR1995 2 2 01APR2008 1 2 01APR2012 2 3 01Jun1996 3 4 01MAY1997 1 ; proc sql; create table want as select * from have where id in (select id from have where number=1); quit;
use a double do-until to scan each ID for the existence of a first pregnancy:
data have;
input id start :date9. number;
format start yymmdd10.;
datalines;
1 01APR1995 2
2 01APR2008 1
2 01APR2012 2
3 01Jun1996 3
4 01MAY1997 1
;
data want;
do until(last.id);
set have; by id;
if number = 1 then keep = 1;
end;
do until(last.id);
set have; by id;
if keep then output;
end;
drop keep;
run;
proc print data=want; run;
Read in only births in 1995 and later, since you will not use earlier births. If the first such birth for a given id is pregnancy number 1, then keep that id.
data have;
input id start :date9. number;
format start yymmdd10.;
datalines;
1 01APR1995 2
2 01APR2008 1
2 01APR2012 2
3 01Jun1996 3
4 01MAY1997 1
;
data want (drop=_:);
set have (where=(start>='01jan1995'd)) ;
by id;
if first.id then _keep_this_id=(number=1);
retain _keep_this_id;
if _keep_this_id;
run;
This depends, of course, on the data being sorted by ID/NUMBER (or equivalently ID/START).
Editted note: you could relax the sort order condition to require only that, for each ID, data are sorted by NUMBER. But the ID groups do not need to be sorted. Simply replace
by id;
with
by id notsorted;
data have; input id start :date9. number; format start yymmdd10.; datalines; 1 01APR1995 2 2 01APR2008 1 2 01APR2012 2 3 01Jun1996 3 4 01MAY1997 1 ; proc sql; create table want as select * from have where id in (select id from have where number=1); quit;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: