DATA Step, Macro, Functions and more

logic behind lag() when using with first. ??

Reply
Contributor
Posts: 60

logic behind lag() when using with first. ??

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

Obs loan_id date amount n diff_amt 1 2 3 4 5 6 7 8 9
112/11/20154001.
111/11/20156002.
110/11/20158003.
210/11/201501.
209/11/20158002200
208/11/201510003.
303/11/20156001.
302/11/201580020
301/11/201520003.

 

 

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;

Super User
Super User
Posts: 6,502

Re: logic behind lag() when using with first. ??

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;
Valued Guide
Posts: 797

Re: logic behind lag() when using with first. ??

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;
Valued Guide
Posts: 797

Re: logic behind lag() when using with first. ??

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;
Ask a Question
Discussion stats
  • 3 replies
  • 144 views
  • 0 likes
  • 3 in conversation