<?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 3 Year Growth in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculate-3-Year-Growth/m-p/242154#M44939</link>
    <description>&lt;P&gt;It can be done efficiently with a data step, using an array:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Company : $8. Revenue Year;
cards;
HP 100 2012
HP 200 2013
HP 300 2014
Sun 200 2011
Sun 400 2012
Sun 500 2013
Sun 800 2015    &amp;lt;- Skipped 2014
Apple 150 2014
Apple 250 2015
Google 120 2010
Google 190 2011
Google 320 2012
Google 440 2013
;

proc sort data=have; by company year; run;

data growth;
set have; by company;
array _r{1950:2050}  _temporary_;
if first.company then call missing(of _r{*});
_r{year} = revenue;
if last.company then
    if not missing(_r{year-3}) then do;
        growth3y = (revenue-_r{year-3}) / _r{year-3};
        output;
        end;
format growth3y percentn8.2;
run;

proc print data=growth noobs; run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 07 Jan 2016 04:11:26 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2016-01-07T04:11:26Z</dc:date>
    <item>
      <title>Calculate 3 Year Growth</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-3-Year-Growth/m-p/241996#M44897</link>
      <description>&lt;P&gt;I have the following dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt; input Company : $8. Revenue Year;&lt;BR /&gt; cards;&lt;BR /&gt;HP 100 2012&lt;BR /&gt;HP 200 2013&lt;BR /&gt;HP 300 2014&lt;BR /&gt;Sun 200 2011&lt;BR /&gt;Sun 400 2012&lt;BR /&gt;Sun 500 2013&lt;BR /&gt;Sun 600 2014&lt;BR /&gt;Sun 800 2015&lt;BR /&gt;Apple 150 2014&lt;BR /&gt;Apple 250 2015&lt;BR /&gt;Google 120 2010&lt;BR /&gt;Google 190 2011&lt;BR /&gt;Google 320 2012&lt;BR /&gt;Google 440 2013&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to calculate 3 year growth rate for each company. Eg. for HP, no three year growth. For Sun, 2015-2012/2012 for Google, 2013-2010/2010 and so on. How do I do this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jan 2016 09:24:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-3-Year-Growth/m-p/241996#M44897</guid>
      <dc:creator>sasmaverick</dc:creator>
      <dc:date>2016-01-06T09:24:26Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate 3 Year Growth</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-3-Year-Growth/m-p/241997#M44898</link>
      <description>&lt;P&gt;Well, haven't tested this, but just merge the year + 3 data back onto the original data and calculate, something like:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table WANT as
  select  A.*,
             case when B.VAL ne . then A.VAL - (B.VAL / B.VAL)
                      else . end as GROWTH
  from    HAVE A
  left join HAVE B
  on       A.COMPANY=B.COMPANY
  and     A.YEAR=(B.YEAR+3);
