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

Hello, I need some help with reporting percentages. I have a data set with list of cities and countries and am counting how often a city appears (for example Melbourne, Australia is in 18 rows, per proc report count n). 

 pctn is calculating percent out of all countries? I think.

1. How would I calculate so each country is 100% and the city count percentages will add to 100% per country.  in the snippet below, Austria summary 100%, salzburg 66%/vienna 33%. 

2. What if I wanted to calculate percent out of a completely different number? say "each city count"/1000. Can I define my own denominator in a calculate field?

 

proc report data=city_data;

columns country city (n pctn);

define country /group;

define city /group;

define pctn /format =percent9.1 'Percent';

define n /'count';

rbreak before /summarize;

break before country/summarize style=header;

compute before;
country='All countries';
endcomp;

run;

 

CountriesCitiescountPercent
All Countries 199100.00%
Australia 189.00%
AustraliaMelbourne189.00%
Austria 31.50%
AustriaVienna10.50%
 Salzberg21.00%
Belize 10.50%
BelizeBelmopan10.50%
Bulgaria 31.50%
BulgariaSofia31.50%
Brazil 94.50%
BrazilSao Paolo21.00%
 Rio de Janeiro42.00%
 Brasilia42.00%
 Porto Alegre10.50%

.......

.......

1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
SAS Super FREQ

@PaigeMiller -- I agree with you. If someone is not comfortable with PROC REPORT and using helper variables and COMPUTE blocks, then pre-summarizing and computing the percents in a DATA step or switching to PROC TABULATE (which has custom denominators and also the ROWPCT/COLPCT statistics) is a preferred approach.

  For the sake of completeness, here's an example of how to do it within PROC REPORT using helper variables. I switched to using SASHELP.SHOES because the OP did not provide data. This report has a percent of region and then a fake percent where the denominator was set to a constant value of 3371.

Cynthia_sas_0-1728050946123.png

Ths places where the 2 new percents are calculated are highlighted:

Cynthia_sas_1-1728051011199.png

  Basically, if you want to calculate percent of region with PROC REPORT, you have to "grab" and hold the count for that region into a helper variable called RCNT, which is automatically retained and then use rcnt in the divide for REGPCT. The calculation for FAKEPCT just uses a numeric constant as the denominator.

 

Cynthia

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

My answer to both questions is to do the calculations and get them the way you want them to be before using PROC REPORT. For example, using PROC SUMMARY and/or PROC FREQ and/or DATA steps. If you do that, then PROC REPORT is simple to program. I say this because I feel much more comfortable using those tools to program with, rather than the internal tools in PROC REPORT.

--
Paige Miller
eawh100
Fluorite | Level 6

ok, thanks. I'd just calculate whatever percents I want in a new column in say a data step and then use that new column within proc report?

 

PaigeMiller
Diamond | Level 26

Yes, all the info winds up in a data set, so you know you have done the calculations properly, and then that data set is used in PROC REPORT

--
Paige Miller
Cynthia_sas
SAS Super FREQ

@PaigeMiller -- I agree with you. If someone is not comfortable with PROC REPORT and using helper variables and COMPUTE blocks, then pre-summarizing and computing the percents in a DATA step or switching to PROC TABULATE (which has custom denominators and also the ROWPCT/COLPCT statistics) is a preferred approach.

  For the sake of completeness, here's an example of how to do it within PROC REPORT using helper variables. I switched to using SASHELP.SHOES because the OP did not provide data. This report has a percent of region and then a fake percent where the denominator was set to a constant value of 3371.

Cynthia_sas_0-1728050946123.png

Ths places where the 2 new percents are calculated are highlighted:

Cynthia_sas_1-1728051011199.png

  Basically, if you want to calculate percent of region with PROC REPORT, you have to "grab" and hold the count for that region into a helper variable called RCNT, which is automatically retained and then use rcnt in the divide for REGPCT. The calculation for FAKEPCT just uses a numeric constant as the denominator.

 

Cynthia

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 213 views
  • 5 likes
  • 3 in conversation