<?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: Help on computing daily EOD Stock balance using either Retain or LAG function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-on-computing-daily-EOD-Stock-balance-using-either-Retain-or/m-p/887001#M350492</link>
    <description>&lt;P&gt;You have a current balance, and a history of arrivals and departures.&amp;nbsp; You want to reconstruct the historic balances.&amp;nbsp; I.e. today's arrival should be subtracted from today's balance to generate yesterday's balance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you sorted data by descending date within productcode.&amp;nbsp; Then:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  set mystocktab ;
  by descending productcode descending days;

  _nxt_arrived=lag(arrived);
  _nxt_departed=lag(departed);

  retain expected_balance;
  if first.productcode then expected_balance=realbalance;
  else expected_balance=expected_balance-_nxt_arrived+_nxt_departed;
  
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Even though I use the lag function (which usually implies retrieval of a chronologically preceding value), I name the resulting variables with a prefix of _NXT_, because data are proceeding in reverse chronological order.&lt;/P&gt;</description>
    <pubDate>Sun, 30 Jul 2023 20:57:49 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2023-07-30T20:57:49Z</dc:date>
    <item>
      <title>Help on computing daily EOD Stock balance using either Retain or LAG function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-on-computing-daily-EOD-Stock-balance-using-either-Retain-or/m-p/886390#M350257</link>
      <description>&lt;P&gt;&lt;BR /&gt;Hi Experts:&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;Am trying to&amp;nbsp;compute daily stock holdings based on the 'Arrival' and 'Departs' of different product codes. I have only the EOD Balance of holdings of each stock. As I need to identify HOW&amp;nbsp; much qty has come in and went out, on every day and what would be the balance held on each day. This is very much required to compute the rental charges on those quantities held on such dates.&lt;/P&gt;&lt;P&gt;I tried the below code and unable to achieve my expected results. Can anyone help me on this ?&lt;/P&gt;&lt;P&gt;Sample input data:&lt;/P&gt;&lt;P&gt;data MyStockTab;&lt;BR /&gt;infile datalines missover;&lt;BR /&gt;input DAYS Date9. ProductCode $8. TYPE $2. Arrived Departed RealBalance ;&lt;BR /&gt;format DAYS DAte9.;&lt;BR /&gt;informat DAYS DAte9.;&lt;BR /&gt;cards;&lt;BR /&gt;26JUL2021 MB6448 A 21 0 220&lt;BR /&gt;25JUL2021 MB6448 A 14 0 0&lt;BR /&gt;24JUL2021 MB6448 A 28 0 0&lt;BR /&gt;23JUL2021 MB6448 A 31 0 0&lt;BR /&gt;22JUL2021 MB6448 A 20 0 0&lt;BR /&gt;21JUL2021 MB6448 D 0 30 0&lt;BR /&gt;21JUL2021 MB6448 A 11 0 0&lt;BR /&gt;20JUL2021 MB6448 A 41 0 0&lt;BR /&gt;25JUN2021 MB3748 A 64 0 320&lt;BR /&gt;20JUN2021 MB3748 D 0 18 0&lt;BR /&gt;18JUN2021 MB3748 A 39 0 0&lt;BR /&gt;12JUN2021 MB3748 A 25 0 0&lt;BR /&gt;11JUN2021 MB3748 D 0 30 0&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;RUN;&lt;/P&gt;&lt;P&gt;*Sorting the descending order days, Asceding on TYPE;&lt;/P&gt;&lt;P&gt;proc sort data=MyStockTab out=MyStockSort;&lt;BR /&gt;by ProductCode days descending TYPE ;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;*Trying to find out the daily stock balance;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data MyStockTable ;&lt;BR /&gt;set MyStockSort;&lt;BR /&gt;by ProductCode ;&lt;BR /&gt;retain Arrived Departed DailyBalance;&lt;BR /&gt;if first.ProductCode then DailyBalance=RealBalance;&lt;BR /&gt;DailyBalance = DailyBalance + Lag(Departed) - Lag(Arrived) ;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;Attached is the XLS file which contains both data and the expected column output.&lt;BR /&gt;In Excel sheet, I wrote the formulae using cell reference but am trying to perform same via SAS.&lt;/P&gt;&lt;P&gt;Please help in correcting my code,&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Ananda&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jul 2023 11:22:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-on-computing-daily-EOD-Stock-balance-using-either-Retain-or/m-p/886390#M350257</guid>
      <dc:creator>sinako</dc:creator>
      <dc:date>2023-07-26T11:22:17Z</dc:date>
    </item>
    <item>
      <title>Re: Help on computing daily EOD Stock balance using either Retain or LAG function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-on-computing-daily-EOD-Stock-balance-using-either-Retain-or/m-p/886406#M350260</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;why do you omit to calculate the DailyBalance with 41 arrived on&amp;nbsp;20JUL2021 for MB6448 in your example sheet?&lt;/P&gt;
