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

 

Below I create two tables, with the second table a subset of the first. What I'd like is to retain only the observations from the first table where the MemberCTG and HCPCS on a given observation in the first table matches the MemberCTG and HCPCS on an observation on the second table. 

 

My thought was to somehow pass the MemberCTGs from the second table into a macro list and filter the first table on those, then do the same thing with the HCPCs from the second table and filter the first table on that again. But that seems like it ends up being a far too complicated solution for the problem I'm trying to solve.

 

PROC SQL;
   CREATE TABLE cb.LONGTERMOX09252019_Filter1 AS 
   SELECT t1.ClaimID, 
          t1.LineNum, 
          t1.MemberID, 
          t1.MemberCTG, 
          t1.date, 
          t1.HCPCS, 
          t1.billed, 
          t1.allowed, 
          t1.paid, 
          t1.MR_Cases_Admits, 
          t1.MR_Units_Days, 
          t1.MR_Procs
      FROM cb.LongTermOx09252019 t1
      ORDER BY t1.MemberCTG,
               t1.HCPCS,
               t1.date;
QUIT;

data cb.LONGTERMOX09252019_Filter2 (keep=MemberCTG HCPCS Count);
  set cb.LONGTERMOX09252019_Filter1;
  by MemberCTG HCPCS;
  if first.HCPCS then count=0;
  count+1;
  if last.HCPCS and count > 35 then output;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@acemanhattan 

Looking at the code you've posted I believe you could get the desired result directly via a group by/having clause

PROC SQL;
   CREATE TABLE cb.want AS 
   SELECT t1.ClaimID, 
          t1.LineNum, 
          t1.MemberID, 
          t1.MemberCTG, 
          t1.date, 
          t1.HCPCS, 
          t1.billed, 
          t1.allowed, 
          t1.paid, 
          t1.MR_Cases_Admits, 
          t1.MR_Units_Days, 
          t1.MR_Procs
      FROM cb.LongTermOx09252019 t1
      group BY t1.MemberCTG,
               t1.HCPCS
      having count(*) > 35
      ORDER BY t1.MemberCTG,
               t1.HCPCS,
               t1.date;
      ;
QUIT;

View solution in original post

7 REPLIES 7
ChrisNZ
Tourmaline | Level 20

Like this?

proc sql;
  select TABLE1.* 
  from TABLE1 
      ,(select unique MemberCTG, HCPCS from TABLE2)
  where TABLE1.MemberCTG = TABLE2.MemberCTG
    and TABLE1.HCPCS     = TABLE2.HCPCS;

 

acemanhattan
Quartz | Level 8

The output isn't quite what I'm looking for in the sense that it's not a data set I can export and use for modeling.

ChrisNZ
Tourmaline | Level 20

@acemanhattan  I wish there was a SQL keyword to CREATE tables. It would be so nice to be able to CREATE tables. 🙂

sustagens
Pyrite | Level 9

Here is another way of doing it, although it takes two passes at the second table

 

proc sql;
create table want as(
select t1.* 
from cb.LongTermOx09252019 t1 
where t1.MemberCTG in (select distinct MemberCTG from cb.LONGTERMOX09252019_Filter2) and 
t1.HCPCS in (Select distinct HCPCS from cb.LONGTERMOX09252019_Filter2)
);
quit;

 

Patrick
Opal | Level 21

@acemanhattan 

Looking at the code you've posted I believe you could get the desired result directly via a group by/having clause

PROC SQL;
   CREATE TABLE cb.want AS 
   SELECT t1.ClaimID, 
          t1.LineNum, 
          t1.MemberID, 
          t1.MemberCTG, 
          t1.date, 
          t1.HCPCS, 
          t1.billed, 
          t1.allowed, 
          t1.paid, 
          t1.MR_Cases_Admits, 
          t1.MR_Units_Days, 
          t1.MR_Procs
      FROM cb.LongTermOx09252019 t1
      group BY t1.MemberCTG,
               t1.HCPCS
      having count(*) > 35
      ORDER BY t1.MemberCTG,
               t1.HCPCS,
               t1.date;
      ;
QUIT;

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
  • 7 replies
  • 879 views
  • 2 likes
  • 4 in conversation