'Updating rows from the values of previous row

I have dataset where I need to update the values from the previous rows based on test id.

Dataset that I have.

 input acct_num test_id amount ;
    DATALINES; 
    55203610 1  10
    55203610 1  78
    55203610 1  34
    55203610 2  40   
    55203610 2  50
    55203610 2  80
    ;
    run;

Data that I need .

    DATALINES; 
    55203610 1  10
    55203610 1  78
    55203610 1  34
    55203610 2  10   
    55203610 2  78
    55203610 2  34
    ;
    run;

So basically the amount field from testid1 has to be updated to amount field of testid2.



Solution 1:[1]

Without unique keys you cannot do this.

If there are no other variables then just eliminate the TEST_ID=2 observations and duplicate the TEST_ID=1 observations.

data have;
  input acct_num :$10. test_id amount ;
DATALINES; 
55203610 1  10
55203610 1  78
55203610 1  34
55203610 2  40   
55203610 2  50
55203610 2  80
;

data want;
  set have(in=in1 where=(test_id=1))
      have(in=in2 where=(test_id=1))
  ;
  by acct_num;
  if in2 then test_id=2;
run;

Result

Obs    acct_num    test_id    amount

 1     55203610       1         10
 2     55203610       1         78
 3     55203610       1         34
 4     55203610       2         10
 5     55203610       2         78
 6     55203610       2         34

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tom