title: How to create 0/1 flag witch infrom whether values were changed between 2 column in some dates in PROC SQL in SAS Enterprise Guide?
body: <p>I have table in SAS Enterprise Guide like below. Table presents history of clients and his eventually changes of services used (columns: BEFORE, AGAIN).</p>
  <p>ID and DT are not sorted, but could be if it is important</p>
  <p>data types of columns:</p>
  <ul>
  <li>ID - numeric</li>
  <li>DT - date</li>
  <li>BEFORE - character</li>
  <li>AFTER - character</li>
  </ul>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>ID</th>
  <th>DT</th>
  <th>BEFORE</th>
  <th>AFTER</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>123</td>
  <td>21MAY2021</td>
  <td>PR</td>
  <td>P</td>
  </tr>
  <tr>
  <td>123</td>
  <td>28OCT2021</td>
  <td>P</td>
  <td>P</td>
  </tr>
  <tr>
  <td>123</td>
  <td>30OCT2023</td>
  <td>P</td>
  <td>P</td>
  </tr>
  <tr>
  <td>85</td>
  <td>01AUG2021</td>
  <td>PR</td>
  <td>PR</td>
  </tr>
  <tr>
  <td>85</td>
  <td>15AUG2021</td>
  <td>PR</td>
  <td>M</td>
  </tr>
  <tr>
  <td>85</td>
  <td>22AUG2021</td>
  <td>M</td>
  <td>PR</td>
  </tr>
  <tr>
  <td>11</td>
  <td>25JUN2021</td>
  <td>P</td>
  <td>P</td>
  </tr>
  <tr>
  <td>122</td>
  <td>22JUL2021</td>
  <td>PR</td>
  <td>PR</td>
  </tr>
  <tr>
  <td>444</td>
  <td>18MAY2022</td>
  <td>PR</td>
  <td>M</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>And I need to create new binary column &quot;COL1&quot; with values 0/1</p>
  <ul>
  <li>If some client (&quot;ID&quot;) changed service from PR (in &quot;BEFORE&quot;) to P or M (in &quot;AFTER&quot;) and did not return to PR (in &quot;AFTER&quot;) service for another 4 or more months (&quot;DT&quot;) --&gt; then 1</li>
  <li>If some client changed service from PR (in &quot;BEFORE&quot;) to P or M (in &quot;AFTER&quot;) and return to PR (in &quot;AFTER&quot;) earlier than after 4 months --&gt; then 0</li>
  <li>If client did not make change from PR to P or M --&gt; then 0</li>
  </ul>
  <p>So as a result I need something like below:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>ID</th>
  <th>DT</th>
  <th>BEFORE</th>
  <th>AFTER</th>
  <th>COL1</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>123</td>
  <td>21MAY2021</td>
  <td>PR</td>
  <td>P</td>
  <td>1 --&gt; changed PR to P and did not back to PR for 4 or more months</td>
  </tr>
  <tr>
  <td>123</td>
  <td>28OCT2021</td>
  <td>P</td>
  <td>P</td>
  <td>1</td>
  </tr>
  <tr>
  <td>123</td>
  <td>30OCT2023</td>
  <td>P</td>
  <td>P</td>
  <td>1</td>
  </tr>
  <tr>
  <td>85</td>
  <td>01AUG2021</td>
  <td>PR</td>
  <td>PR</td>
  <td>0</td>
  </tr>
  <tr>
  <td>85</td>
  <td>15AUG2021</td>
  <td>PR</td>
  <td>M</td>
  <td>0</td>
  </tr>
  <tr>
  <td>85</td>
  <td>22AUG2021</td>
  <td>M</td>
  <td>PR</td>
  <td>0  --&gt; back to PR less than 4 months after changing PR to P or M</td>
  </tr>
  <tr>
  <td>11</td>
  <td>25JUN2021</td>
  <td>P</td>
  <td>P</td>
  <td>0  --&gt; client did not make change from PR to P or M</td>
  </tr>
  <tr>
  <td>122</td>
  <td>22JUL2021</td>
  <td>PR</td>
  <td>PR</td>
  <td>0  --&gt; client did not make change from PR to P or M</td>
  </tr>
  <tr>
  <td>444</td>
  <td>18MAY2022</td>
  <td>PR</td>
  <td>M</td>
  <td>1 --&gt; customer changed PR to P or M and we have no further history of changes so 1</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I totally do not know how to do that in PROC SQL in SAS Enterprise Guide, do you know what to do ?</p>