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;
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.
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?
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.
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 ...;
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: