BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ArmyMP
Calcite | Level 5

I generated code that I can't seem to get it to work right. It seems to work fine for most records but I occasionally have records with an issue and I can't figure out what I'm doing wrong.

 

In looking at the following table, how do I get OP_CALC for the first record to equal the Balance_Amt and have all subsequent records calculate the dif of the previous amount? In other words, OP_Calc for COUNT 2 should be -250.03. For some reason, my code seems to be treating the second record in this example as the first.

 

COUNTApplied_AmtBalance_AmtOP_CALC
10503503
2250.03252.97252.97
3502-249.03-502
4-249.030249.03
5250-250-250
6-2500250

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

All functions that work with lagged values must not be called conditionally.

Restructure your code like this:

data op_calc_2;
set op_calc;
by CLMEVNTID COUNT;
OP_CALC = dif(Balance_Amt);
if COUNT = 1 and Balance_Amt > 0 then OP_CALC = Balance_Amt;
run;

View solution in original post

5 REPLIES 5
mkeintz
PROC Star

We can't diagnose your code without seeing it.  Please post the code (using the popup window generated by clicking on the "running max" icon).

 

 

--------------------------
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

--------------------------
ArmyMP
Calcite | Level 5
DATA OP_CALC;
SET WORK.ACCT_BAL_DTL;
  by CLMEVNTID CLMACCNTBLNCDT;
  if first.CLMEVNTID then
	  COUNT=1;
	else COUNT + 1;
run;
	
DATA OP_CALC_2;
SET WORK.OP_CALC;
  by CLMEVNTID COUNT;
  if COUNT=1 AND Balance_Amt>0 then
	  OP_CALC=Balance_Amt;
	else OP_CALC=dif(Balance_Amt);
run;
ballardw
Super User

The Dif and Lag functions run queues. So the "last value" is the value from the last time a condition was true. If you always want the immediately previous value to be used in the assignment then create a temporary value out side of any conditional statement and then use that value as needed. Such as:

DATA OP_CALC_2;
SET WORK.OP_CALC;
  by CLMEVNTID COUNT;
  difval=dif(Balance_Amt);
  if COUNT=1 AND Balance_Amt>0 then
	  OP_CALC=Balance_Amt;
  else OP_CALC=difval;
  drop difval;
run;
Kurt_Bremser
Super User

All functions that work with lagged values must not be called conditionally.

Restructure your code like this:

data op_calc_2;
set op_calc;
by CLMEVNTID COUNT;
OP_CALC = dif(Balance_Amt);
if COUNT = 1 and Balance_Amt > 0 then OP_CALC = Balance_Amt;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 5 replies
  • 693 views
  • 1 like
  • 4 in conversation