Hello Everyone
Probably another simple question but I am stuck.
I have a data set that looks something like the following:
id | fabric color | location | price | fiveormore |
1 | blue | store | $ 4.99 | no |
2 | black | warehouse | $ 6.00 | yes |
3 | red | warehouse | $ 2.00 | no |
4 | blue | store | $ 10.00 | yes |
5 | black | store | $ 7.50 | yes |
6 | red | warehouse | $ 10.00 | yes |
7 | blue | store | $ 5.00 | yes |
8 | black | warehouse | $ 3.00 | no |
9 | red | store | $ 1.00 | no |
10 | blue | store | $ 5.00 | yes |
What I am trying to tabulate the count of each color that are say $5.00 or more per location which I can do pretty easily but I need another column that is the percentage of the grand total of the color regardless of location.
Something like:
Store | Warehouse | |||
color | count | % | count | % |
Blue | 3 | 75.0 | 0 | 0 |
Black | 1 | 33.3 | 1 | 33.3 |
Red | 0 | 0 | 1 | 33.3 |
I have done something like below but, obviously, it is not really giving me what I want.
PROC FORMAT;
picture pctf (round) other='09.9';
RUN;
proc tabulate
data = birth.lowbirthweight17;
class color location;
table county, location*(n=' ' pctn<color>*f=pctf.);
where fiveormore = 'Yes';
run;
I just can't quite figure out how to do it. Any help would be greatly appreciated.
Here is some UNTESTED code for you, since we can't work from screen captures (if you want tested code, we'd need the actual data as a SAS data step). If you make a binary variable that has numeric values 0 and 1 (instead of character values yes and no) then the math becomes much simpler. You probably don't even need PROC SUMMARY here, you could probably do it all in PROC REPORT, but that's the way I do it.
data have2;
set have(drop=fiveormore);
above5=(price>=5);
run;
proc summary nway data=have2;
class fabric_color location;
var above5;
output out=stats sum=count mean=percent;
run;
proc report data=stats;
columns fabric_color location,(count percent);
define fabric_color/group;
define location/across;
define count/sum;
define percent/sum;
run;
What is the denominator of the percent? How do you get 75% for the blue/store combination?
In the table there are total of 4 units of blue.
Here is some UNTESTED code for you, since we can't work from screen captures (if you want tested code, we'd need the actual data as a SAS data step). If you make a binary variable that has numeric values 0 and 1 (instead of character values yes and no) then the math becomes much simpler. You probably don't even need PROC SUMMARY here, you could probably do it all in PROC REPORT, but that's the way I do it.
data have2;
set have(drop=fiveormore);
above5=(price>=5);
run;
proc summary nway data=have2;
class fabric_color location;
var above5;
output out=stats sum=count mean=percent;
run;
proc report data=stats;
columns fabric_color location,(count percent);
define fabric_color/group;
define location/across;
define count/sum;
define percent/sum;
run;
I will give that a try. Thanks for your time and your assistance.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.