quit;&lt;/PRE&gt;
&lt;P&gt;Also note, you can search the forums, some of the initial posts may be useful:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Procedures/calculate-growth-rate-within-groupings/m-p/10660/highlight/true#M1053" target="_blank"&gt;https://communities.sas.com/t5/SAS-Procedures/calculate-growth-rate-within-groupings/m-p/10660/highlight/true#M1053&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/Growth-By-Product-Per-Quarter/m-p/50313/highlight/true#M10490" target="_blank"&gt;https://communities.sas.com/t5/Base-SAS-Programming/Growth-By-Product-Per-Quarter/m-p/50313/highlight/true#M10490&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jan 2016 09:42:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-3-Year-Growth/m-p/241997#M44898</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-01-06T09:42:40Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate 3 Year Growth</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-3-Year-Growth/m-p/241999#M44899</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Company : $8. Revenue Year;
cards;
HP 100 2012
HP 200 2013
HP 300 2014
Sun 200 2011
Sun 400 2012
Sun 500 2013
Sun 600 2014
Sun 800 2015
Apple 150 2014
Apple 250 2015
Google 120 2010
Google 190 2011
Google 320 2012
Google 440 2013
;
run;
proc sql;
select *,((select Revenue from have where Company=a.Company and Year=a.Year+3)-a.Revenue)/a.Revenue  as r
 from have as a;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Jan 2016 09:54:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-3-Year-Growth/m-p/241999#M44899</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-01-06T09:54:04Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate 3 Year Growth</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-3-Year-Growth/m-p/242007#M44901</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can also try following solution:-&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input Company : $8. Revenue Year;&lt;BR /&gt;cards;&lt;BR /&gt;HP 100 2012&lt;BR /&gt;HP 200 2013&lt;BR /&gt;HP 300 2014&lt;BR /&gt;Sun 200 2011&lt;BR /&gt;Sun 400 2012&lt;BR /&gt;Sun 500 2013&lt;BR /&gt;Apple 150 2014&lt;BR /&gt;Apple 250 2015&lt;BR /&gt;Google 120 2010&lt;BR /&gt;Google 190 2011&lt;BR /&gt;Google 320 2012&lt;BR /&gt;Google 440 2013&lt;BR /&gt;Sun 600 2014&lt;BR /&gt;Sun 800 2015&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sort data=have;&lt;BR /&gt;by Company Year;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql noprint;&lt;BR /&gt;Select&lt;BR /&gt;"'" || strip(Company) || "'" into : company_Name separated by ','&lt;BR /&gt;From&lt;BR /&gt;have&lt;BR /&gt;group by Company&lt;BR /&gt;having count(Company) &amp;gt; 3&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%put company_Name =&amp;amp;company_Name;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;set have (where= (Company in(&amp;amp;company_Name)));&lt;BR /&gt;by company notsorted;&lt;BR /&gt;retain Total_revenue 0;&lt;/P&gt;&lt;P&gt;if first.company then&lt;BR /&gt;Total_revenue=0;&lt;BR /&gt;Total_revenue = revenue + Total_revenue;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let me know if it works for you.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jan 2016 11:24:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-3-Year-Growth/m-p/242007#M44901</guid>
      <dc:creator>ad123123</dc:creator>
      <dc:date>2016-01-06T11:24:54Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate 3 Year Growth</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-3-Year-Growth/m-p/242059#M44916</link>
      <description>&lt;P&gt;If you know you have data for all years and no gaps in time this data step also works using the lag function directly.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have; 
by company year;
run;


data want;
set have;
by company year;
if first.company then count=1;
else count+1;
return=(lag3(revenue)-revenue)/lag3(revenue);
if count&amp;lt;=3 then return=.;
format return percent8.2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Jan 2016 15:40:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-3-Year-Growth/m-p/242059#M44916</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-01-06T15:40:08Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate 3 Year Growth</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-3-Year-Growth/m-p/242154#M44939</link>
      <description>&lt;P&gt;It can be done efficiently with a data step, using an array:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Company : $8. Revenue Year;
cards;
HP 100 2012
HP 200 2013
HP 300 2014
Sun 200 2011
Sun 400 2012
Sun 500 2013
Sun 800 2015    &amp;lt;- Skipped 2014
Apple 150 2014
Apple 250 2015
Google 120 2010
Google 190 2011
Google 320 2012
Google 440 2013
;

proc sort data=have; by company year; run;

data growth;
set have; by company;
array _r{1950:2050}  _temporary_;
if first.company then call missing(of _r{*});
_r{year} = revenue;
if last.company then
    if not missing(_r{year-3}) then do;
        growth3y = (revenue-_r{year-3}) / _r{year-3};
        output;
        end;
format growth3y percentn8.2;
run;

proc print data=growth noobs; run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Jan 2016 04:11:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-3-Year-Growth/m-p/242154#M44939</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-01-07T04:11:26Z</dc:date>
    </item>
  </channel>
</rss>

