BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lillymaginta
Lapis Lazuli | Level 10

 I have the following data, multiple observation per id. several s_Date, one adm_date per id,

I want to do the following: if hf=1 and the s_date is before adm_date then delete all observation for that id. 

 

 

id       s_date           hf      adm_Date         
1        1/1/2005       1       2/2/2004
1        7/7/2005       0
2        4/3/2004       0
2       2/2/2005        1       6/7/2005
2                       0
3                       0
3       4/4/2006        1     8/8/2005
3

 

1 ACCEPTED SOLUTION

Accepted Solutions
mohamed_zaki
Barite | Level 11
data have;
input id s_date ddmmyy9. hf adm_Date ddmmyy9.;
cards;
1 1/1/2005 1 2/2/2004
1 7/7/2005 0 .
2 4/3/2004 0 .
2 2/2/2005 1 6/7/2005
2 . 0 .
3 . 0 .
3 4/4/2006 1 8/8/2005
;
run;

proc sql ;
create table want as
select *
from have
where id not in (select distinct id from have where s_date < adm_date and hf=1)
;
quit;

View solution in original post

5 REPLIES 5
mohamed_zaki
Barite | Level 11

Is there will always be only one hf=1 per id?

lillymaginta
Lapis Lazuli | Level 10

yes one hf per id

mohamed_zaki
Barite | Level 11
data have;
input id s_date ddmmyy9. hf adm_Date ddmmyy9.;
cards;
1 1/1/2005 1 2/2/2004
1 7/7/2005 0 .
2 4/3/2004 0 .
2 2/2/2005 1 6/7/2005
2 . 0 .
3 . 0 .
3 4/4/2006 1 8/8/2005
;
run;

proc sql ;
create table want as
select *
from have
where id not in (select distinct id from have where s_date < adm_date and hf=1)
;
quit;
stat_sas
Ammonite | Level 13

proc sql;
create table want as
select * from have
group by id
having sum(case when s_date<adm_date and hf=1 then 1 else 0 end)=0;
quit;

lillymaginta
Lapis Lazuli | Level 10
Thank you Mohamed asnd stat_sas both codes worked and produced the same results. I appreciate it

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1221 views
  • 2 likes
  • 3 in conversation