how do repeat the ID for every patient so that they have 11 instances they appear in the dataset. Some patients appear 3 4 7 or 11 times but I want everyone to be featured 11 times.
The following code should work, it repeats the last observation of each id until there is 11 observations per id.
data want;
set have;
by id;
retain i 1;
if first.id then do;
i=1;
i=i+1;
output;
end;
else if last.id=0 and first.id=0 then do;
i=i+1;
output;
end;
else if last.id then do;
do until (i=12);
i=i+1;
output;
status=0;
end;
end;
drop i;
run;
What other variables are in your dataset? Should they be missing or with any initial value?
Please post an sample of your dataset.
Why do you want the IDs to be in 11 observations ? what is the logic or needs?
One way to do it is to select distinct IDs, replicate them to have 11 times each and then
remerge them with your dataset by ID and maybe some other variable (like a date).
Yes,
i need every instance repeated 11 times because I want the data to reflect the patient at different time intervals. the intervals for the experiment are the same for each patient and we took their vitals at each instance so there are other variables. however some patients died during before the full time length of experiment so there is no data for times above when they died. I still want to include them to 11 times for continuity and mark them as Status=0 for dead at this time interval. Im trying to put the data in a counting style.
The data wu
ID. T1
T2
Status
Trt
Number
Size
Z
1 | 0 | 3 | 1 | 1 | 1 | 3 | 12.3 |
1 | 3 | 10 | 1 | 1 | 1 | 3 | 14.7 |
1 | 10 | 15 | 1 | 1 | 1 | 3 | 13.8 |
1 | 15 | 23 | 0 | 1 | 1 | 3 | 15.5 |
2 | 0 | 3 | 1 | 1 | 1 | 5 | 12.3 |
2 | 3 | 10 | 1 | 1 | 1 | 3 | 14.7 |
2 | 10 | 15 | 0 | 1 | 1 | 3 | 13.8 |
2 | 15 | 23 | 0 | 1 | 1 | 3 | 15.5 |
Looking at your table posted each observation have a unique KEY made of
ID T1 and T2.
What values should T1, T2 have in next added observations ?
Steps to result will be:
1) select distinct values of ID taken from current dataset
2) replicate IDs 11 times including T1 T2 values creating a skeleton dataset
3) merging the skeleton with the current dataset by ID T1 T2
The following code should work, it repeats the last observation of each id until there is 11 observations per id.
data want;
set have;
by id;
retain i 1;
if first.id then do;
i=1;
i=i+1;
output;
end;
else if last.id=0 and first.id=0 then do;
i=i+1;
output;
end;
else if last.id then do;
do until (i=12);
i=i+1;
output;
status=0;
end;
end;
drop i;
run;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Lock in the best rate now before the price increases on April 1.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.