BookmarkSubscribeRSS Feed
omerzeybek
Obsidian | Level 7

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?

3 REPLIES 3
LinusH
Tourmaline | Level 20

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
ChrisNZ
Tourmaline | Level 20

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;

 

Astounding
PROC Star

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.

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
  • 1062 views
  • 0 likes
  • 4 in conversation