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

Hello everyone, I have a large dataset that looks like this:

 

data have;
input customerID product;
datalines;
A1 P4
A1 P6
A1 P2
A2 P3
A2 P4
A3 P1
;

 

I want to get a count of how many products each customerID has assigned to it. Each row is unique so there is no need to account for duplicates.

This is what I'm trying to achieve:

 

data want;

input customerID product count;
datalines;
A1 P4 3
A1 P6 3
A1 P2 3
A2 P3 2
A2 P4 2
A3 P1 1
;

 

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

If the data are sorted by customerid:

 

data want;
  set have (in=firstpass) have (in=secondpass);
  by customerid;
  if first.customerid then count=0;
  count + firstpass;
  if secondpass;
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

2 REPLIES 2
r_behata
Barite | Level 11
data have;
input customerID $ product $;
datalines;
A1 P4
A1 P6
A1 P2
A2 P3
A2 P4
A3 P1
;
run;

proc SQL;
Create table want as
select customerID,
	   product,
	   count(1) as count
from have
group by customerID;
Quit;
mkeintz
PROC Star

If the data are sorted by customerid:

 

data want;
  set have (in=firstpass) have (in=secondpass);
  by customerid;
  if first.customerid then count=0;
  count + firstpass;
  if secondpass;
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

SAS Innovate 2025: Register Today!

 

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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