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

Hi Everyone, I would like to delete the information if the patient take >=1 drugs in the same day.  Bascially, if the patient take two or drugs in the same day, then delete all the information from this date.

orginal data:

ID

Date

Drug

101

10/1/2019

A

101

10/1/2019

B

101

10/1/2019

C

101

10/2/2019

A

101

10/3/2019

A

101

10/3/2019

B

101

10/3/2019

B

 

I want:

ID

Date

Drug

101

10/2/2019

A

 

Thanks for your help,

Sarah

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

Assuming your input is sorted by ID and DATE (if not, PROC SORT it):

data want;
  set have;
  by ID Date;
  if first.date and last.date;
run;

 

View solution in original post

5 REPLIES 5
mklangley
Lapis Lazuli | Level 10

Here's one way to do it:

data have;
    input ID Date: mmddyy10. Drug $;
    format Date mmddyy10.;
    datalines;
    101 10/1/2019 A
    101 10/1/2019 B
    101 10/1/2019 C
    101 10/2/2019 A
    101 10/3/2019 A
    101 10/3/2019 B
    101 10/3/2019 B 
    ;
run;

/* Find which dates have only one drug */
proc sql;
    create table date_count as
    select date, count(distinct drug) as count
    from have
    group by date
    having count(distinct drug) = 1;
quit;

/* Select only those dates from the original data */
proc sql;
    create table want as
    select a.id, a.date, a.drug
    from have a
    inner join date_count b 
        on a.date = b.date;
quit;
Kurt_Bremser
Super User

Since SAS allows a remerge, you can do it in one select:

proc sql;
create table want as
  select *
  from have a
  group by id, date
  having count(distinct drug) = 1  
;
quit;
Jagadishkatam
Amethyst | Level 16

please try this untested code

 

proc sql;
create table want as select *, count(id) as cnt from have group by id, date having cnt<=1;
quit;
Thanks,
Jag
novinosrin
Tourmaline | Level 20

Hi @lulu3  A sorted dataset (as your sample suggests) may benefit from a one pass

 


data have;
    input ID Date: mmddyy10. Drug $;
    format Date mmddyy10.;
    datalines;
    101 10/1/2019 A
    101 10/1/2019 B
    101 10/1/2019 C
    101 10/2/2019 A
    101 10/3/2019 A
    101 10/3/2019 B
    101 10/3/2019 B 
    ;
run;

data want;
 do _n_=1 by 1 until(last.date);
  do until(last.drug);
   set have;
   by id date drug;
  end;
 end;
 if _n_=1 then output;
run;
s_lassen
Meteorite | Level 14

Assuming your input is sorted by ID and DATE (if not, PROC SORT it):

data want;
  set have;
  by ID Date;
  if first.date and last.date;
run;

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 695 views
  • 4 likes
  • 6 in conversation