<?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: Period over Period calculation in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Period-over-Period-calculation/m-p/494907#M72332</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/232207"&gt;@dukem&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1) The calculation must be done by unique ID (Roll_number). The m-o-m/y-o-y change of that&amp;nbsp;roll_number.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Add a counter and reset at the top. Use FIRST/LAST to set the initial variables.&amp;nbsp;&lt;A href="https://stats.idre.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;
&lt;P&gt;2) Should I format date in any particular way?...currently YYYYMM i.e 201708.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You should have a SAS date, with a date format. The actual format doesn't matter, the calculation uses the underlying data to do it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;
&lt;P&gt;3) The start period is 201707...therefore, this block should always be blank for very Roll_number&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Not sure how this matters? Using First/Last and a counter will set it to blank/missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/232207"&gt;@dukem&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;4)Is there any way to put this in a macro for future months since this a monthly updated product&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Not sure what value a macro adds since the number of dates don't matter here. It will do it regardless of the amount of data present.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have SAS/ETS you can check PROC EXPAND which does this automatically.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Check the CONVERT statement and the transformation options.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/?docsetId=etsug&amp;amp;docsetTarget=etsug_expand_examples04.htm&amp;amp;docsetVersion=14.2&amp;amp;locale=en" target="_blank"&gt;https://documentation.sas.com/?docsetId=etsug&amp;amp;docsetTarget=etsug_expand_examples04.htm&amp;amp;docsetVersion=14.2&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 12 Sep 2018 16:13:20 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2018-09-12T16:13:20Z</dc:date>
    <item>
      <title>Period over Period calculation</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Period-over-Period-calculation/m-p/494892#M72329</link>
      <description>&lt;P&gt;I'm trying to&amp;nbsp;c&lt;SPAN class="token comment"&gt;alculate previous periods&amp;nbsp;difference.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ref_period&amp;nbsp; Final_price&lt;/P&gt;&lt;P&gt;201707&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 10000&lt;BR /&gt;201708&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 20000&lt;BR /&gt;201709&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 50000&lt;BR /&gt;201710&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 5000&lt;BR /&gt;201711&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 8000&lt;BR /&gt;201712&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;40000&lt;BR /&gt;201801&amp;nbsp;&amp;nbsp;&amp;nbsp; 30000&lt;BR /&gt;201802&amp;nbsp;&amp;nbsp;&amp;nbsp; 100000&lt;BR /&gt;201803&amp;nbsp;&amp;nbsp;&amp;nbsp; 2000&lt;BR /&gt;201804&amp;nbsp;&amp;nbsp;&amp;nbsp; 1000&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like calculate&lt;/P&gt;&lt;P&gt;1) Month-over-month % change &amp;nbsp;(ie. 201803-201804)&lt;/P&gt;&lt;P&gt;2) Year-over-year % change&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (i.e 201703-2018-03)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to do using final_price variable. I know I have to create a label, but not sure after that.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance&lt;/P&gt;&lt;P&gt;Duke&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Sep 2018 15:19:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Period-over-Period-calculation/m-p/494892#M72329</guid>
      <dc:creator>dukem</dc:creator>
      <dc:date>2018-09-12T15:19:14Z</dc:date>
    </item>
    <item>
      <title>Re: Period over Period calculation</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Period-over-Period-calculation/m-p/494897#M72330</link>
      <description>&lt;P&gt;Do you have data for every period?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If so, look into the LAG and LAG12() functions.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/232207"&gt;@dukem&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I'm trying to&amp;nbsp;c&lt;SPAN class="token comment"&gt;alculate previous periods&amp;nbsp;difference.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ref_period&amp;nbsp; Final_price&lt;/P&gt;
&lt;P&gt;201707&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 10000&lt;BR /&gt;201708&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 20000&lt;BR /&gt;201709&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 50000&lt;BR /&gt;201710&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 5000&lt;BR /&gt;201711&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 8000&lt;BR /&gt;201712&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;40000&lt;BR /&gt;201801&amp;nbsp;&amp;nbsp;&amp;nbsp; 30000&lt;BR /&gt;201802&amp;nbsp;&amp;nbsp;&amp;nbsp; 100000&lt;BR /&gt;201803&amp;nbsp;&amp;nbsp;&amp;nbsp; 2000&lt;BR /&gt;201804&amp;nbsp;&amp;nbsp;&amp;nbsp; 1000&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like calculate&lt;/P&gt;
&lt;P&gt;1) Month-over-month % change &amp;nbsp;(ie. 201803-201804)&lt;/P&gt;
&lt;P&gt;2) Year-over-year % change&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (i.e 201703-2018-03)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to do using final_price variable. I know I have to create a label, but not sure after that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you in advance&lt;/P&gt;
&lt;P&gt;Duke&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Sep 2018 15:28:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Period-over-Period-calculation/m-p/494897#M72330</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-09-12T15:28:11Z</dc:date>
    </item>
    <item>
      <title>Re: Period over Period calculation</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Period-over-Period-calculation/m-p/494904#M72331</link>
      <description>&lt;P&gt;m-o-m&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data diff;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; set x;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;mchg = dif (&amp;nbsp;variable ) / lag(&amp;nbsp;variable ) * 100;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; label mchg = "Monthly Percent Change";&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;y-o-y&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data diff;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; set x;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; ychng = dif12( variable) / lag12( variable ) * 100;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; label ychng = "Yearly Percent Change";&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The issue I'm have is:&lt;/P&gt;&lt;P&gt;1) The calculation must be done by unique ID (Roll_number). The m-o-m/y-o-y change of that&amp;nbsp;roll_number.&lt;/P&gt;&lt;P&gt;2) Should I format date in any particular way?...currently YYYYMM i.e 201708.&lt;/P&gt;&lt;P&gt;3) The start period is 201707...therefore, this block should always be blank for very Roll_number&lt;/P&gt;&lt;P&gt;4)Is there any way to put this in a macro for future months since this a monthly updated product&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope this makes sense.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Sep 2018 16:02:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Period-over-Period-calculation/m-p/494904#M72331</guid>
      <dc:creator>dukem</dc:creator>
      <dc:date>2018-09-12T16:02:10Z</dc:date>
    </item>
    <item>
      <title>Re: Period over Period calculation</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Period-over-Period-calculation/m-p/494907#M72332</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/232207"&gt;@dukem&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1) The calculation must be done by unique ID (Roll_number). The m-o-m/y-o-y change of that&amp;nbsp;roll_number.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Add a counter and reset at the top. Use FIRST/LAST to set the initial variables.&amp;nbsp;&lt;A href="https://stats.idre.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;
