title: Read previous and next observations
body: <p>I have a dataset like this(sp is an indicator):</p>
  
  <pre><code>datetime        sp
  ddmmyy:10:30:00 N
  ddmmyy:10:31:00 N
  ddmmyy:10:32:00 Y
  ddmmyy:10:33:00 N
  ddmmyy:10:34:00 N
  </code></pre>
  
  <p>And I would like to extract observations with "Y" and also the previous and next one:</p>
  
  <pre><code>ID              sp
  ddmmyy:10:31:00 N
  ddmmyy:10:32:00 Y
  ddmmyy:10:33:00 N
  </code></pre>
  
  <p>I tired to use "lag" and successfully extract the observations with "Y" and the next one, but still have no idea about how to extract the  previous one.</p>
  
  <p>Here is my try:</p>
  
  <pre><code>data surprise_6_step3; set surprise_6_step2;
  length lag_sp $1;
  lag_sp=lag(sp);
  if sp='N' and lag(sp)='N' then delete;
  run;
  </code></pre>
  
  <p>and the result is:</p>
  
  <pre><code>ID              sp
  ddmmyy:10:32:00 Y
  ddmmyy:10:33:00 N
  </code></pre>
  
  <p>Any methods to extract the previous observation also?
  Thx for any help.</p>