<?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: Calculating YoY with quarterly data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculating-YoY-with-quarterly-data/m-p/805986#M317515</link>
    <description>&lt;P&gt;Sort the dataset to a different order, and then compare observations and calculate:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input year QTR FirmID NI;
datalines;
2000 1 555 60
2000 2 555 61
2000 3 555 64
2001 1 555 66
2000 4 568 900
2001 4 568 945
;

proc sort data=have;
by firmid qtr year;
run;

data want;
set have;
by firmid qtr;
format yoy percent6.2;
yoy = ifn(
  not first.firmid and qtr = lag(qtr) and dif(year) = 1,
  dif(ni) / lag(ni),
  .
);
run;

proc sort data=want;
by firmid year qtr;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The code will take care of missing year/qtr combinations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The nice effect of the IFN function is that all arguments are evaluated before the condition is applied, so the LAG and DIF functions are always executed and have their queues in order.&lt;/P&gt;</description>
    <pubDate>Tue, 05 Apr 2022 07:05:40 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2022-04-05T07:05:40Z</dc:date>
    <item>
      <title>Calculating YoY with quarterly data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-YoY-with-quarterly-data/m-p/805965#M317508</link>
      <description>&lt;P&gt;I have tried to find some already existing threads here but it seems that there is no such similar to calculating year-over-year growth using SAS.&lt;/P&gt;&lt;P&gt;I have quarterly net income data. What I want to calculate is YoY growth of net income(NI).&lt;/P&gt;&lt;P&gt;For example, if the data looks like as below...&amp;nbsp;&lt;/P&gt;&lt;P&gt;Year&amp;nbsp; QTR&amp;nbsp; &amp;nbsp;FirmID&amp;nbsp; &amp;nbsp;NI&lt;/P&gt;&lt;P&gt;2000&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;555&amp;nbsp; &amp;nbsp; &amp;nbsp; 60&lt;/P&gt;&lt;P&gt;2000&amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;555&amp;nbsp; &amp;nbsp; &amp;nbsp; 61&lt;/P&gt;&lt;P&gt;2000&amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;555&amp;nbsp; &amp;nbsp; &amp;nbsp; 64&lt;/P&gt;&lt;P&gt;2001&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;555&amp;nbsp; &amp;nbsp; &amp;nbsp; 66&lt;/P&gt;&lt;P&gt;2000&amp;nbsp; &amp;nbsp; &amp;nbsp;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;568&amp;nbsp; &amp;nbsp; &amp;nbsp; 900&lt;/P&gt;&lt;P&gt;2001&amp;nbsp; &amp;nbsp; &amp;nbsp;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;568&amp;nbsp; &amp;nbsp; &amp;nbsp; 945&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The YoY of NI for firm ID 555 should be 10%. (66/60).&amp;nbsp;&lt;/P&gt;&lt;P&gt;The YoY of NI for firm ID 568 should be 5%. (990/900).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The thing is... I first used "lag" but figured out that many observations do not have all quarters like firm ID 568 in the example.&lt;/P&gt;&lt;P&gt;I am thinking of something like Vlookup in Excel, but I am unsure how to do it in SAS.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am still working on it but ask for your help as well.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks in advance!!!&lt;/P&gt;</description>
      <pubDate>Tue, 05 Apr 2022 01:40:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-YoY-with-quarterly-data/m-p/805965#M317508</guid>
      <dc:creator>JKCho</dc:creator>
      <dc:date>2022-04-05T01:40:08Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating YoY with quarterly data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-YoY-with-quarterly-data/m-p/805967#M317509</link>
      <description>&lt;P&gt;how about this code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table growth as 
  select a.*
       , b.year as year2
       , b.ni as ni2
       , (ni2 / a.ni * 100) as growth from have as a
  left join have as b
  on a.firmid=b.firmid and
     a.QTR=b.QTR and
     a.year+1=b.year
  where b.year ne .
  ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Apr 2022 01:58:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-YoY-with-quarterly-data/m-p/805967#M317509</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2022-04-05T01:58:54Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating YoY with quarterly data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-YoY-with-quarterly-data/m-p/805971#M317511</link>
      <description>&lt;P&gt;True, the fact that your time series may have holes means you can't use a simplistic application of LAG.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BUT each invocation of LAG builds its own queue of values, (as opposed to a simple look-back), allowing this simple technique to do what you want:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input year  QTR   FirmID   NI;
datalines;
2000     1       555      60
2000     2       555      61
2000     3       555      64
2001     1       555      66
2000     4       568      900
2001     4       568      945
run;
data want;
  set have ;
  if qtr=1 then YOY=ifn(firmid=lag(firmid),dif(ni)/lag(ni),.); else
  if qtr=2 then YOY=ifn(firmid=lag(firmid),dif(ni)/lag(ni),.); else
  if qtr=3 then YOY=ifn(firmid=lag(firmid),dif(ni)/lag(ni),.); else
  if qtr=4 then YOY=ifn(firmid=lag(firmid),dif(ni)/lag(ni),.); 
  format yoy percent5.2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;BTW, the &lt;EM&gt;&lt;STRONG&gt;DIF(X)&lt;/STRONG&gt;&lt;/EM&gt; function is simply &lt;EM&gt;&lt;STRONG&gt;X-lag(X)&lt;/STRONG&gt;&lt;/EM&gt;.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;True, ordinarily in programming, if you have&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if qtr=1 then x=function(y); else
