BookmarkSubscribeRSS Feed
LKlein88
Calcite | Level 5

Hey folks,

I am wondering if there are any issues with the approach I'm taking in using this program with the LAG command.  What I am looking to do is read in the previous LagClusterID if certain conditions are met (if EucDistance < 656 ft, 15 < BaseTime - LagTime < 720, and points are consecutive).  I've included code below and attached two documents, one for Dataset2 and one for Dataset3.  The first image is Dataset2, and the second is Dataset3.  What I am trying to do in Dataset3 is to append Cluster values 17262 to LagIDs 16689 and 16690 in addition to point 16688.  The attached data has been cleansed of any features which may identify any locations or times.  Let me know if these alterations pose any issues.  Any help is appreciated.  Thanks!

Data Dataset2;

  set Dataset1;

  BaseDum = BaseClusterID;

  LagDum = lag(BaseDum);

  Lag1Dum = LagDum;

  if (substr(LagClusterID,1,7) = 'cluster') AND (substr(LagDum,1,7) ne 'cluster')

  then LagClusterID = LagDum;

run;

Data Dataset3;

  set Dataset2;

  if (substr(BaseClusterID,1,7) = 'cluster') AND (substr(LagDum,1,7) ne 'cluster') AND (substr(lag(LagDum),1,7) ne 'cluster')

  then Lag1Dum = lag1(LagDum);

  if (substr(lag1(LagClusterID),1,7) ne 'cluster') AND (substr(LagClusterID,1,7) ne 'cluster')

  then LagClusterID = Lag1Dum;

run;

Dataset2.png

Dataset3.png

5 REPLIES 5
Tom
Super User Tom
Super User

You normally do NOT want to put LAG() function call inside of a conditional as you have done in your second data step.

Plus you have two different places where you can calling LAG() for the same variable since one is conditional and the other is not they could return different values for the same observation in the data set.

Each instance of the LAG() function sets up its own queue of values based on when the instance of the LAG() function executes.

LKlein88
Calcite | Level 5

Ah, I understand.  This does help a bit, though I am now wondering if there is a different command that may help with what I'm trying to accomplish.  Do you think a DO loop may be of help here?

Tom
Super User Tom
Super User

Maybe, but your requirements as not clear.

Post sample input data and sample result data. Post them as DATA steps with inline data so others can re-create your data and see the issue. Just include enough observations to demonstrate the issue.

ballardw
Super User

The general solution is before the conditional use to assign the appropriate lagged values to other variables, use those variables and if you don't need the values (probably after debugging) to drop them:

lagx1 = lag(x);

lagx10 = lag10(x);

if lagx10 ... then ...;

jakarman
Barite | Level 11

The LAG function of SAS is not the LAG function of SQL. You should see the SAS LAG functions as the QUEUE function.

The lag works in a condition but the result is a shift in the queue it is not retrieving something from a previous record.

---->-- ja karman --<-----

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 1190 views
  • 3 likes
  • 4 in conversation