Hi there,
I am running into issues with the lag function. I am able to use lag(variable), if recalling only the observation immediately prior, however I am not able to use lag2(variable), lag3(variable), etc.. As soon as I enter a numerical value (n) next to the lag function, it is no longer highlighted in blue or recognized as a SAS function.
Checking my data shows it is not picking up the function, beyond the first lag(variable), with no 'n' specified.
Has anyone experienced this? I am using SAS Studio.
As others already explained: SAS Studio is just the client with some in-built syntax highlighting. This highlighting is not perfect so just take it as guidance.
SAS Studio sends the code to the SAS server for execution. It's the SAS log created as part of execution that will tell you if some syntax is really valid or not. The syntax you shared is valid so just execute it and inspect the SAS log.
Not sure what you believe is not working.
Make sure that in your real code you don't use the lag function conditionally. It only works as you'd expect if it executes for every single row of data.
data demo;
set sashelp.class;
/*Setting lag name of the original name*/
LAG_name = lag(name); *'lag' highlighed in blue;
LAG2_name = lag2(name); *'lag' not highlighed;
LAG3_name = lag3(name); *'lag' not highlighed;
LAG4_name = lag4(name); *'lag' not highlighed;
run;
proc print data=demo;
var name lag:;
run;
The lag function is not being used conditionally. Any lag greater than lag of 1 observation is not being recognized as a function, at all. I have attached a screenshot in case this is helpful.
Confirming this issue also persists when I run the code. Lag2 onwards is not recognized.
What is the photograph in your PDF file supposed to represent?
(and why did you go to the trouble of making a PDF file and uploading it when you can just paste the image into the editor or use the Insert image icon instead of the attach file icon?)
I assume you are talking about the coloring that the SAS/Studio editor is adding to try and help you? Remember that SAS/studio is NOT the actual SAS processor. It is just a front end tool to help you edit and submit your code to SAS.
If you want to show proof that SAS did not understand the lag2() function then you need to paste in the lines from the SAS log with the error message they generate.
As others already explained: SAS Studio is just the client with some in-built syntax highlighting. This highlighting is not perfect so just take it as guidance.
SAS Studio sends the code to the SAS server for execution. It's the SAS log created as part of execution that will tell you if some syntax is really valid or not. The syntax you shared is valid so just execute it and inspect the SAS log.
The syntax checker in Studio is not 100% complete, it will miss some elements. The real check happens when you run your code; look at the log.
Perhaps this is it. I am at a loss. I have tried to run my data using the lag functions and this confirms only the first lag(variable) is recognized (before I add a numerical value) as if the numerical value invalidates it. I have attached a screenshot of the code, itself, unhighlighted.
Don't let yourself be confused by the coloring of the Studio editor. You know that LAGx is valid, so use it.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.