I have the following dataset. What I want to do is create a new time_3 column that uses the FIRST date. if one of them is missing, use the one that is available.
subjectid | time_1 | time_2 |
1 | 1/2/2012 1:50 | |
2 | 1/4/2012 13:53 | 1/3/2012 1:50 |
3 | 1/5/2012 1:50 | 2/6/2012 9:32 |
4 | 1/9/2019 12:12 |
subjectid | time_1 | time_2 | time_use |
1 | 1/2/2012 1:50 | 1/2/2012 1:50 | |
2 | 1/4/2012 13:53 | 1/3/2012 1:50 | 1/3/2012 1:50 |
3 | 1/5/2012 1:50 | 2/6/2012 9:32 | 1/5/2012 1:50 |
4 | 1/9/2019 12:12 | 1/9/2019 12:12 |
I had to do in two data sets and is there any one step that solves all?
data want; set have;
if time_1 < time_2 then time_use= time_1;
else if time_2 < time_1 then time_use= time_2;
format time_use datetime16.;run;
run;
data have; set have;
if time_use= . and ventstopdtm = . then time_use= time_2;
else if time_use= . and time_2 = . then time_use= time_1 ;
else time_use= time_use;
format time_use datetime16.;
run;
data want;
set have;
if nmiss(time_1,time_2) then time_use=max(time_1,time_2);
else time_use=min(time_1,time_2);
run;
hi,
im not sure that is optimal but i think that it s worked
can you test it?
have a good day
A
data want;
set have;
if nmiss(time1,time2) then time3=coalesce(time1,time2);
else time3=min(time1,time2);
run;
Since you are examining only 2 time variables, min(time1,time2) is the same as coalesce(time1,time2) for your purposes (i.e. when there is one or more missing value):
data want;
set have;
newtime=min(time1,time2);
run;
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.