&lt;P&gt;Same question for MB3748, you do not include the last line (Departed 30) in your calculation&lt;/P&gt;
&lt;P&gt;and why do you add departed and substract arrived? Shouldn't it be the opposite?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jul 2023 13:12:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-on-computing-daily-EOD-Stock-balance-using-either-Retain-or/m-p/886406#M350260</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2023-07-26T13:12:25Z</dc:date>
    </item>
    <item>
      <title>Re: Help on computing daily EOD Stock balance using either Retain or LAG function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-on-computing-daily-EOD-Stock-balance-using-either-Retain-or/m-p/886574#M350324</link>
      <description>Dear Sir/Madam:&lt;BR /&gt;&lt;BR /&gt;Thank you for your reply and valid question.&lt;BR /&gt;&lt;BR /&gt;I have two separate tables, one table contains only the Last Balance of each stock and another one which has only in-out info like How much arrived and how much departed. I merged both tables.&lt;BR /&gt;&lt;BR /&gt;NOW , I need to compute the daily EOD balance of stock which is on hold with us. Holding of stocks incurs charges of refrigeration, electricity, logistics, rentals, transportation, etc on daily basis. So, the expenditure upon balance held on EOD basis should be calculated to minimize the overall cost.&lt;BR /&gt;&lt;BR /&gt;Regarding your question, the last line whether it could be depart or arrived stock, but that should be taken into consideration so to define the DailyBalance value.&lt;BR /&gt;&lt;BR /&gt;To get better understanding, you can re-sort my data in ascending order of the DAYS, where you can see that, any in-out values are stored in respective columns but the DailyBalance value is computed as final value i.e.available balance for the day (by adding or substracting respective movements of stocks).&lt;BR /&gt;&lt;BR /&gt;Hope I could be able to clarify.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;&lt;BR /&gt;Ananda&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 27 Jul 2023 05:23:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-on-computing-daily-EOD-Stock-balance-using-either-Retain-or/m-p/886574#M350324</guid>
      <dc:creator>sinako</dc:creator>
      <dc:date>2023-07-27T05:23:30Z</dc:date>
    </item>
    <item>
      <title>Re: Help on computing daily EOD Stock balance using either Retain or LAG function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-on-computing-daily-EOD-Stock-balance-using-either-Retain-or/m-p/886592#M350328</link>
      <description>Hi Expert:&lt;BR /&gt;&lt;BR /&gt;Just to add a point. As I do not have any starting point of the Balance value. I am doing a retro-calculation from the current balance backwards to compute the daily EOD balance of stock. Below is example output for your reference.&lt;BR /&gt;DAYS P_Code TYPE Arv Dpt RBal My Exp. Balance&lt;BR /&gt;26-Jul-21 MB6448 A 21 0 223 223&lt;BR /&gt;25-Jul-21 MB6448 A 14 0 0 202&lt;BR /&gt;24-Jul-21 MB6448 A 28 0 0 188&lt;BR /&gt;23-Jul-21 MB6448 A 31 0 0 160&lt;BR /&gt;22-Jul-21 MB6448 A 20 0 0 129&lt;BR /&gt;21-Jul-21 MB6448 D 0 30 0 109&lt;BR /&gt;21-Jul-21 MB6448 A 11 0 0 139&lt;BR /&gt;20-Jul-21 MB6448 A 41 0 0 128&lt;BR /&gt;25-Jun-21 NB3748 A 64 0 320 320&lt;BR /&gt;20-Jun-21 NB3748 D 0 18 0 256&lt;BR /&gt;18-Jun-21 NB3748 A 39 0 0 274&lt;BR /&gt;12-Jun-21 NB3748 A 25 0 0 235&lt;BR /&gt;11-Jun-21 NB3748 D 0 30 0 210&lt;BR /&gt;&lt;BR /&gt;Hope it helps,&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Ananda</description>
      <pubDate>Thu, 27 Jul 2023 08:06:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-on-computing-daily-EOD-Stock-balance-using-either-Retain-or/m-p/886592#M350328</guid>
      <dc:creator>sinako</dc:creator>
      <dc:date>2023-07-27T08:06:11Z</dc:date>
    </item>
    <item>
      <title>Re: Help on computing daily EOD Stock balance using either Retain or LAG function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-on-computing-daily-EOD-Stock-balance-using-either-Retain-or/m-p/886597#M350332</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;please re-read my question and answer correspondingly.&lt;/P&gt;
