I'm trying to build new functions with the FCMP procedure (on SAS 9.2), and I'm having strange results with that one : I want to divide the current value by the one that was read on the previous observation.
As in a Data step, I tried the LAG function : no error there, and reading the online doc for FCMP, I did not read that LAG was a forbidden function.
PROC FCMP OUTLIB=work.funcTest.test ;
FUNCTION ratio (variable) ;
prev = LAG(variable) ;
IF prev NE . THEN r = variable / prev ;
ELSE r = . ;
RUN ; QUIT ;
The strange thing is, during execution, the LAG function does not seem to return the previous value, but the current one ! So I always end up with a result of 1 for my function.
OPTION CMPLIB=(work.funcTest) ;
DATA work.test ;
SET sashelp.class ;
r = ratio(weight) ;
vPrec = LAG(weight) ;
IF vPrec NE . THEN r2 = weight / vPrec ;
ELSE r2 = . ;
PROC PRINT ;
Comparing with the hard-coded version, it seems that the formula is correct... but that LAG is not supported properly by FCMP.
Any help from some experienced FCMP user ? Thanks in advance.
I think this has to do with the concepts.
Within a subsoutine/function all are local, except perhaps parameters. So from where should lag() access its lag-stack history? I assume it should be local. Certainly it differs from a data step lag() if the first call returns the input;
In another non-data step context lag() doesn't function ~ when invoked inside %sysfunc() it is rejected at syntax level. I think lag() has more relevance in a subroutine/function, but it cannot interact with the data step lag stacks.
I do agree with you : having LAG work in any context would be really strange. That's why I was trying to use it in FCMP, expecting an error or warning message. But nothing came, except strange results.
Moreover, when I run
PROC FCMP LIST ;
RUN ; QUIT ;
the Output shows :
Listing of Variables used in the Program
_N_ _ERROR_ _LIST_ _DELTA_ _DER_ _I_
The Program Lag Length is 0.
Does that mean that the "Lag length" can be tuned ? I didn't see any option about that.
Maybe some SAS folk would have an answer.
Thanks for your answers.