title: Calculate row values based on previous calulated rows SAS
body: <p>I have a SAS-dataset with an overall structure similar to the one below. I want to calculate all &quot;missing&quot; values in the MONEY-column based on the previous month's MONEY and SPEND for each ID (student).</p>
  <p>My data looks like this (INPUT_DATA):</p>
  <pre><code>ID        MONTH       MONEY      SPEND  
  01            1        1000        300
  01            2           .        200
  01            3           .        200
  01            4           .        300
  02            1        2000        100
  02            2           .        100
  02            3           .        150
  02            4           .        100
  03            1         900        600
  03            2           .        100
  03            3           .        200
  </code></pre>
  <p>I want it to look like this (OUTPUT_DATA):</p>
  <pre><code>ID        MONTH       MONEY      SPEND  
  01            1        1000        300
  01            2         700        200
  01            3         500        200
  01            4         300        300
  02            1        2000        100
  02            2        1900        100
  02            3        1800        150
  02            4        1650        100
  03            1         900        600
  03            2         300        100
  03            3         200        200
  </code></pre>
  <p>How is this possible using SAS? I tried something like this using the LAG-function:</p>
  <pre><code>DATA OUTPUT_DATA;
      SET INPUT_DATA;
      IF MONTH &gt; 1 THEN MONEY = LAG1(MONEY) - LAG1(SPEND);
  RUN;
  </code></pre>
  <p>But unfortunately, it does not return the expected output. The data can be created using this:</p>
  <pre><code>data INPUT_DATA;
    infile datalines;
    input ID MONTH MONEY SPEND;
  datalines;
  01            1        1000        300
  01            2           .        200
  01            3           .        200
  01            4           .        300
  02            1        2000        100
  02            2           .        100
  02            3           .        150
  02            4           .        100
  03            1         900        600
  03            2           .        100
  03            3           .        200
  ;
  run;
  </code></pre>