Help using Base SAS procedures

Issues with Lag Command

Reply
Occasional Contributor
Posts: 10

Issues with Lag Command

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

Super User
Super User
Posts: 6,499

Re: Issues with Lag Command

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.

Occasional Contributor
Posts: 10

Re: Issues with Lag Command

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?

Super User
Super User
Posts: 6,499

Re: Issues with Lag Command

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.

Super User
Posts: 10,483

Re: Issues with Lag Command

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

Valued Guide
Posts: 3,208

Re: Issues with Lag Command

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 --<-----
Ask a Question
Discussion stats
  • 5 replies
  • 294 views
  • 3 likes
  • 4 in conversation