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

I have two datasets:

 

data one;

input id acctype$ catalogtype$ amount;

datalines;

1 credit issued 10
1 credit refund 5
1 credit cancelled 20
2 debit issued 20
2 debit refund 20
3 cash issued 20

run;

 

data two;

input id acctype$ catalogtype$ amount;

datalines;

1 credit issued 10
1 credit refund 10
1 credit cancelled 10
2 debit issued 20
2 debit refund 10
4 cash refund 10

run;

 

I want a output that should get matched records from left table, and non matching records from right table like this:

 

1 credit issued 10 matched

1 credit refund 5   not-matched
1 credit cancelled not-matched
2 debit issued 20 matched
2 debit refund 20 not-matched
3 cash issued 20 not-matched

 

I want this specifically using proc sql, please. I tried left outer join and union all of both datasets but not able to get solution. 

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26
proc sql;
   create table WANT as 
  select  A.*,
          case when B.ID is not null then "Matched" else "UnMatched" end as RES
  from    ONE A
  left join TWO B
  on      A.ID=B.ID
  and     A.ACCTYPE=B.ACCTYPE
  and     A.CATALOGTYPE=B.CATALOGTYPE
  and     A.AMOUNT=B.AMOUNT;
quit;

Not tested, but that should work.

View solution in original post

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26
proc sql;
   create table WANT as 
  select  A.*,
          case when B.ID is not null then "Matched" else "UnMatched" end as RES
  from    ONE A
  left join TWO B
  on      A.ID=B.ID
  and     A.ACCTYPE=B.ACCTYPE
  and     A.CATALOGTYPE=B.CATALOGTYPE
  and     A.AMOUNT=B.AMOUNT;
quit;

Not tested, but that should work.

vsharipriya
Fluorite | Level 6

Yes. This worked. I was joining using only id and hence wasnt getting the solution as I wanted. 

Yavuz
Quartz | Level 8
Try this please

proc sql;
create table WANT as
select A.*,
case when B.ID is not null then "Matched" else "UnMatched" end as RES
from ONE A
left join TWO B
on A.ID=B.ID;
quit;
vsharipriya
Fluorite | Level 6
Thanks for the solution. But as i said, joining with just the id did not do my job.Had to join on all the fields like previous solution. Because I want to look at all the fields to match on amount
Yavuz
Quartz | Level 8
Sorry. I suggest that create a new variable as newvar and join them:

Data one1;
Set one;
Newvar=put(id,10.)||acctype||catalogtype||put(amount,2.);

Data two1;
Set two;
Newvar=put(id,10.)||acctype||catalogtype||put(amount,2.);

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6555 views
  • 2 likes
  • 3 in conversation