BookmarkSubscribeRSS Feed
monday89
Fluorite | Level 6

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;

3 REPLIES 3
kelxxx
Quartz | Level 8
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

novinosrin
Tourmaline | Level 20

data want;
 set have;
 if nmiss(time1,time2) then time3=coalesce(time1,time2);
 else time3=min(time1,time2);
run;
mkeintz
PROC Star

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;

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 835 views
  • 0 likes
  • 4 in conversation