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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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