data sv_raw1;
input PT_ID $ Visit $ Res;
datalines;
2 4 10
2 5 15
1 1 20
1 2 40
1 3 30
2 1 30
2 2 60
2 3 50
3 1 .
3 2 40
3 3 60
3 4 70
4 1 30
4 2 20
4 3 30
4 4 35
5 1 90
5 2 91
5 3 30
5 4 40
5 5 70
6 1 .
6 2 .
6 3 30
6 4 40
1 0 10
2 0 10
3 0 10
4 0 10
5 0 10
6 0 10
;
Scenario:
1. Baseline visit = 2.
2. Prior baseline visit's res value ( 0,1) should populate in BASE (Variable)
3. Post Baseline visit's res value (3+ visits) should populate in BASE (Variable)
3. if Visit 2 value is missing then prior visit's value should populate in BASE (Variable)
Output Required:
PT_ID | Visit | Res | Base |
1 | 0 | 10 | 10 |
1 | 1 | 20 | 10 |
1 | 2 | 40 | 40 |
1 | 3 | 30 | 40 |
2 | 0 | 10 | 10 |
2 | 1 | 30 | 10 |
2 | 2 | 60 | 60 |
2 | 3 | 50 | 60 |
2 | 4 | 10 | 60 |
2 | 5 | 15 | 60 |
3 | 0 | 10 | 10 |
3 | 1 | . | 10 |
3 | 2 | 40 | 40 |
3 | 3 | 60 | 40 |
3 | 4 | 70 | 40 |
4 | 0 | 10 | 10 |
4 | 1 | 30 | 10 |
4 | 2 | 20 | 20 |
4 | 3 | 30 | 20 |
4 | 4 | 35 | 20 |
5 | 0 | 10 | 10 |
5 | 1 | 90 | 10 |
5 | 2 | 91 | 91 |
5 | 3 | 30 | 91 |
5 | 4 | 40 | 91 |
5 | 5 | 70 | 91 |
6 | 0 | 10 | 10 |
6 | 1 | . | 10 |
6 | 2 | . | 10 |
6 | 3 | 30 | 10 |
6 | 4 | 40 | 10 |
My code:
proc sort data=sv_raw1 out=sv1;
by PT_ID Visit Res;
run;
data baseline1;
set sv1;
retain Base;
by PT_ID Visit Res;
if first.PT_ID then Base = Res;
else if visit eq '2' then Base = Res;
else if visit not in ('2') then
run;