<?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: Prior period value listed along with the current period value - LAG question, in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Prior-period-value-listed-along-with-the-current-period-value/m-p/600101#M173376</link>
    <description>&lt;P&gt;What exactly are you having trouble with?&lt;/P&gt;
&lt;P&gt;The short,simple way is to process the data in order and use the LAG() function to remember the value of the variable from the previous call to the LAG() function.&amp;nbsp; Remember to clear the result when you start a new account, otherwise your variable will have the last balance from the previous account.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by AccountNumber Period;
  PriorPeriodCurrentBalance=lag(CurrentBalance);
  if first.AccountNumber then call missing(PriorPeriodCurrentBalance);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 29 Oct 2019 15:41:01 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2019-10-29T15:41:01Z</dc:date>
    <item>
      <title>Prior period value listed along with the current period value - LAG question,</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Prior-period-value-listed-along-with-the-current-period-value/m-p/600083#M173366</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a data set that includes Period (20190930, 20190831, 20190731,....), AccountNumber, CurrentBalance, and some other columns.&lt;/P&gt;&lt;P&gt;Let's say the data set goes back 2 years or back to Period 20180131.&lt;/P&gt;&lt;P&gt;What I want to do is I want to add a column to each row in the data set, called PriorPeriodCurrentBalance, and populate it with, well, the current balance amount from the prior month.&lt;/P&gt;&lt;P&gt;I'm pretty sure this can be done with the LAG function and I found some examples but I think I may need to sort my data set first and I can't find examples that do just that.&lt;/P&gt;&lt;P&gt;Anyone has any ideas?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 29 Oct 2019 14:48:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Prior-period-value-listed-along-with-the-current-period-value/m-p/600083#M173366</guid>
      <dc:creator>SasDewd</dc:creator>
      <dc:date>2019-10-29T14:48:06Z</dc:date>
    </item>
    <item>
      <title>Re: Prior period value listed along with the current period value - LAG question,</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Prior-period-value-listed-along-with-the-current-period-value/m-p/600093#M173369</link>
      <description>&lt;P&gt;For sorting (you may have to play with it depending on your data) although it's not 100% necessary, it is extremely useful so you can ensure your LAG function is working correctly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SORT DATA = Have OUT = Want;
	BY AccountNumber Period;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Once your data is sorted how you want it, I would recommend inputting ROW_NUMBER() function with PROC SQL. I'm assuming you want the LAG to be the prior period for the given Account Number?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If so you would do something like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
CREATE TABLE Want AS
SELECT *
       ,ROW_NUMBER() OVER(PARTITION BY Period ORDER BY AccountNumber)
   FROM Have;&lt;BR /&gt;QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Once your ROW_NUMBER() function is lining up with the data the way you want it you can then implement LAG() using the same OVER(PARTITION BY) as your ROW_NUMBER.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sometimes in these cases your data will be reversed so the PRIOR period you want will actually be row 3 while the current period is row 2. If this is the case simply switch your ORDER&amp;nbsp; BY in the ROW_NUMBER (or LAG) to DESC/ASC and that should fix it.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Oct 2019 15:20:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Prior-period-value-listed-along-with-the-current-period-value/m-p/600093#M173369</guid>
      <dc:creator>Krueger</dc:creator>
      <dc:date>2019-10-29T15:20:10Z</dc:date>
    </item>
    <item>
      <title>Re: Prior period value listed along with the current period value - LAG question,</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Prior-period-value-listed-along-with-the-current-period-value/m-p/600101#M173376</link>
      <description>&lt;P&gt;What exactly are you having trouble with?&lt;/P&gt;
&lt;P&gt;The short,simple way is to process the data in order and use the LAG() function to remember the value of the variable from the previous call to the LAG() function.&amp;nbsp; Remember to clear the result when you start a new account, otherwise your variable will have the last balance from the previous account.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by AccountNumber Period;
  PriorPeriodCurrentBalance=lag(CurrentBalance);
  if first.AccountNumber then call missing(PriorPeriodCurrentBalance);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Oct 2019 15:41:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Prior-period-value-listed-along-with-the-current-period-value/m-p/600101#M173376</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-29T15:41:01Z</dc:date>
    </item>
    <item>
      <title>Re: Prior period value listed along with the current period value - LAG question,</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Prior-period-value-listed-along-with-the-current-period-value/m-p/600112#M173381</link>
      <description>&lt;P&gt;Your data is apparently&amp;nbsp; monthly date sorted by AccountNumber and descending date. &amp;nbsp; So yes, you could sort in ascending order, then use the LAG function to retrieve the value for the prior period, making sure you&amp;nbsp; don't contaminate the start of one accountnumber with lagged values from the prior account.&amp;nbsp; At its simplest such a program would look like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have  out=have_ascending;
  by accountnumber period;
run;

data want;
   set have_ascending;
   by accountnumber;
   lagged_bal=lag(current_balance);
   if first.accountnumber=1 then lagged_bal=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now, if for some reason you need the data to be restored to original order, implying a second sort, you could avoid sorting altogether by something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have (keep=accountnumber);
  by accountnumber;
  merge have
        have (firstobs=2 keep=currentbalance rename=(currentbalance=prior_balance));
&lt;STRIKE&gt;**  if first.accountnumber the prior_balance=.;&lt;/STRIKE&gt;
  if last.accountnumber then prior_balannce=.;
  run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Correction above, thanks to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; .&lt;/P&gt;</description>
      <pubDate>Tue, 29 Oct 2019 16:40:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Prior-period-value-listed-along-with-the-current-period-value/m-p/600112#M173381</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-10-29T16:40:34Z</dc:date>
    </item>
    <item>
      <title>Re: Prior period value listed along with the current period value - LAG question,</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Prior-period-value-listed-along-with-the-current-period-value/m-p/600113#M173382</link>
      <description>&lt;P&gt;If reading the data in descending date order then it is LAST observation for the account that should not have a prior value instead of the FIRST.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Oct 2019 16:34:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Prior-period-value-listed-along-with-the-current-period-value/m-p/600113#M173382</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-29T16:34:50Z</dc:date>
    </item>
  </channel>
</rss>

