<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Proc report Compute Percentage after each quarter and Year End in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-report-Compute-Percentage-after-each-quarter-and-Year-End/m-p/765423#M242460</link>
    <description>Sorry, I forgot to add the format. I am using a custom format for quarter, it is edited now. Also, I have added the expected output table. It calculates the percentage at each row for Appointment based visits and Unexpected visits.</description>
    <pubDate>Wed, 01 Sep 2021 16:05:42 GMT</pubDate>
    <dc:creator>Rydhm</dc:creator>
    <dc:date>2021-09-01T16:05:42Z</dc:date>
    <item>
      <title>Proc report Compute Percentage after each quarter and Year End</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-report-Compute-Percentage-after-each-quarter-and-Year-End/m-p/765198#M242361</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to create proc report for a site which report data monthly. We summarize the details of visits and create percentages for each month/quarter and year to date. I am struggling with calculating the percentages at each step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need the percentage at each report line, for each quarter and then ytd. Following is the code I am using. It has sample data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format; 
value quarter
1 = "Jan-Mar"
2 = "Apr-Jun"
3 = "Jul-Sep"
4 = "Oct-Dec";
run;

data have;
input year month$ quarter site_name$ status$ total_visits appointment unexpected oth date date9.;
format date date9.;
datalines;
2021 Jan 1 ABC Submit 15 6 3 0 01JAN2021 
2021 Feb 1 ABC Submit 7 3 1 5 01FEB2021 
2021 Mar 1 ABC Submit 11 6 3 0 01MAR2021 
2021 Apr 2 ABC Submit 21 5 4 0 01APR2021 
2021 May 2 ABC Submit 8 5 2 4 01MAY2021 
2021 Jun 2 ABC Submit 10 7 3 0 01JUN2021 
;
run;

proc report data=have;

column 
("Year" year) 
("Month" month) 
(quarter) 
("Status" status)
("Total visits" 

	("visits*" total_visits) 
	("appointment based"

		("Count" appointment) 
		("%" pct_visits)
	)
	("Unexpected visits"

		("Count" unexpected) 
		("%" pct_visits_unexpected)
	)
)
("Other~" oth);

define year / order "" order=data style=[cellwidth=18mm];
define month / "" order=data style=[cellwidth=18mm];
define quarter / order noprint format=comma8.;
define Status / "" style=[cellwidth=20mm];

define total_visits / "" style=[cellwidth=25mm] format=comma8.;

define appointment / "" style=[cellwidth=25mm] format=comma8.;
define pct_visits / computed "" style=[cellwidth=25mm] format=5.2;

define unexpected / "" style=[cellwidth=25mm] format=comma8.;
define pct_visits_unexpected / computed "" style=[cellwidth=25mm] format=5.2;

define oth / "" style=[cellwidth=25mm] format=comma8.;

/*
compute before quarter;
    holdtot = total_visits.sum;
	app = appointment.sum;
	unx = unexpected.sum;
  endcomp;

compute pct_visits;
 pct_visits= (app/holdtot)*100;
endcomp;

compute pct_visits_unexpected;
 pct_visits_unexpected= (unx/holdtot)*100;
endcomp;
*/
compute pct_visits;
 pct_visits= (appointment.sum/total_visits.sum)*100;
endcomp;

compute pct_visits_unexpected;
 pct_visits_unexpected= (unexpected.sum/total_visits.sum)*100;
endcomp;

compute year;
	if year ~= "" then keep = year;
	if year = "" then year = keep;
endcomp;

compute Status;
	if Status = "Draft" then call define (_row_, "style", "style = [backgroundcolor=pink]");
endcomp;

break after quarter/summarize;

compute after quarter;
	month = put(quarter, quarter.);
	call define (_row_, "style", "style = [backgroundcolor=lightorange]");
endcomp;

break after year/summarize;
compute after year;
	month = "YTD";
	call define (_row_, "style", "style = [backgroundcolor=lightgray]");
endcomp;
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Following is the output I am getting:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 906px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/63187i563D81F57C041D9F/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As you can see, it is calculating same %age for each row.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Output I want is below:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 822px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/63211i52448367B36A2952/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Appreciate any help!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Sep 2021 16:17:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-report-Compute-Percentage-after-each-quarter-and-Year-End/m-p/765198#M242361</guid>
      <dc:creator>Rydhm</dc:creator>
      <dc:date>2021-09-01T16:17:32Z</dc:date>
    </item>
    <item>
      <title>Re: Proc report Compute Percentage after each quarter and Year End</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-report-Compute-Percentage-after-each-quarter-and-Year-End/m-p/765206#M242364</link>
      <description>&lt;P&gt;First thing:&lt;/P&gt;
&lt;PRE&gt;compute after quarter;
	month = put(quarter, quarter.);
	call define (_row_, "style", "style = [backgroundcolor=lightorange]");
endcomp;&lt;/PRE&gt;
&lt;P&gt;Your quarter values are numeric 1 to 4 and 1) there is no "quarter" format, 2) the QTR. format is for dates. So what do you expect to display when Quarter=1? Unless you have written a custom format that you did not share perhaps????&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Compute before what in the next block? As written this calculates the same thing before every line. Did you check and see that percentage is the overall, i.e. 32/72 for appointment and 16/72 for unexpected visits?&lt;/P&gt;
&lt;PRE&gt;compute before;
    holdtot = total_visits.sum;
   app = appointment.sum;
   unx = unexpected.sum;
endcomp;&lt;/PRE&gt;
&lt;P&gt;Take a look at what happens when you add quarter to the compute before in the block above. That may show you part of what is happening.&lt;/P&gt;
&lt;P&gt;Since I don't know which percentage you want where I am stopping here.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Sep 2021 01:27:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-report-Compute-Percentage-after-each-quarter-and-Year-End/m-p/765206#M242364</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-09-01T01:27:02Z</dc:date>
    </item>
    <item>
      <title>Re: Proc report Compute Percentage after each quarter and Year End</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-report-Compute-Percentage-after-each-quarter-and-Year-End/m-p/765423#M242460</link>
      <description>Sorry, I forgot to add the format. I am using a custom format for quarter, it is edited now. Also, I have added the expected output table. It calculates the percentage at each row for Appointment based visits and Unexpected visits.</description>
      <pubDate>Wed, 01 Sep 2021 16:05:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-report-Compute-Percentage-after-each-quarter-and-Year-End/m-p/765423#M242460</guid>
      <dc:creator>Rydhm</dc:creator>
      <dc:date>2021-09-01T16:05:42Z</dc:date>
    </item>
    <item>
      <title>Re: Proc report Compute Percentage after each quarter and Year End</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-report-Compute-Percentage-after-each-quarter-and-Year-End/m-p/765426#M242462</link>
      <description>&lt;P&gt;Never mind! It worked, thanks for your idea to play with compute block. I have updated code. Thanks .&lt;/P&gt;</description>
      <pubDate>Wed, 01 Sep 2021 16:18:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-report-Compute-Percentage-after-each-quarter-and-Year-End/m-p/765426#M242462</guid>
      <dc:creator>Rydhm</dc:creator>
      <dc:date>2021-09-01T16:18:45Z</dc:date>
    </item>
  </channel>
</rss>

