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

Hi SAS community...

Long time lurker but new user.

 

So, I have a question that I am stumped with.

  1. I have 1 data set with all my data. There are multiple rows with each user and each user may have multiple dates. For example, there can be multiple rows for the same person on the same date and then additional rows for more dates and so on...
  2. Now, I want to delete dates where the claim is 0 and keep those dates where the claim is 1. So, all claims on that date where the claim exists will be kept and all dates where claim is 0 will be removed. For example, the claim is readmission. So, for example, for one date, the person may have 5 claims but one claim=1 so I want to keep all those claims on that date. 
  3. I created a separate variable 'readmission_claim=1' if the claim was readmission. The other claims are all 0. 
  4. t is my data set

Here is code I have but it's not removing dates that have 0 claim. 

 

proc sql;
create table output as select * from input t
where exists (select * from input t where t.person=t.person and t.date=t.date and t.readmission_claim=1); 
quit;
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

So I made up some quick-and-dirty test data, and ran my SQL against it. See the result.

data input;
input person date :yymmdd8. readmission_claim;
format date yymmddd10.;
cards;
1 20180305 0
1 20180305 1
1 20180306 0
1 20180306 0
2 20180305 0
2 20180305 0
2 20180306 0
2 20180306 0
;
run;

proc sql;
create table output as
select *
from input t
group by t.person, t.date
having max(t.readmission_claim) = 1
;
quit;

proc print data=output noobs;
run;
                        readmission_
person          date        claim

   1      2018-03-05          1     
   1      2018-03-05          0     

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

Please supply some example data in a data step so we can test code against it.

Try a having clause:

proc sql;
create table output as
select *
from input t
group by t.person, t.date
having max(t.readmission_claim) = 1
;
quit;
s_lassen
Meteorite | Level 14

The problem seems to be that you are using the same alias inside and outside your EXISTS subquery. Try removing the t alias from the inner query table:

proc sql;
create table output as select * from input t
where exists (select * from input where person=t.person and date=t.date and readmission_claim=1); 
quit;

In your original query, all rows will be included if there is a single row in the whole table having readmission_claim=1, otherwise no rows will be selected.

A_Swoosh
Quartz | Level 8

Thank you for noticing the error. This fixed it. 

Kurt_Bremser
Super User

So I made up some quick-and-dirty test data, and ran my SQL against it. See the result.

data input;
input person date :yymmdd8. readmission_claim;
format date yymmddd10.;
cards;
1 20180305 0
1 20180305 1
1 20180306 0
1 20180306 0
2 20180305 0
2 20180305 0
2 20180306 0
2 20180306 0
;
run;

proc sql;
create table output as
select *
from input t
group by t.person, t.date
having max(t.readmission_claim) = 1
;
quit;

proc print data=output noobs;
run;
                        readmission_
person          date        claim

   1      2018-03-05          1     
   1      2018-03-05          0     
A_Swoosh
Quartz | Level 8
That worked! Thanks for helping a SAS noob

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 880 views
  • 1 like
  • 3 in conversation