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;
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
  • 3 replies
  • 1291 views
  • 1 like
  • 4 in conversation