Hi Experts:
Am trying to 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 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.
I tried the below code and unable to achieve my expected results. Can anyone help me on this ?
Sample input data:
data MyStockTab;
infile datalines missover;
input DAYS Date9. ProductCode $8. TYPE $2. Arrived Departed RealBalance ;
format DAYS DAte9.;
informat DAYS DAte9.;
cards;
26JUL2021 MB6448 A 21 0 220
25JUL2021 MB6448 A 14 0 0
24JUL2021 MB6448 A 28 0 0
23JUL2021 MB6448 A 31 0 0
22JUL2021 MB6448 A 20 0 0
21JUL2021 MB6448 D 0 30 0
21JUL2021 MB6448 A 11 0 0
20JUL2021 MB6448 A 41 0 0
25JUN2021 MB3748 A 64 0 320
20JUN2021 MB3748 D 0 18 0
18JUN2021 MB3748 A 39 0 0
12JUN2021 MB3748 A 25 0 0
11JUN2021 MB3748 D 0 30 0
;
RUN;
*Sorting the descending order days, Asceding on TYPE;
proc sort data=MyStockTab out=MyStockSort;
by ProductCode days descending TYPE ;
run;
*Trying to find out the daily stock balance;
data MyStockTable ;
set MyStockSort;
by ProductCode ;
retain Arrived Departed DailyBalance;
if first.ProductCode then DailyBalance=RealBalance;
DailyBalance = DailyBalance + Lag(Departed) - Lag(Arrived) ;
run;
Attached is the XLS file which contains both data and the expected column output.
In Excel sheet, I wrote the formulae using cell reference but am trying to perform same via SAS.
Please help in correcting my code,
Regards,
Ananda
You have a current balance, and a history of arrivals and departures. You want to reconstruct the historic balances. I.e. today's arrival should be subtracted from today's balance to generate yesterday's balance.
So you sorted data by descending date within productcode. Then:
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;
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.
Hi,
why do you omit to calculate the DailyBalance with 41 arrived on 20JUL2021 for MB6448 in your example sheet?
Same question for MB3748, you do not include the last line (Departed 30) in your calculation
and why do you add departed and substract arrived? Shouldn't it be the opposite?
- Cheers -
Hi,
please re-read my question and answer correspondingly.
In my opinion your calculation is both wrong in the result and the method.
IMHO the line
20-Jul-21 MB6448 A 41 0 0 128
should be
20-Jul-21 MB6448 A 41 0 0 128-41=87!
AND
in your code the formula "DailyBalance = DailyBalance + Lag(Departed) - Lag(Arrived) ;"
should be
DailyBalance = DailyBalance - Lag(Departed) + Lag(Arrived) ;
so that the whole results are wrong.
Please confirm.
- Cheers -
You have a current balance, and a history of arrivals and departures. You want to reconstruct the historic balances. I.e. today's arrival should be subtracted from today's balance to generate yesterday's balance.
So you sorted data by descending date within productcode. Then:
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;
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.