BookmarkSubscribeRSS Feed
_Zack_
Fluorite | Level 6

Hi, i'm new to MIP and i'm trying to use lag function inside a User-Defined Logic in SAS Model Implementation Platform to look back several previous value from previous simulationhorizon, but end up getting below error.

ERROR: The program lag length is not defined. Either a variable depends recursively on its own lagged values, or there is a sequence of more than 20 delayed definitions of lagged variables.

below is my code:

beginblock main;

if simulationhorizon <= NO then do;

INT_LAG1 = lag1(INT);

INT_LAG2 = lag2(INT);

INT_LAG3 = lag3(INT);

if simulationhorizon = NO then ACCUM_INT = sum(INT, of INT_LAG:);

end;

_value_=1;

endblock;

 

Or maybe is there any better ways to get previous (eg: lag2, or lagX) value rather than using lag function?

2 REPLIES 2
ballardw
Super User

I don't know about MIP bu the LAG function generally does not work as expected inside any "if" type structure as the lagged value(s) come from the last time the "if" was true, not the immediately preceding record. So you can be looking at considerably different values than expected. The typical approach is to unconditionally assign the Lag value to a variable as you have done prior to the condition and then conditionally use those values.

 

So maybe, and I am guessing here as no experience with MIP, moving the LAG calls to before the "if simulation horizon" would address this. Might require a second set of variables though like:

beginblock main;
   L1 = lag1(INT)
   L2 = lag2(INT)
   L3 = lag3(INT)
   if simulationhorizon <= NO then do;
   INT_LAG1 = L1;
   INT_LAG2 = L2;
   INT_LAG3 = L3;
   if simulationhorizon = NO then ACCUM_INT = sum(INT, of INT_LAG:);
   end;
   _value_=1;
endblock;

If you paste code into a text box opened on the forum with either the </> or "running man" icon indentation and such is preserved. The message window will reformat text and sometimes results in code that doesn't run properly when copy/paste to an editor because of inserted or removed characters.

 

 

yot
SAS Employee yot
SAS Employee

User Defined Logic in MIP is compiled into a risk method. If you look at the generated code you will see it is inside of a PROC COMPILE. The behavior there is very similar to PROC FCMP. The code looks like data step code and often does the same thing but, not always. Within the MIP documentation there is a best practices guide. It helps explain this as well as intermediate variables being retained and get_rf_by_horizon. 

In this case, if INT is a risk factor, using get_rf_by_horizon would be best.