BookmarkSubscribeRSS Feed
atul_desh
Quartz | Level 8

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;

3 REPLIES 3
Tom
Super User Tom
Super User

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;
mkeintz
PROC Star

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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
mkeintz
PROC Star

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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 736 views
  • 0 likes
  • 3 in conversation