BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lone0708
Fluorite | Level 6

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)

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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;

View solution in original post

3 REPLIES 3
PGStats
Opal | Level 21

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;

PGStats_0-1647997576409.png

 

PG
mkeintz
PROC Star

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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Ksharp
Super User
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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 956 views
  • 0 likes
  • 4 in conversation