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 | . |
Top 3 Sites with highest # issues | # Open Issues | percentages |
1350 | 7 | 35% |
1601 | 7 | 35% |
1450 | 6 | 30% |
20 | 100% |
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
PROC FREQ makes it simple.
proc freq data=have;
tables site_number;
run;
It wouldn't give me the result I want
@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.
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
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% |
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
Thanks so much it's working
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 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: