The SAS Output Delivery System and reporting techniques

compute the percentages in proc report

Accepted Solution Solved
Reply
Regular Contributor
Posts: 165
Accepted Solution

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 Issuespercentages
13507.
16017.
14506.
 20.
 
and that the aimed ressult 
 
Top 3 Sites with highest # issues
# Open Issuespercentages
1350735%
1601735%
1450630%
 20100%
 
 

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

View solution in original post


All Replies
Respected Advisor
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

Posted in reply to PaigeMiller

It wouldn't give me the result I want 

Respected Advisor
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

Posted in reply to Cynthia_sas

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 Issuespercentages
1350735%
1601735%
1450630%
total 20100%
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

 

compute_after_total.png

Regular Contributor
Posts: 165

Re: compute the percentages in proc report

Posted in reply to Cynthia_sas

Thanks so much it's working 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 182 views
  • 2 likes
  • 3 in conversation