BookmarkSubscribeRSS Feed
Giraffe123
Fluorite | Level 6

Hi everyone, I hope you can assist me with the following question:

 

I want to roll up as data according to customer id and month. My data look like this:

Customer_idMonthProduct

1

Jan2010

Savings

1Jan2010Cheque
1Jan2010Savings
1Jan2010Morgage
1Jan2010Savings
2Jan2010Cheque
2Jan2010Cheque
1Feb2010Savings
1Feb2010Morgage
1Feb2010Savings
2Feb2010Cheque
2Feb2010Cheque
2Feb2010Cheque
2Feb2010Morgage
1Apr2010Morgage
1Apr2010Savings
2Apr2010Savings

 

I would like output that counts the number of different products per customer per month, like this:

Customer_idMonthnum_chequenum_savingsnum_morgage
1Jan2010131
2Jan2010200
1Feb2010021
2Feb2010301
1Apr2010011
2Apr2010010

 

3 REPLIES 3
maguiremq
SAS Super FREQ

You can coerce all the missing values to zero. This should do it, though.

 

proc sort data = have;
	by customer_id month;
run;

proc freq data = have noprint;
	by customer_id month;
	table product / out = have_sum;
run;

proc transpose data = have_sum out = want (drop = _:) prefix = num_;
	by customer_id month;
	id product;
	var count;
run;
Kurt_Bremser
Super User

PROC REPORT does this:

data have;
input Customer_id $ Month :monyy7. Product $;
format month yymmd7.;
datalines;
1 Jan2010 Savings
1 Jan2010 Cheque
1 Jan2010 Savings
1 Jan2010 Morgage
1 Jan2010 Savings
2 Jan2010 Cheque
2 Jan2010 Cheque
1 Feb2010 Savings
1 Feb2010 Morgage
1 Feb2010 Savings
2 Feb2010 Cheque
2 Feb2010 Cheque
2 Feb2010 Cheque
2 Feb2010 Morgage
1 Apr2010 Morgage
1 Apr2010 Savings
2 Apr2010 Savings
;

options missing = 0;

proc report data=have;
column month customer_id product;
define month / group;
define customer_id / group;
define product / across;
run;
Ksharp
Super User
data have;
input Customer_id $ Month :monyy7. Product $;
format month yymmd7.;
datalines;
1 Jan2010 Savings
1 Jan2010 Cheque
1 Jan2010 Savings
1 Jan2010 Morgage
1 Jan2010 Savings
2 Jan2010 Cheque
2 Jan2010 Cheque
1 Feb2010 Savings
1 Feb2010 Morgage
1 Feb2010 Savings
2 Feb2010 Cheque
2 Feb2010 Cheque
2 Feb2010 Cheque
2 Feb2010 Morgage
1 Apr2010 Morgage
1 Apr2010 Savings
2 Apr2010 Savings
;
proc sql;
create table want as
select  Customer_id , Month ,
sum(Product = 'Cheque') as Cheque,
sum(Product = 'Savings') as Savings,
sum(Product = 'Morgage') as Morgage

 from have
  group by  Customer_id , Month 
   order by 2,1;
quit;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 560 views
  • 1 like
  • 4 in conversation