&lt;P&gt;In my opinion your calculation is both wrong in the result and the method.&lt;/P&gt;
&lt;P&gt;IMHO the line&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;20-Jul-21 MB6448 A 41 0 0 128&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;should be&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;20-Jul-21 MB6448 A 41 0 0 128&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;-41=87!&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;AND &lt;/FONT&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;SPAN&gt;in your code the formula "DailyBalance = DailyBalance + Lag(Departed) - Lag(Arrived) ;"&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;SPAN&gt;should be&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;SPAN&gt;DailyBalance = DailyBalance &lt;FONT size="5"&gt;&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;-&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt; Lag(Departed) &lt;FONT size="5"&gt;&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;+&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt; Lag(Arrived) ;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;so that the whole results are wrong.&lt;/P&gt;
&lt;P&gt;Please confirm.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jul 2023 08:41:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-on-computing-daily-EOD-Stock-balance-using-either-Retain-or/m-p/886597#M350332</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2023-07-27T08:41:09Z</dc:date>
    </item>
    <item>
      <title>Re: Help on computing daily EOD Stock balance using either Retain or LAG function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-on-computing-daily-EOD-Stock-balance-using-either-Retain-or/m-p/887001#M350492</link>
      <description>&lt;P&gt;You have a current balance, and a history of arrivals and departures.&amp;nbsp; You want to reconstruct the historic balances.&amp;nbsp; I.e. today's arrival should be subtracted from today's balance to generate yesterday's balance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you sorted data by descending date within productcode.&amp;nbsp; Then:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  set mystocktab ;
  by descending productcode descending days;

  _nxt_arrived=lag(arrived);
  _nxt_departed=lag(departed);

  retain expected_balance;
  if first.productcode then expected_balance=realbalance;
  else expected_balance=expected_balance-_nxt_arrived+_nxt_departed;
  
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Even though I use the lag function (which usually implies retrieval of a chronologically preceding value), I name the resulting variables with a prefix of _NXT_, because data are proceeding in reverse chronological order.&lt;/P&gt;</description>
      <pubDate>Sun, 30 Jul 2023 20:57:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-on-computing-daily-EOD-Stock-balance-using-either-Retain-or/m-p/887001#M350492</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-07-30T20:57:49Z</dc:date>
    </item>
    <item>
      <title>Re: Help on computing daily EOD Stock balance using either Retain or LAG function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-on-computing-daily-EOD-Stock-balance-using-either-Retain-or/m-p/887054#M350503</link>
      <description>Hi Expert ( mkeintz):&lt;BR /&gt;&lt;BR /&gt;Thank you very much for your solution. It worked well !!!..&lt;BR /&gt;&lt;BR /&gt;My sincere thanks and very heartful gratitude for your trick.&lt;BR /&gt;&lt;BR /&gt;Very nice you,&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;&lt;BR /&gt;Ananda</description>
      <pubDate>Mon, 31 Jul 2023 05:25:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-on-computing-daily-EOD-Stock-balance-using-either-Retain-or/m-p/887054#M350503</guid>
      <dc:creator>sinako</dc:creator>
      <dc:date>2023-07-31T05:25:09Z</dc:date>
    </item>
  </channel>
</rss>

