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
data test;
input id$2. start  end drug ;
attrib start format =date9. informat=date9.;
attrib end format =date9. informat=date9.;
datalines;
1 01JAN2015 14FEB2015 0
1 18FEB2015 30APR2015 1
2 01jan2015 28JAN2015  1
2 01apr2015 30apr2015   1
3 01JAN2015 14FEB2015 1
3 01JAN2015 14FEB2015 0
;
run;

 I have the following data. I want to flag the first date, keep the id, drug. If the id have both drug 0 and drug 1 on the same date then delete.

Output  data 

1 01JAN2015 14FEB2015 0
2 01jan2015 28JAN2015  1

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

HI @lillymaginta  Your sample is all "sets of 2 records for each id", is it really a representative sample. Well, assuming I understand your requirement, here is a SQL solution

 

data test;
input id$2. start  end drug ;
attrib start format =date9. informat=date9.;
attrib end format =date9. informat=date9.;
datalines;
1 01JAN2015 14FEB2015 0
1 18FEB2015 30APR2015 1
2 01jan2015 28JAN2015  1
2 01apr2015 30apr2015   1
3 01JAN2015 14FEB2015 1
3 01JAN2015 14FEB2015 0
;
run;

proc sql;
create table want as
select *
from 
(select * from test group by id,start,end having not(count(*)>1 and sum(drug)))
group by id
having min(start)=start;
quit;

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20

HI @lillymaginta  Your sample is all "sets of 2 records for each id", is it really a representative sample. Well, assuming I understand your requirement, here is a SQL solution

 

data test;
input id$2. start  end drug ;
attrib start format =date9. informat=date9.;
attrib end format =date9. informat=date9.;
datalines;
1 01JAN2015 14FEB2015 0
1 18FEB2015 30APR2015 1
2 01jan2015 28JAN2015  1
2 01apr2015 30apr2015   1
3 01JAN2015 14FEB2015 1
3 01JAN2015 14FEB2015 0
;
run;

proc sql;
create table want as
select *
from 
(select * from test group by id,start,end having not(count(*)>1 and sum(drug)))
group by id
having min(start)=start;
quit;
lillymaginta
Lapis Lazuli | Level 10

Thank you! 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 503 views
  • 2 likes
  • 2 in conversation