BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Quartz | Level 8

## Perform a ratio by classes

Hi guys,

Suppose to have a dataset:

ID       Class        Place

1            0               1

2           0               1

3           0               1

4           1                2

5           2               2

6           2               2

.....        .....             .....

Then, you have another dataset of which you will take only the total number of rows, for example 10.

Now you want to divide the number of IDs in the first dataset by the total number of IDs in the second dataset, i.e., 10 but you want to do this by "Class" and "Place". How, this, can be achieved? In other words the desired output would be:

 Place Class Count Ratio 1 0 3 0.3 1 1 0 0 1 2 0 0 2 0 0 0 2 1 1 0.1 2 2 2 0.2

As you can see (as an example), for place =1, there are IDs only in Class 0 and they are 3 in total. So 3/10 = 0.3. These are example numbers of the real situation. Totally I have 10 "Classes", 1 "Place" and 19.398 rows in the first dataset and 76.984 rows in the second dataset.

Thank you in advance

1 ACCEPTED SOLUTION

Accepted Solutions
Diamond | Level 26

## Re: Perform a ratio by classes

For problems that involve counting, your first choice ought to be PROC FREQ

``````proc freq data=have;
tables class*place/noprint list sparse out=counts;
run;
data want;
set counts(drop=percent);
ratio=count/10;
run;``````

From now on, please provide data as WORKING data step code, for example:

``````data have;
input ID Class Place;
cards;
1            0               1
2           0               1
3           0               1
4           1                2
5           2               2
6           2               2
;``````

--
Paige Miller
1 REPLY 1
Diamond | Level 26

## Re: Perform a ratio by classes

For problems that involve counting, your first choice ought to be PROC FREQ

``````proc freq data=have;
tables class*place/noprint list sparse out=counts;
run;
data want;
set counts(drop=percent);
ratio=count/10;
run;``````

From now on, please provide data as WORKING data step code, for example:

``````data have;
input ID Class Place;
cards;
1            0               1
2           0               1
3           0               1
4           1                2
5           2               2
6           2               2
;``````

--
Paige Miller
Discussion stats
• 1 reply
• 339 views
• 1 like
• 2 in conversation