SAS lag problems

I just found an interesting (that's interesting in the 'I just spent an hour debugging that?' sense) characteristic in SAS. If you have a variable, x, and are using the lag of x, do NOT put the lag(x) inside a conditional statement. This can apparently cause some pretty strange results. It is probably easier to see through code than to explain:

SAS:
  1. data test;
  2. input id$1 x;
  3. cards;
  4.   a 1
  5.   a 2
  6.   a 3
  7.   b 12
  8.   b 13
  9.   b 15
  10. run;
  11.  
  12. data test;
  13. set test;
  14.   lagid=lag(id);
  15.   if lagid=id then do;
  16.     lagx=lag(x);
  17.     duration=x-lagx;
  18.   end;
  19. run;
  20.  
  21. proc print;
  22. run;

And here are the results:

SAS:
  1. Obs    id     x    lagid    lagx    duration
  2. 1     a      1               .         .
  3. 2     a      2      a        .         .
  4. 3     a      3      a        2         1
  5. 4     b     12      a        .         .
  6. 5     b     13      b        3        10
  7. 6     b     15      b       13         2

There are obviously some strange things going on here. I have no idea what is going on in the 2nd row of the results where lagx is missing when it should be 1. And in the 5th row, lagx is the 3 from 2 rows above when it should be 12. Luckily, the solution is easy. Just execute the lag every time:

SAS:
  1. data test;
  2. set test;
  3.   lagid=lag(id);
  4.   lagx=lag(x);
  5.   if lagid=id then do;
  6.     duration=x-lagx;
  7.   end;
  8. run;

Anyway, it took me two years to run into this so I guess this isn't a common mistake but if you're ever having trouble with a lag, check to make sure it is executed for every line of the data.