<?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: Calculate percentage from  grand total when last row in data set is grand total in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculate-percentage-from-grand-total-when-last-row-in-data-set/m-p/530545#M145101</link>
    <description>&lt;P&gt;Hi!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To achieve a desired result there are many different approaches. One of them could be using macro variable:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
   set tbl end=end;
   if end then call symput('total',total);
   /* or */
   /*if category = 'Total' then call symput('total',total);*/
run;

data tbl_pct;
   set tbl;
   format pct percent6.2;
   pct = total / &amp;amp;total.;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Good luck!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- Karolis&lt;/P&gt;</description>
    <pubDate>Mon, 28 Jan 2019 06:22:00 GMT</pubDate>
    <dc:creator>karolis_b</dc:creator>
    <dc:date>2019-01-28T06:22:00Z</dc:date>
    <item>
      <title>Calculate percentage from  grand total when last row in data set is grand total</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-percentage-from-grand-total-when-last-row-in-data-set/m-p/530544#M145100</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;Let's say that I have a data &amp;nbsp;set that includes totals for each category and in last row there is grand total.&lt;/P&gt;
&lt;P&gt;The task is to calculate percentage of total in each category from the grand total.&lt;/P&gt;
&lt;P&gt;(So we need to add a new column called: "PCT" )&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data tbl;
input category $  total;
cards;
a  20
b  50
c  80
d  40
e  10
Total 200
;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jan 2019 05:42:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-percentage-from-grand-total-when-last-row-in-data-set/m-p/530544#M145100</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-01-28T05:42:47Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate percentage from  grand total when last row in data set is grand total</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-percentage-from-grand-total-when-last-row-in-data-set/m-p/530545#M145101</link>
      <description>&lt;P&gt;Hi!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To achieve a desired result there are many different approaches. One of them could be using macro variable:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
   set tbl end=end;
   if end then call symput('total',total);
   /* or */
   /*if category = 'Total' then call symput('total',total);*/
run;

data tbl_pct;
   set tbl;
   format pct percent6.2;
   pct = total / &amp;amp;total.;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Good luck!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- Karolis&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jan 2019 06:22:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-percentage-from-grand-total-when-last-row-in-data-set/m-p/530545#M145101</guid>
      <dc:creator>karolis_b</dc:creator>
      <dc:date>2019-01-28T06:22:00Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate percentage from  grand total when last row in data set is grand total</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-percentage-from-grand-total-when-last-row-in-data-set/m-p/530547#M145103</link>
      <description>&lt;P&gt;You can retrieve the value of the last observation by using the nobs= and point= option:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input category $ total;
cards;
a  20
b  50
c  80
d  40
e  10
Total 200
;
run;

data want;
if _n_ = 1 then set have (keep=total rename=(total=grand)) nobs=nobs point=nobs;
set have;
pct = total / grand;
format pct percent8.2;
drop grand;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;category    total         pct

 a            20      10.00% 
 b            50      25.00% 
 c            80      40.00% 
 d            40      20.00% 
 e            10       5.00% 
 Total       200      100.0% 
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jan 2019 06:40:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-percentage-from-grand-total-when-last-row-in-data-set/m-p/530547#M145103</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-01-28T06:40:55Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate percentage from  grand total when last row in data set is grand total</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-percentage-from-grand-total-when-last-row-in-data-set/m-p/530549#M145105</link>
      <description>&lt;P&gt;Can you please explain this code:&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;if _n_ = 1 then set have (keep=x rename=(x=grand)) nobs=nobs point=nobs;&lt;BR /&gt;set have;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;_n_=1 &amp;nbsp;means that it is going to first observation??&lt;/P&gt;
&lt;P&gt;But grand value is in last observation.&lt;/P&gt;
&lt;P&gt;I see that in this code there are 2 set statement.&lt;/P&gt;
&lt;P&gt;It works perfect but I just dont understand the code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jan 2019 07:04:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-percentage-from-grand-total-when-last-row-in-data-set/m-p/530549#M145105</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-01-28T07:04:19Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate percentage from  grand total when last row in data set is grand total</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-percentage-from-grand-total-when-last-row-in-data-set/m-p/530551#M145106</link>
      <description>&lt;P&gt;_n_ is an automatic variable that holds the count of data step iterations. Here, I use it to execute the first set statement only in iteration one (before anything else is done).&lt;/P&gt;
&lt;P&gt;The nobs= and point= options are explained in the documentation of the &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=lestmtsref&amp;amp;docsetTarget=p00hxg3x8lwivcn1f0e9axziw57y.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;Set Statement&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;Used like this, they do a direct (random access) read of the last observation of the dataset. This prevents a full scan of the dataset, which will improve performance if the dataset is large.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jan 2019 07:21:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-percentage-from-grand-total-when-last-row-in-data-set/m-p/530551#M145106</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-01-28T07:21:25Z</dc:date>
    </item>
  </channel>
</rss>

