DATA Step, Macro, Functions and more

Counting the number of IDs

Accepted Solution Solved
Reply
Contributor
Posts: 44
Accepted Solution

Counting the number of IDs

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. 

 

IDsales
1car
1truck
1bike
1plane
2bike
2car
2motor
4plane
4bike
4car
4motor

 

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. 

 

NsalesNindividuals
10
20
31
42

 

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


Accepted Solutions
Solution
‎10-01-2016 05:27 PM
Super User
Posts: 17,828

Re: Counting the number of IDs

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;

View solution in original post


All Replies
Solution
‎10-01-2016 05:27 PM
Super User
Posts: 17,828

Re: Counting the number of IDs

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;

Respected Advisor
Posts: 4,648

Re: Counting the number of IDs

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;
PG
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 295 views
  • 3 likes
  • 3 in conversation