英文:
SAS PROC REPORT column percentage subtotals calculating incorrectly
问题
I'm working on a SAS report using PROC REPORT but having challenges getting the subtotal percentages to calculate correctly. Below is some code I've been working with.
option missing = "-";
ods escapechar='\';
ods tagsets.excelxp
options (orientation='portrait' autofit_height='yes' embedded_titles='yes' embedded_footnotes='yes' sheet_name="TABLE1");
proc report data=TABLE1 split='^' headskip nowd
style(header)=[color=black backgroundcolor=lightblue just=center fontfamily=calibri fontweight=bold fontsize=3.5];
column stratify_var stratify_var_categ cnt0 cnt1 pct1 cnt2 pct2;
define stratify_var / group order=data left " " style(column)=[cellwidth=3.5in font_face=Calibri font_size=3 font_weight=bold just=l];
define stratify_var_categ / display left " " style(column)=[cellwidth=3in font_face=Calibri font_size=3 font_weight=light just=l];
define cnt0 / analysis sum "Total living in City X" style(column)=[tagattr='format:#,###' cellwidth=1in font_face=Calibri font_size=2.5 font_weight=light just=r];
define cnt1 / analysis sum "Number of ER visits in 2018" style(column)=[tagattr='format:#,###' cellwidth=1in font_face=Calibri font_size=2.5 font_weight=light just=r];
define pct1 / analysis sum "Percent with ER visit in 2018" style(column)=[tagattr='format:##0.0%' cellwidth=1in font_face=Calibri font_size=2.5 font_weight=light just=r];
define cnt2 / analysis sum "Number of ER visits in 2019" style(column)=[tagattr='format:#,###' cellwidth=1in font_face=Calibri font_size=2.5 font_weight=light just=r];
define pct2 / analysis sum "Percent with ER visit in 2019" style(column)=[tagattr='format:##0.0%' cellwidth=1in font_face=Calibri font_size=2.5 font_weight=light just=r];
define Count / analysis sum "Total" style(column)=[tagattr="format:#,###" cellwidth=1in font_face=Calibri font_size=2.5 font_weight=light just=r];
break after stratify_var / ol skip summarize suppress style=[backgroundcolor=lightblue font_face=Calibri font_size=2.5 font_weight=bold just=r];
compute after stratify_var;
stratify_var = 'Sub-total';
endcomp;
title1 bold f="Calibri" h=13pt j=c "Table 1 test report";
footnote1 f="Calibri" h=10pt j=l "Table 1 test footnote";
run;
I got the table in the screen capture below (re-created in Excel for reasons I won't bore anyone with, so not perfectly matching the color and fonts in the code above). I expected to get the subtotals within the columns "Percent with ER visits in 2018" and "Percent with ER visits in 2019" to calculate as the value for the Gender, Age Group, and Income to show as Number of ER visits in 2018/Total Living in City X for the group, and not the sum of the percents. I have experimented with changing the definitions of the pct1 and pct2 to computed columns but have ended up either with errors or with no percentages calculating in the columns at all. Any help would be appreciated. Thanks so much. [Output of the above SAS code] 1
英文:
I'm working on a SAS report using PROC REPORT but having challenges getting the subtotal percentages to calculate correctly. Below is some code I've been working with.
option missing = "-";
ods escapechar='\';
ods tagsets.excelxp
options (orientation='portrait' autofit_height='yes' embedded_titles='yes' embedded_footnotes='yes' sheet_name="TABLE1");
proc report data=TABLE1 split='^' headskip nowd
style(header)=[color=black backgroundcolor=lightblue just=center fontfamily=calibri fontweight=bold fontsize=3.5];
column stratify_var stratify_var_categ cnt0 cnt1 pct1 cnt2 pct2;
define stratify_var / group order=data left " " style(column)=[cellwidth=3.5in font_face=Calibri font_size=3 font_weight=bold just=l];
define stratify_var_categ / display left " " style(column)=[cellwidth=3in font_face=Calibri font_size=3 font_weight=light just=l];
define cnt0 / analysis sum "Total living in City X" style(column)=[tagattr='format:#,###' cellwidth=1in font_face=Calibri font_size=2.5 font_weight=light just=r];
define cnt1 / analysis sum "Number of ER visits in 2018" style(column)=[tagattr='format:#,###' cellwidth=1in font_face=Calibri font_size=2.5 font_weight=light just=r];
define pct1 / analysis sum "Percent with ER visit in 2018" style(column)=[tagattr='format:##0.0%' cellwidth=1in font_face=Calibri font_size=2.5 font_weight=light just=r];
define cnt2 / analysis sum "Number of ER visits in 2019" style(column)=[tagattr='format:#,###' cellwidth=1in font_face=Calibri font_size=2.5 font_weight=light just=r];
define pct2 / analysis sum "Percent with ER visit in 2019" style(column)=[tagattr='format:##0.0%' cellwidth=1in font_face=Calibri font_size=2.5 font_weight=light just=r];
define Count / analysis sum "Total" style(column)=[tagattr="format:#,###" cellwidth=1in font_face=Calibri font_size=2.5 font_weight=light just=r];
break after stratify_var / ol skip summarize suppress style=[backgroundcolor=lightblue font_face=Calibri font_size=2.5 font_weight=bold just=r];
compute after stratify_var;
stratify_var = 'Sub-total';
endcomp;
title1 bold f="Calibri" h=13pt j=c "Table 1 test report";
footnote1 f="Calibri" h=10pt j=l "Table 1 test footnote";
run;
I got the table in the screen capture below (re-created in Excel for reasons I won't bore anyone with, so not perfectly matching the color and fonts in the code above). I expected to get the subtotals within the columns "Percent with ER visits in 2018" and "Percent with ER visits in 2019" to calculate as the value for the Gender, Age Group, and Income to show as Number of ER visits in 2018/Total Living in City X for the group, and not the sum of the percents. I have experimented with changing the definitions of the pct1 and pct2 to computed columns but have ended up either with errors or with no percentages calculating in the columns at all. Any help would be appreciated. Thanks so much.Output of the above SAS code
答案1
得分: 1
你不能总结预先计算的百分比,你需要使用计算块。
例如:
define pct1 / computed format=percent9.2 ;
compute pct1 ;
if cnt0.sum then pct1 = cnt1.sum / cnt0.sum ;
endcomp ;
这只适用于小计;如果你有一个总计,它会由于你对数据分组的方式而三重计算你的值。在这里,使用PROC TABULATE会更好。
英文:
You can't sum up pre-calculated percentages, you would need to use a compute block.
For example :
<pre>
define pct1 / computed format=percent9.2 ;
compute pct1 ;
if cnt0.sum then pct1 = cnt1.sum / cnt0.sum ;
endcomp ;
</pre>
This will work for the sub-totals only; if you had a grand total it would triple-count your volumes due to the way you're grouping the data. PROC TABULATE would be a better option here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论