DATA Step, Macro, Functions and more

Proc FCMP and LAG function

Reply
Super Contributor
Posts: 260

Proc FCMP and LAG function

Hi all.
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.
[pre]
PROC FCMP OUTLIB=work.funcTest.test ;
FUNCTION ratio (variable) ;
prev = LAG(variable) ;
IF prev NE . THEN r = variable / prev ;
ELSE r = . ;
RETURN(r) ;
ENDSUB ;
RUN ; QUIT ;
[/pre]
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.
[pre]
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 = . ;
RUN ;
PROC PRINT ;
RUN ;
[/pre]
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.
Olivier
Valued Guide
Posts: 2,177

Re: Proc FCMP and LAG function

Oliver

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.

that's just my inference=guess

PeterC
Respected Advisor
Posts: 3,799

Re: Proc FCMP and LAG function

Perhaps the function will need two arguments and the second is lag.

[pre]
ratio = ratio(variable,lag(variable));
[/pre]

How about divide;

[pre]
r3 = divide(weight,lag(weight));
[/pre] Message was edited by: data _null_;
Super Contributor
Posts: 260

Re: Proc FCMP and LAG function

Peter,
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
[pre]
PROC FCMP LIST ;
RUN ; QUIT ;
[/pre]
the Output shows :
[pre]
Listing of Variables used in the Program

_N_ _ERROR_ _LIST_ _DELTA_ _DER_ _I_
The Program Lag Length is 0.
[/pre]
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.
Olivier
Ask a Question
Discussion stats
  • 3 replies
  • 228 views
  • 0 likes
  • 3 in conversation