Hi:
I thought your criteria was that the vehicle ids and dates had to be the SAME on the consecutive obs and the the city/state had to be different on the consecutive obs. Without seeing the cities and states that were in your data and seeing your code with LAG, it's hard to understand why the LAG function didn't work for you. That may be a question for Tech Support. They could look at your data and your code with LAG and help you figure out how to make LAG work.
The thing is that when SAS is reading on obs #1, it can't make the decision about whether to output #1 because it hasn't read obs #2 yet -- the decision, when dealing with consecutive pairs can't be made until the second obs in the pair has been encountered (since SAS reads through the dataset sequentially).
This may mean that you have to code LAG to "save" more values from the LAGged observation. This might also mean that the structure of your final dataset could/will be different than the structure of the input dataset. I envision something more like this coming out of LAG and then you could code your conditional logic:
[pre]
V_ID EndV_ID Start_Dt End_Dt Start_TM End_TM StartODO EndODO StartCity StartSt EndCity EndSt StartObs EndObs
1296 1296 1/30/2008 1/30/2008 0:00:00 08:22:42 18301 18560 Omaha NE Kearney NE 1 2
[/pre]
Basically, you'd use LAG to build 1 OBS out of every pair of consecutive observations. Sure, you'll throw away some observations because they don't make sense (like when Veh ID changes or the date is wrong), but with the above information all on 1 obs, it would be easy to see whether the 2 vehicle IDs were the same and the cities, etc and then just subtract to get the miles. One thing that is mentioned in most of the papers and notes about LAG is that you cannot use LAG conditionally (inside an IF statement) -- and I have found this to be true.
There is a Tech Support example
http://support.sas.com/kb/24/665.html that explains:
/* This example shows the difference in output when you use conditional */
/* and unconditional logic in your program. Because the LAG function stores */
/* values on the queue only when it is called, you must call LAG unconditionally */
/* (outside the IF condition) to get the correct answers. (SAS Language Reference, */
/* Dictionary --> Functions and CALL Routines --> LAG function, Example 2) */
This means that you really need to issue the LAG function on every obs and THEN test the conditions and calculate your MILES and/or TIME. With this scenario -- using LAG for every OBS, you will probably have fewer observations coming out of the data step program than you had going into the data step program -- because there will be some combinations of the LAGged observations that do not make sense to test or output.
Here are some more examples of using LAG:
http://support.sas.com/kb/25/938.html
http://support.sas.com/kb/24/694.html
cynthia