Hi SAS Pros,
I'm trying to create2 variables (as showed in the file - PT_pre and PT_post) to present the person-time patients have accrued in each stage, with the pre- and post-stage splits between T9 and T10.
Pre-stage:
The person-time for patients with outcome=1 in pre-stage should sum up the values from T1 until Tn, where Tn is determined by the value of outcome_timepoint (T5 for ID=XXXXX1).
The person-time for patients with outcome=0 in pre-stage should sum up the values from T1 until T9.
Post-stage:
The person-time for patients with outcome=1 in post-stage should sum up the values from T10 until Tn, where Tn is determined by the value of outcome_timepoint (T13 for ID=XXXXX3).
The person-time for patients with outcome=0 in post-stage should sum up the values from T10 until T20.
For example, ID=XXXXX1 is outcome=1 at time point T5, so the person-time he contributed during pre-stage is PT_pre=sum(of T1-T5) (only T1=0, T2=1, T3=1, T4=0, and T5=1). Not every single time point this patient had contribution to person-time, so T1-T20 had both 0 or 1 value.
I appreciate with any help!
Regards,
C
This DATA step builds your input data:
data have;
infile datalines dsd dlm='|';
input ID:$6. Outcome Outcome_timepoint:$3. T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17 T18 T19 T20;
datalines;
XXXXX1|1|T5|0|1|1|0|1|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|3|0
XXXXX2|0|NA|0|1|1|0|1|1|1|0|0|0|0|1|1|0|1|1|1|0|0|0|5|5
XXXXX3|1|T13|0|1|1|0|1|1|1|0|0|0|0|1|1|0|0|0|0|0|0|0|5|2
XXXXX4|0|NA|0|1|1|0|1|1|1|0|0|0|0|1|1|0|1|1|1|0|0|0|5|5
;
This DATA step performs the actions you specified:
data want;
drop i;
set have;
array t[*] t:;
if outcome=0 then do;
PT_pre=sum(of t1-t9);
PT_Post=sum(of t10-t20);
end;
else if outcome=1 then do;
do i=1 to input(compress(outcome_timepoint,,'kd'),32.);
PT_Pre=sum(PT_pre,t[i]);
end;
do i=10 to input(compress(outcome_timepoint,,'kd'),32.);
PT_Post=sum(PT_pre,t[i]);
end;
end;
else put "WARNING: Bad outcome value for " ID= outcome=;
run;
And this is the result:
Obs | ID | Outcome | Outcome_timepoint | T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9 | T10 | T11 | T12 | T13 | T14 | T15 | T16 | T17 | T18 | T19 | T20 | PT_pre | PT_Post |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | XXXXX1 | 1 | T5 | 0 | 1 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 3 | . |
2 | XXXXX2 | 0 | NA | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 0 | 0 | 0 | 5 | 5 |
3 | XXXXX3 | 1 | T13 | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 7 | 8 |
4 | XXXXX4 | 0 | NA | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 0 | 0 | 0 | 5 | 5 |
Note that PT_POST for record 1 is missing, because you specified
The person-time for patients with outcome=1 in post-stage should sum up the values from T10 until Tn, where Tn is determined by the value of outcome_timepoint
The timepoint for observation 1 is T5, which is less than T10. What was your desired outcome for this row? Did you intend for this row to add up the values from T5 to T10?
Hi,
Thank you so much for your help!
The desired outcomes are the person-time was accrued within its own period. Observation 1 developed outcome in T5, which is in pre-stage, then the expected PT_pre=sum(of T1-T9)=3. Observation 3 developed outcome in both pre- and post-stages, so the expected PT_pre=sum(of T1-T9)=5 and PT_post=sum(of T10-T20)=2. It looks like only the calculated results for observation 3 are not correct. Could you please advice how should I modify the code to cooperate my request?
Again, I really appreciate it!
ID | Outcome | Outcome_timepoint | PT_pre | PT_Post | T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9 | T10 | T11 | T12 | T13 | T14 | T15 | T16 | T17 | T18 | T19 | T20 |
XXXXX1 | 1 | T5 | 3 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
XXXXX2 | 0 | NA | 5 | 5 | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 0 | 0 | 0 |
XXXXX3 | 1 | T13 | 5 | 2 | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
XXXXX4 | 0 | NA | 5 | 5 | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 0 | 0 | 0 |
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.