statsguyz
statsguyz

Reputation: 469

Modifying final column value in SAS by group

I have the following data set:

Student         TestDayStart        TestDayEnd
001             1                   5
001             6                   10
001             11                  15
002             1                   4
002             5                   9
002             10                  14

I would like to make the last 'TestDayEnd' the final value for 'TestDayStart' for each Student. So the data should look like this:

Student         TestDayStart        TestDayEnd
001             1                   5
001             6                   10
001             11                  15
001             15                  15
002             1                   4
002             5                   9
002             10                  14
002             14                  14

I'm not quite sure how I can do this in SAS. Any insight would be appreciated.

Upvotes: 0

Views: 27

Answers (1)

J_Lard
J_Lard

Reputation: 1103

After sorting the dataset you can do this within a data step.

proc sort data=have;
by student testdaystart testdayend;
run;

Now you can use the by and retain statements in the data step. The by statement allows you to find the last student, and the retain statement lets you keep the previous value in the dataset.

data want;
set have;
retain last_testdayend;
by student testdaystart testdayend;
output;
last_testdayend = testdayend;
if last.student then do;
    if testdaystart ne testdayend then do;
        testdaystart = last_testdayend;
        testdayend = last_testdayend;
        output; * this second output statement creates a new record in the dataset;
    end;
end;
drop last_testdayend;
run;

Upvotes: 1

Related Questions