Example:
customer_id | movie |
12345 | Rocky |
12345 | Jaws |
12345 | Titanic |
98765 | Jurassic Park |
75319 | Titanic |
75319 | The Dark Knight |
35712 | Legally Blonde |
There are 4 distinct customer_ids in this table. How can I get the desired output below?
# of movies | # of customers |
1 | 2 |
2 | 1 |
3 | 1 |
Where I have the number of movies in one column, and the number of customers with corresponding number of movies in the second column.
Thanks!
@iced_tea wrote:
Example:
customer_id movie 12345 Rocky 12345 Jaws 12345 Titanic 98765 Jurassic Park 75319 Titanic 75319 The Dark Knight 35712 Legally Blonde There are 4 distinct customer_ids in this table. How can I get the desired output below?
# of movies # of customers 1 2 2 1 3 1
Where I have the number of movies in one column, and the number of customers with corresponding number of movies in the second column.
Thanks!
One way:
proc freq data=have noprint; table customer_id/out=temp (rename=(count=NumMovies)); run; proc freq data=temp noprint; table nummovies /out= want(rename=(count=numcustomers) drop=percent); run;
The first proc freq counts how many movies each customer sees. Then you count how many of the counted movies seen tells how many customers see how many movies.
The rename is to get variables close to yours which are not standard variable names. Assign labels if you want different text to appear in reports.
@iced_tea wrote:
Example:
customer_id movie 12345 Rocky 12345 Jaws 12345 Titanic 98765 Jurassic Park 75319 Titanic 75319 The Dark Knight 35712 Legally Blonde There are 4 distinct customer_ids in this table. How can I get the desired output below?
# of movies # of customers 1 2 2 1 3 1
Where I have the number of movies in one column, and the number of customers with corresponding number of movies in the second column.
Thanks!
One way:
proc freq data=have noprint; table customer_id/out=temp (rename=(count=NumMovies)); run; proc freq data=temp noprint; table nummovies /out= want(rename=(count=numcustomers) drop=percent); run;
The first proc freq counts how many movies each customer sees. Then you count how many of the counted movies seen tells how many customers see how many movies.
The rename is to get variables close to yours which are not standard variable names. Assign labels if you want different text to appear in reports.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.