Hello all,
I have a dataset with hundreds of observations. I wanted to know how many individual IDs had more than1 or 2 or 3.... sales. Here is my sample dataset.
ID | sales |
1 | car |
1 | truck |
1 | bike |
1 | plane |
2 | bike |
2 | car |
2 | motor |
4 | plane |
4 | bike |
4 | car |
4 | motor |
In the above dataset each Individual ID had either 4 sales (For 1st ID), 3 sales (For 2nd ID) or 4 sales (For 4th ID). I wanted to know how many individuals had 4 sale count and three sale count. My output should look something like this.
Nsales | Nindividuals |
1 | 0 |
2 | 0 |
3 | 1 |
4 | 2 |
There were no Subject IDs with 1 or 2 sales count. So the first two rows are zero. There was 1 subject ID (ID-2) with 3 sales and 2 subject IDs (ID-1 and 4) with 4 sales.
Thank you
Double proc freq.
1. First one gets you a count by Person and Sales (table id).
2. Second one gets you how many of each types of sales and then you can filter out what you need.
You won't get 0's though, only missing so you'll need fo find a way to add them in, there's several.
proc freq data=sashelp.cars noprint;
table make/out=countID;
run;
proc freq data=countID;
table count/out=Want;
run;
Double proc freq.
1. First one gets you a count by Person and Sales (table id).
2. Second one gets you how many of each types of sales and then you can filter out what you need.
You won't get 0's though, only missing so you'll need fo find a way to add them in, there's several.
proc freq data=sashelp.cars noprint;
table make/out=countID;
run;
proc freq data=countID;
table count/out=Want;
run;
Or use a SQL query:
proc sql;
create table salesCount as
select nSales, count(id) as nIndividuals
from (
select id, count(sales) as nSales from have group by id)
group by nSales;
select * from salesCount;
quit;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.