## compute the percentages in proc report

Solved
Regular Contributor
Posts: 165

# compute the percentages in proc report

I'm running proc report using this code and that's what I got. I'm not able to compute the right percentages.

Please advise me how I can enhance my code  to get the aimed result

proc report data=QUERIES3;
column SITE_NUMBER numsites percentages;
define SITE_NUMBER/ "Top 3 Sites with highest # issues";
define numsites/ analysis sum "# Open Issues";
define percentages/ computed format=percent9.3;

COMPUTE percentages;
percentages=(numsites/numsites.sum);
endcomp;
rbreak after/ summarize ;
run;

 Top 3 Sites with highest # issues # Open Issues percentages 1350 7 . 1601 7 . 1450 6 . 20 .

and that the aimed ressult

 Top 3 Sites with highest # issues # Open Issues percentages 1350 7 35% 1601 7 35% 1450 6 30% 20 100%

Accepted Solutions
Solution
‎02-22-2018 11:32 AM
SAS Super FREQ
Posts: 9,433

## Re: compute the percentages in proc report

[ Edited ]

Hi: You didn't post all of your code (such as your ODS statements) or any data for people to test with. You refer to numsites correctly as numsites.sum in one place in your compute block, but not in the other.But without seeing your data, it's hard to decide what to recommend.

Is SITE_NUMBER character? If so, it has a default usage of DISPLAY. If it is numeric, then it has a default usage of SUM.

Here's an example using FAKE data of how you need to capture the grand total for NUMSITES into a temporary variable before you start writing report rows. Then you use that temporary variable to generate the calculated percentage. I will post a code example using fake data.

``````title; footnote;
data fakedata;
infile datalines;
input site \$ numsites;
return;
datalines;
aaa 7
bbb 7
ccc 6
;
run;

proc report data=fakedata;
column site numsites calcpct;
define site / display;
define numsites / sum;
define calcpct / computed 'Percent' f=percent9.2;
rbreak after / summarize;
compute before;
holdtot = numsites.sum;
endcomp;
compute calcpct;
calcpct = numsites.sum / holdtot;
endcomp;
run;``````

cynthia

All Replies
Posts: 3,277

## Re: compute the percentages in proc report

PROC FREQ makes it simple.

``````proc freq data=have;
tables site_number;
run;``````
--
Paige Miller
Regular Contributor
Posts: 165

## Re: compute the percentages in proc report

It wouldn't give me the result I want

Posts: 3,277

## Re: compute the percentages in proc report

mona4u wrote:

It wouldn't give me the result I want

I can't help you if that's all the information you are going to provide.

--
Paige Miller
Solution
‎02-22-2018 11:32 AM
SAS Super FREQ
Posts: 9,433

## Re: compute the percentages in proc report

[ Edited ]

Hi: You didn't post all of your code (such as your ODS statements) or any data for people to test with. You refer to numsites correctly as numsites.sum in one place in your compute block, but not in the other.But without seeing your data, it's hard to decide what to recommend.

Is SITE_NUMBER character? If so, it has a default usage of DISPLAY. If it is numeric, then it has a default usage of SUM.

Here's an example using FAKE data of how you need to capture the grand total for NUMSITES into a temporary variable before you start writing report rows. Then you use that temporary variable to generate the calculated percentage. I will post a code example using fake data.

``````title; footnote;
data fakedata;
infile datalines;
input site \$ numsites;
return;
datalines;
aaa 7
bbb 7
ccc 6
;
run;

proc report data=fakedata;
column site numsites calcpct;
define site / display;
define numsites / sum;
define calcpct / computed 'Percent' f=percent9.2;
rbreak after / summarize;
compute before;
holdtot = numsites.sum;
endcomp;
compute calcpct;
calcpct = numsites.sum / holdtot;
endcomp;
run;``````

cynthia

Regular Contributor
Posts: 165

## Re: compute the percentages in proc report

I have another question I just want to add the word total to the end

can you show me how I can do it.

sorry about that but I'm really new to proc report

 Top 3 Sites with highest # issues # Open Issues percentages 1350 7 35% 1601 7 35% 1450 6 30% total 20 100%
SAS Super FREQ
Posts: 9,433

## Re: compute the percentages in proc report

Hi:

You need a COMPUTE AFTER block, the method that I show (a simple assignment statement) will only work if the LENGTH of SITE is \$5 or bigger. The changes to the previous program are highlighted in yellow:

cynthia

Regular Contributor
Posts: 165