DATA Step, Macro, Functions and more

Lag function malfunction

Reply
Contributor
Posts: 65

Lag function malfunction

I am using SAS Base V9.4

I have daily balances each of my customers and i want to find out if they increases or decreases their balance from one day to another. So I wrote the following code but i am receiving following Error.

 

"ERROR 72-185: The LAG function call has too many arguments."

IF LAG(CUST_ID,1)=CUST_ID AND (BALANCE - LAG(BALANCE,1))>0 THEN FLAG="FLAG=1"

 

 

what could be the problem?

Super User
Posts: 5,257

Re: Lag function malfunction

Attaching some sample data, and preferred outcome would help.

 

Lag() is quite efficient, if the data is pre-sorted. But requires a little bit of coding.

SQL for me is easier to understand/code, and if you have the balance data, you could just left join the balance table with itself.

Data never sleeps
PROC Star
Posts: 1,563

Re: Lag function malfunction

[ Edited ]

You haven't opened the doc to look up the lag function have you?

It only takes one argument.  Which happens to be what the error message tells you.

 

Assuming the data is sorted by CUSTOMER DATE:

 

data HAVE;
infile cards missover;
  input CUSTOMER $ BALANCE ;
  cards;
1 34 
1 45
1 65
2 67 
2 78
3 34         
3 35          
3 33          
run;
data WANT;
  set HAVE;
  if      lag(BALANCE)>BALANCE and CUSTOMER=lag(CUSTOMER) then WHAT='Less';
  else if lag(BALANCE)=BALANCE and CUSTOMER=lag(CUSTOMER) then WHAT='Same';
  else if lag(BALANCE)<BALANCE and CUSTOMER=lag(CUSTOMER) then WHAT='More';
run;

 

Super User
Posts: 5,085

Re: Lag function malfunction

Besides solving the problem, here are a couple of tools you might want to learn.  Compare the DIF function to the LAG function.  More important, learn how to use a BY statement in a DATA step.  You will definitely need to know this going forward.  One approach:

 

data want;

   set have;

   by customer;

   change = dif(balance);

   if first.customer=0 and change > 0 then flag='FLAG=1';

run;

 

This assumes that your data are sorted by CUSTOMER.  You can decide whether or not to drop CHANGE from the final data set.

 

What did you want the value of FLAG to be?  "1" or "FLAG=1"?  Right now, you're getting the longer result.

 

Good luck.

Ask a Question
Discussion stats
  • 3 replies
  • 239 views
  • 0 likes
  • 4 in conversation