I want the difference of latest amount & previous amount according to loan_id, In Try1 I'm getting not correct output then I tried Try2
which worked but i'm not able to understand why Try1 Got failed ??
Try1:
data loan1;
input loan_id date ddmmyy10. amount;
format date ddmmyy10.;
datalines;
1 10/11/2015 800
1 12/11/2015 400
2 09/11/2015 800
2 10/11/2015 0
3 01/11/2015 2000
1 11/11/2015 600
3 02/11/2015 800
3 03/11/2015 600
2 08/11/2015 1000
;
run;
proc sort data=loan1;
by loan_id descending date;
run;
data want;
set loan1;
by loan_id;
if first.loan_id then n=1;
else n+1;
if n=2 then diff_amt=amount-lag1(amount);
run;
proc print data=want;
run;
Output :
SAS Output
1 | 12/11/2015 | 400 | 1 | . |
1 | 11/11/2015 | 600 | 2 | . |
1 | 10/11/2015 | 800 | 3 | . |
2 | 10/11/2015 | 0 | 1 | . |
2 | 09/11/2015 | 800 | 2 | 200 |
2 | 08/11/2015 | 1000 | 3 | . |
3 | 03/11/2015 | 600 | 1 | . |
3 | 02/11/2015 | 800 | 2 | 0 |
3 | 01/11/2015 | 2000 | 3 | . |
Try 2:
This gave me correc o/p :
data want;
set loan1;
by loan_id;
if first.loan_id then n=1;
else n+1;
prev_value=lag1(amount);
if n=2 then diff_amt=amount-prev_value;
run;
proc print data=want;
run;
LAG() returns values based on the previous calls. It knows nothing about observations or data step iterations. The reason the first one does not do what you want is because use did not call LAG() for every value. Try this little data step and look at the results.
data _null_;
do i=1 to 10 ;
lagall = lag(i);
if mod(i,2)=0 then lageven=lag(i);
else lagodd = lag(i);
put (i lag: ) (=);
end;
run;
This is a good opportunity to benefit from multiperior lags, and arrays with lower bounds of 0:
Regards
Mark
data _null_;
array L{0:1} lageven lagodd;
do I=1 to 10;
L{mod(i,2)} = lag2(i);
put (i lag:) (=);
end;
run;
This program does not require sorting. Instead it holds the "lagged values" in a hash table. No need for the lag function:
regards,
Mark
data loan1;
input loan_id date ddmmyy10. amount;
format date ddmmyy10.;
datalines;
1 10/11/2015 800
1 12/11/2015 400
2 09/11/2015 800
2 10/11/2015 0
3 01/11/2015 2000
1 11/11/2015 600
3 02/11/2015 800
3 03/11/2015 600
2 08/11/2015 1000
;
run;
data want;
set loan1;
attrib prior_amt length=8;
if _n_=1 then do;
declare hash h();
h.definekey('loan_id');
h.definedata('prior_amt');
h.definedone();
end;
rc=h.find();
if rc^=0 then diff_amt=.;
else diff_amt=amount-prior_amt;
rc=h.replace(key:loan_id,data:amount);
run;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.