BookmarkSubscribeRSS Feed
Olivier
Pyrite | Level 9
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
3 REPLIES 3
Peter_C
Rhodochrosite | Level 12
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
data_null__
Jade | Level 19
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_;
Olivier
Pyrite | Level 9
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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1222 views
  • 0 likes
  • 3 in conversation