if qtr=2 then x=function(y); else
if qtr=3 then x=function(y); else
if qtr=4 then x=function(y);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;you could get the same results by using&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if (qtr=1 or qtr=2 or qtr=3 or qtr=4) then x=function(y);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But that doesn't happen with the LAG (and DIF) functions.&amp;nbsp; That's because every mention of LAG (or DIF) generates a separate queue.&amp;nbsp; So in my code, there is a separate&amp;nbsp; queue for each QTR value - just what you need for YOY.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You may have heard dire warnings to never use the LAG function inside the THEN clause of an IF statement.&amp;nbsp; There's a reason for those warnings, but they are simply a failure to consider LAG as a queue manager.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, regardless of the result of the first argument of the IFN function, both of its outcomes (i.e. the 2nd and 3rd arguments) are executed.&amp;nbsp; So the LAG function is always executed for a given QTR, regardless of whether FIRMID=lag(FIRMID) inside the IFN function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Whenever you see LAG - think&amp;nbsp; "UPDATE FIFO QUEUE".&lt;/P&gt;</description>
      <pubDate>Tue, 05 Apr 2022 03:16:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-YoY-with-quarterly-data/m-p/805971#M317511</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-04-05T03:16:55Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating YoY with quarterly data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-YoY-with-quarterly-data/m-p/805974#M317513</link>
      <description>&lt;P&gt;What do you want to do when the value from one year ago does not exist?&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Apr 2022 04:29:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-YoY-with-quarterly-data/m-p/805974#M317513</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-04-05T04:29:47Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating YoY with quarterly data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-YoY-with-quarterly-data/m-p/805975#M317514</link>
      <description>&lt;P&gt;Cute, but what if there are three years of data on the middle year is missing that quarter?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input year  QTR   FirmID   NI;
datalines;
2000     1       555      60
2001     2       555      61
2001     3       555      64
2002     1       555      66
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Apr 2022 04:34:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-YoY-with-quarterly-data/m-p/805975#M317514</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-04-05T04:34:22Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating YoY with quarterly data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-YoY-with-quarterly-data/m-p/805986#M317515</link>
      <description>&lt;P&gt;Sort the dataset to a different order, and then compare observations and calculate:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input year QTR FirmID NI;
datalines;
2000 1 555 60
2000 2 555 61
2000 3 555 64
2001 1 555 66
2000 4 568 900
2001 4 568 945
;

proc sort data=have;
by firmid qtr year;
run;

data want;
set have;
by firmid qtr;
format yoy percent6.2;
yoy = ifn(
  not first.firmid and qtr = lag(qtr) and dif(year) = 1,
  dif(ni) / lag(ni),
  .
);
run;

proc sort data=want;
by firmid year qtr;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The code will take care of missing year/qtr combinations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The nice effect of the IFN function is that all arguments are evaluated before the condition is applied, so the LAG and DIF functions are always executed and have their queues in order.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Apr 2022 07:05:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-YoY-with-quarterly-data/m-p/805986#M317515</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-04-05T07:05:40Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating YoY with quarterly data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-YoY-with-quarterly-data/m-p/806084#M317556</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Cute, but what if there are three years of data on the middle year is missing that quarter?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input year  QTR   FirmID   NI;
datalines;
2000     1       555      60
2001     2       555      61
2001     3       555      64
2002     1       555      66
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That s primarily a research decision.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then the OP would have to decide whether to set YOY to missing, by modifying the IF statement to:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; if qtr=1 then YOY=ifn(firmid=lag(firmid) and year-1=lag(year),dif(ni)/lag(ni),.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or else calculate an annualized YOY, as in:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if qtr=1 then ANNUALIZED_YOY=ifn(firmid=lag(firmid),(ni/lag(ni))**1/dif(year)-1,.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For a single year YOY, this replaces dif(ni)/lag(ni)&amp;nbsp; &amp;nbsp;with&amp;nbsp; &amp;nbsp; &lt;STRIKE&gt;lag(NI)/NI-1.&lt;/STRIKE&gt;&amp;nbsp; &amp;nbsp; NI/lag(NI)&amp;nbsp; - 1&lt;/P&gt;
&lt;P&gt;So a multiyear gap is just the N'th root of&amp;nbsp; &lt;STRIKE&gt;lag(NI)/NI&lt;/STRIKE&gt;&amp;nbsp; NI/lag(NI)&amp;nbsp; &amp;nbsp;minus 1, where N is the number of years in the gap.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't think this issue creates any difficulty in use of IF ... THEN ...LAG&amp;nbsp; &amp;nbsp;constructions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 30 Apr 2022 22:14:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-YoY-with-quarterly-data/m-p/806084#M317556</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-04-30T22:14:49Z</dc:date>
    </item>
  </channel>
</rss>