&lt;P&gt;2) Should I format date in any particular way?...currently YYYYMM i.e 201708.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You should have a SAS date, with a date format. The actual format doesn't matter, the calculation uses the underlying data to do it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;
&lt;P&gt;3) The start period is 201707...therefore, this block should always be blank for very Roll_number&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Not sure how this matters? Using First/Last and a counter will set it to blank/missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/232207"&gt;@dukem&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;4)Is there any way to put this in a macro for future months since this a monthly updated product&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Not sure what value a macro adds since the number of dates don't matter here. It will do it regardless of the amount of data present.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have SAS/ETS you can check PROC EXPAND which does this automatically.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Check the CONVERT statement and the transformation options.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/?docsetId=etsug&amp;amp;docsetTarget=etsug_expand_examples04.htm&amp;amp;docsetVersion=14.2&amp;amp;locale=en" target="_blank"&gt;https://documentation.sas.com/?docsetId=etsug&amp;amp;docsetTarget=etsug_expand_examples04.htm&amp;amp;docsetVersion=14.2&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Sep 2018 16:13:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Period-over-Period-calculation/m-p/494907#M72332</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-09-12T16:13:20Z</dc:date>
    </item>
    <item>
      <title>Re: Period over Period calculation</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Period-over-Period-calculation/m-p/495180#M72339</link>
      <description>&lt;P&gt;One possibility is to use keyed access, something like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create index idx on have(roll_number,ref_period);
quit;

data want;
  set have;
  year_ago=ref_period-100; /* or use intnx if your variable is date type */
  month_ago=ref_period-1-88*(mod(ref_period,100)=1);
  set have(rename=(final_price=year_old_price ref_period=year_ago)) key=idx;
  if _iorc_ then */ no record found */
    _error_=0;
  else
    pct_chg_year=(final_price-year_old_price)/year_old_price;
  set have(rename=(final_price=month_old_price ref_period=month_ago)) key=idx;
  if _iorc_ then */ no record found */
    _error_=0;
  else
    pct_chg_month=(final_price-month_old_price)/month_old_price;
  format pct_chg: percent5.2;
run;   &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;If your ref_period variable is date type, make sure that all the dates are standardized (e.g. to the first daty of the month).&lt;/P&gt;</description>
      <pubDate>Thu, 13 Sep 2018 07:28:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Period-over-Period-calculation/m-p/495180#M72339</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-09-13T07:28:18Z</dc:date>
    </item>
    <item>
      <title>Re: Period over Period calculation</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Period-over-Period-calculation/m-p/495362#M72344</link>
      <description>&lt;P&gt;I have the following program...however, something is wrong with my calculations. I would like the calculations to be based on roll_number within a ref_period. Therefore, the first roll_number (201707) should not have any value.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; p_diff;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set x;&lt;/P&gt;&lt;P&gt;by Roll_number Ref_period;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if not first.Roll_number then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mperchg= dif(FNL_PRICE) / lag(FNL_PRICE)* &lt;STRONG&gt;100&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; yperchg= dif12(FNL_PRICE) / lag12(FNL_PRICE)* &lt;STRONG&gt;100&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;format &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mperchg comma8.2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; yperchg&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; comma8.2;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&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>Thu, 13 Sep 2018 18:48:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Period-over-Period-calculation/m-p/495362#M72344</guid>
      <dc:creator>dukem</dc:creator>
      <dc:date>2018-09-13T18:48:09Z</dc:date>
    </item>
    <item>
      <title>Re: Period over Period calculation</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Period-over-Period-calculation/m-p/495431#M72346</link>
      <description>You cant use LAG within an IF statement, it won’t work as expected because it’s a queue. You need to add a counter, as I mentioned. Use the enumerate to create the new variable and then if COUNTER&amp;gt;1 then calculate MoM and if counter&amp;gt;12 then YoY changes. &lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Untested: &lt;BR /&gt;&lt;BR /&gt;If first.roll_number then counter=0;&lt;BR /&gt;Counter=1;&lt;BR /&gt;&lt;BR /&gt;Lag_Month = lag1(fnl_price);&lt;BR /&gt;Lag_Year = lag12(fnl_price);&lt;BR /&gt;&lt;BR /&gt;If counter&amp;gt;1 then MoM = (fnl_price/lag_month-1)*100;&lt;BR /&gt;If counter&amp;gt;12 then YoY = (fnl_price/lag_year-1)*100;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 13 Sep 2018 17:51:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Period-over-Period-calculation/m-p/495431#M72346</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-09-13T17:51:20Z</dc:date>
    </item>
  </channel>
</rss>

