BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
lewat
Calcite | Level 5

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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.

View solution in original post

7 REPLIES 7
Patrick
Opal | Level 21

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;

Patrick_0-1715487693332.png

 

 

lewat
Calcite | Level 5

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.

Tom
Super User Tom
Super User

What is the photograph in your PDF file supposed to represent?

Tom_0-1715527388875.png

(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.

 

Patrick
Opal | Level 21

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.

lewat
Calcite | Level 5

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.

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
  • 7 replies
  • 1284 views
  • 8 likes
  • 4 in conversation