I have data of the following form:
| Customer_ID | year_month | Segment |
| 1 | 202201 | A |
| 2 | 202201 | A |
| 3 | 202201 | B |
| 4 | 202201 | C |
| 5 | 202202 | A |
| 6 | 202202 | B |
| 7 | 202202 | B |
| 8 | 202202 | C |
| 9 | 202202 | B |
As you can see, we have different customer_id with different year_month and Segment values. Now I would like to count the number of customers for each unique combination or year_month and Segment.
The result should be as follows:
| Segment | ||||
| yearmonth | A | B | C | Total number |
| 202201 | 2 | 1 | 1 | 4 |
| 202202 | 1 | 3 | 1 | 5 |
Any ideas on how to do this in SAS?
data have;
input Customer_ID $ year_month $ Segment $;
datalines;
1 202201 A
2 202201 A
3 202201 B
4 202201 C
5 202202 A
6 202202 B
7 202202 B
8 202202 C
9 202202 B
;
run;
PROC FREQ data=have;
tables year_month * Segment;
run;
/* end of program */
Koen
UNTESTED CODE
proc freq data=have;
tables year_month*segment;
run;
(Please provide data as working SAS data step code from now on, so I can test the code I write against your data)
data have;
input Customer_ID $ year_month $ Segment $;
datalines;
1 202201 A
2 202201 A
3 202201 B
4 202201 C
5 202202 A
6 202202 B
7 202202 B
8 202202 C
9 202202 B
;
run;
PROC FREQ data=have;
tables year_month * Segment;
run;
/* end of program */
Koen
Hello @SasStatistics , it appears that @sbxkoenk has turned your data into SAS data step code. Good for him. Please do that yourself from now on, please do not provide data any other way.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.