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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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