BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
buddha_d
Pyrite | Level 9
data have;
input USERID $4. CARDID :$10. TRANCODE $4.;
cards;
1234 1234567890 3503
1234 1234567890 2013
1234 1234567890 7113
5678 2345678901 2013
5678 2345678901 7113
8907 3456789012 3503
8907 3456789012 2013
7658 4567890123 7113
7658 4567890123 7114
4356 5678901234 3503
4356 5678901234 2013
4356 5678901234 7113
9874 6789012345 3503
9874 6789012345 7113
5437 7890123456 3503
5461 8901234567 7113
5462 9012345678 2013
2543 0123456789 2013
2543 0123456789 7113
2543 0123456789 3503
;
run;

I have huge dataset and the above data is dummy to replicate that dataset. 

I have a dataset which has card userid, cardid and transaction code. I have the following conditions to filter the data. 

1. I want to get the records that have all userid and cardids with the trancodes of '3503' along with '2013' and '7113'

2. I don't need to get any records associated with just ('3503' and '2013') or ('2013' and '7113') or ('3503' and '7113') or individual tran codes

3. I have to have '3503' first and along with that I need to have trancodes of '2013' and '7113'

 

I need to see the output result like this: 

 

USERID CARDID TRANCODE
1234 1234567890 3503
1234 1234567890 2013
1234 1234567890 7113
4356 5678901234 3503
4356 5678901234 2013
4356 5678901234 7113
2543 0123456789 2013
2543 0123456789 7113
2543 0123456789 3503

 

I tried this code and wasn't successful

PROC SQL;
CREATE TABLE WANT AS 
SELECT * 
FROM HAVE
WHERE TRANCODE IN ('3503','2013','7113')
;
QUIT;

Please help me. Thanks in advance

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

If I understood what you mean.

 

data have;
input USERID $4. CARDID :$10. TRANCODE $4.;
cards;
1234 1234567890 3503
1234 1234567890 2013
1234 1234567890 7113
5678 2345678901 2013
5678 2345678901 7113
8907 3456789012 3503
8907 3456789012 2013
7658 4567890123 7113
7658 4567890123 7114
4356 5678901234 3503
4356 5678901234 2013
4356 5678901234 7113
9874 6789012345 3503
9874 6789012345 7113
5437 7890123456 3503
5461 8901234567 7113
5462 9012345678 2013
2543 0123456789 2013
2543 0123456789 7113
2543 0123456789 3503
;
run;
proc sql;
create table want as
select * from have
 group by USERID,CARDID
  having sum(TRANCODE='3503') and sum(TRANCODE='2013') and sum(TRANCODE='7113')
;
quit;

View solution in original post

4 REPLIES 4
Ksharp
Super User

If I understood what you mean.

 

data have;
input USERID $4. CARDID :$10. TRANCODE $4.;
cards;
1234 1234567890 3503
1234 1234567890 2013
1234 1234567890 7113
5678 2345678901 2013
5678 2345678901 7113
8907 3456789012 3503
8907 3456789012 2013
7658 4567890123 7113
7658 4567890123 7114
4356 5678901234 3503
4356 5678901234 2013
4356 5678901234 7113
9874 6789012345 3503
9874 6789012345 7113
5437 7890123456 3503
5461 8901234567 7113
5462 9012345678 2013
2543 0123456789 2013
2543 0123456789 7113
2543 0123456789 3503
;
run;
proc sql;
create table want as
select * from have
 group by USERID,CARDID
  having sum(TRANCODE='3503') and sum(TRANCODE='2013') and sum(TRANCODE='7113')
;
quit;
Kurt_Bremser
Super User

Otherwise, this is how you solve such things with a double DO in a DATA step:

data have;
input USERID $4. CARDID :$10. TRANCODE $4.;
cards;
1234 1234567890 3503
1234 1234567890 2013
1234 1234567890 7113
5678 2345678901 2013
5678 2345678901 7113
8907 3456789012 3503
8907 3456789012 2013
7658 4567890123 7113
7658 4567890123 7114
4356 5678901234 3503
4356 5678901234 2013
4356 5678901234 7113
9874 6789012345 3503
9874 6789012345 7113
5437 7890123456 3503
5461 8901234567 7113
5462 9012345678 2013
2543 0123456789 2013
2543 0123456789 7113
2543 0123456789 3503
;

proc sort data=have;
by userid cardid;
run;

data want;
do until (last.cardid);
  set have (where=(trancode in ("3503","2013","7113")));
  by userid cardid;
  if first.cardid and trancode = "3503" then _3503 = 1;
  if trancode = "2013" then _2013 = 1;
  if trancode = "7113" then _7113 = 1;
end;
do until (last.cardid);
  set have (where=(trancode in ("3503","2013","7113")));
  by userid cardid;
  if _3503 and _2013 and _7113 then output;
end;
drop _:;
run;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 650 views
  • 1 like
  • 3 in conversation