04-27-2015 05:11 PM
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!
BaseDum = BaseClusterID;
LagDum = lag(BaseDum);
Lag1Dum = LagDum;
if (substr(LagClusterID,1,7) = 'cluster') AND (substr(LagDum,1,7) ne 'cluster')
then LagClusterID = LagDum;
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;
04-27-2015 06:01 PM
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.
04-28-2015 08:33 AM
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?
04-28-2015 09:24 AM
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.
04-28-2015 11:23 AM
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 ...;
04-28-2015 02:21 PM
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.