Pyrite | Level 9

## proc report calculating percent

G'day.

I'm new to proc report.

I'm trying to calculate percent using proc report.

OUTPUT DESIRED is a line list of each observation (no grouping).

county sitename sitecity total status1 status1pct status2 status2pct

I've so far:

ods html file ' Percent';

proc report = percent nowd;

column county sitename sitecity total status1 status1pct  status2 status2pct;

define pct / computed "Percent of total" f=percent8.2;

compute pct;

status1pct=status1/total;

status2pct=status2/total;

endcomp;

rbreak after/ summarize;

run;

ods html close;

Why am I getting a warning and why are my percentages coming up empty?

WARNING:  pct is not in the report definition

Thanks!

3 REPLIES 3
SAS Super FREQ

## Re: proc report calculating percent

Hi:

I would recommend starting slow and seeing whether the "regular" PCTN and/or PCTSUM statistics will give you want you want without a COMPUTE block. In order to have a report item calculated in a COMPUTE block, you need to include it in the "report definition" -- otherwise known as the list of report items that are contained in the COLUMN statement.

For example, SASHELP.CLASS should be available to you for practice purposes. The code below uses SASHELP.CLASS in 2 different scenarios -- Report #1 lists every observation (all 19 students) and calculates each person's height and weight as a percent of the overall height and weight for the group. It is a silly calculation, I know, but it serves to illustrate the point about PCTSUM and how it can be "requested) for your numeric variables used on the report without needing a COMPUTE block. Then, Report #2 shows you how with a different usage and by taking name and sex from the COLUMN statement, you get a slightly different report that shows percents for the total height and weight for each age group.

Next, you will have to do some reading about PROC REPORT. Your COMPUTE block, where you have this:

compute pct;

status1pct=status1/total;

status2pct=status2/total;

endcomp;

is likely to fail, even if you put PCT into the COLUMN statement. PROC REPORT has certain requirements for how to use report items in a compute block and one of the requirements is that you use a special reference of the form:

variablename.statisticname in a calculation.

Since you did not indicate what was actually in your data or how your calculated PCT item would be different from STATUS1PCT and STATUS2PCT, I made some "fake" data for report #3. My goal was to use numbers that were fairly easy to figure out how the division was working (so the TOTAL added up to 1000, across all obs) and each county/site observation had a total that equalled STATUS1 + STATUS2. In my program, I calculate STATUS1PCT and STATUS2PCT, and the COMPUTE block for each of those report items does show the compound name that PROC REPORT expects to see for calculations. I assume that your data are different, but what I wanted to show was how calculations work. And, since I'm not a mind-reader, your program didn't really convey enough information to figure out whether you had "pre-summarized" your data or not; especially since you did not show usages or DEFINE statements for COUNTY, SITENAME and SITECITY.

If you posted a sample of your data and a working program, it will be easier for folks to provide more concrete help.

(these 2 statements are in error:

ods html file ' Percent';

proc report = percent nowd;

they should be:

ods html file='Percent.html';

proc report data=percent nowd;

)

You indicated that you were new to PROC REPORT. In addition to running and reviewing the program below, I'd recommend some reading. The documentation on PROC REPORT has a good section on How Proc Report Builds a Report (Base SAS(R) 9.3 Procedures Guide, Second Edition). In addition, these papers are good introductions into how PROC REPORT works:

http://support.sas.com/resources/papers/proceedings12/242-2012.pdf

http://www2.sas.com/proceedings/forum2008/170-2008.pdf

http://support.sas.com/resources/papers/proceedings11/267-2011.pdf

http://www2.sas.com/proceedings/forum2008/079-2008.pdf

http://www.lexjansen.com/pharmasug/2012/TF/PharmaSUG-2012-TF20-SAS.pdf

I hope the example and the references help to get you started with PROC REPORT.

cynthia

ods listing close;
ods html file='c:\temp\calcpercent.html';

proc report data=sashelp.class nowd;
title '1) With Display Usage Showing Every Obs';
column age sex name height,(sum pctsum) weight,(sum pctsum);
define age / display;
define sex / display;
define name / display;
define height / analysis;
define weight / analysis;
define sum / 'Individual Value';
define pctsum / 'Percent' f=percent9.2;
rbreak after / summarize;
run;

proc report data=sashelp.class nowd;
title '2) With Group Usage';
column age n height,(sum pctsum) weight,(sum pctsum);
define age / group;
define height / analysis;
define weight / analysis;
define n  / 'Count';
define sum / 'Group Total Value';
define pctsum / 'Percent' f=percent9.2;
rbreak after / summarize;
run;
ods html close;

data fakedata;
infile datalines dlm=',';
input county sitename total status1  status2  ;
return;
datalines;
1,10,100,60,40
1,11,100,30,70
2,20,200,150,50
2,21,200,100,100
3,30,200,150,50
3,31,200,100,100
;
run;

ods html file='c:\temp\calc_other_Percent.html';

proc report data=fakedata nowd;
title '3) Alternate calculation when dividing by TOTAL';
title2 'To get status1pct and status2pct';
column county sitename total status1 status1pct  status2 status2pct;
define county / display;
define sitename / display;
define total / sum;
define status1 / sum;
define status1pct / computed f=percent9.2;
define status2 / sum;
define status2pct / computed f=percent9.2;
compute status1pct;
status1pct=status1.sum/total.sum;
endcomp;
compute status2pct;
status2pct=status2.sum/total.sum;
endcomp;
rbreak after/ summarize;
run;
ods html close;
title;

Pyrite | Level 9

## Re: proc report calculating percent

Thank you so very much   Appreciate your explanations, suggestions, and resources.  Very very helpful!!!

Calcite | Level 5

## Re: proc report calculating percent

define status1pct/ computed "Percent of total" f=percent8.2;

compute status1pct;

status1pct=status1.sum/total.sum;

endcomp;

Discussion stats
• 3 replies
• 8367 views
• 3 likes
• 3 in conversation