options nocenter;
data test;
do Sex = 'M', 'F', 'F';
do i = 1 to 847;
Savings = round(ranuni(12345)*50, .01);
Chgs = savings + round(ranuni(12345)*40, .01) +
(Sex='M')*Savings*.2;
output;
end;
end;
drop i;
format chgs savings comma10.2;
run;
proc format;
value $osex
'M' = 'Male'
'F' = 'Female'
'T' = 'Total'
;
picture tabpct
0-100 = '009.9%'
;
run;
title 'Output from PROC REPORT';
proc report data=test
missing nowindows;
column sex csex savings=nobs savings savings=meansavings chgs
chgs=meanchg SaveRate;
define sex / noprint group width=8;
define csex / computed 'Sex';
define nobs / n format=comma5.0 'N';
define chgs / sum;
define meanchg / mean 'Mean Chrgs';
define savings / sum;
define meansavings / mean 'Mean Savings';
define SaveRate / computed format=percent7.1 'Savings Rate';
compute saverate;
saverate = savings.sum / chgs.sum;
endcomp;
compute csex / character;
if _break_ = '_RBREAK_' then
csex = 'Total';
else
csex = sex;
endcomp;
rbreak after / summarize;
run;
title 'Output from PROC TABULATE';
proc tabulate data=test missing;
class sex;
var chgs savings;
keylabel sum=' ';
keylabel mean=' ';
keylabel rowpctsum='Savings Rate';
tables sex=' ' all='Total',
n*format=comma5.0
savings=' '*(sum='Savings' mean='Mean
Savings')*format=comma10.2
chgs=' '*(sum='Chgs' mean='Mean Chgs')*format=comma10.2
savings=' '*rowpctsum*format=tabpct.
/ box='Sex'
;
run;
The output:
Output from PROC REPORT
| Sex |
N |
Savings |
Mean Savings |
Chgs |
Mean Chrgs |
Savings Rate |
| F |
1,694 |
43,075.18 |
25.43 |
77,354.14 |
45.66 |
55.7% |
| M |
847 |
21,193.54 |
25.02 |
42,496.27 |
50.17 |
49.9% |
| Total |
2,541 |
64,268.72 |
25.29 |
119,850.41 |
47.17 |
53.6% |
Output from PROC TABULATE
| Sex |
N |
Savings |
Mean Savings |
Chgs |
Mean Chgs |
Savings Rate |
| F |
1,694 |
43,075.18 |
25.43 |
77,354.14 |
45.66 |
55.6% |
| M |
847 |
21,193.54 |
25.02 |
42,496.27 |
50.17 |
49.8% |
| Total |
2,541 |
64,268.72 |
25.29 |
119,850.41 |
47.17 |
53.6% |
1 Comments:
Hi Jack
interesting methodology, blogs !
Testing your example in SAS9, I needed a couple of tweaks that may be already posted to sas-l Your rounding in tabulate results differs from proc report, in the final column, so I pasted the code into sas9 and found only 100% under SavingsRate! My solution was to add (round) to the tabpct format definition, and change tabulate definition for SavingsRate, from *rowpctsum*format=tabpct. to
*pctsum<chgs>='Savings Rate'*format=tabpct7.
Peter
Post a Comment
<< Home