I want to create a datetime variable with numeric date and char time show below. Would anyone please help.
Date variable (Numeric) : 20200423
Time variable (String): 14:21:17.7100000
Thanks,
Did you mean FROM instead of WITH?
A date value is a number of days since 1960. A time value is number of seconds since midnight. A datetime value is a number of seconds since 1960. To build a datetime value from a date and time value use the DHMS() function. Day,Hours,Minutes,Seconds.
datetime=dhms(date,0,0,time);
To convert you string into a time value using the TIME informat.
time=input(string,time20.);
Your number does NOT look like a date, unless you have attached the YYMMDD8. format (or one of the other date type formats that display dates in that pattern). The number 20,200,423 would be in the year 57,265. SAS formats only work with 4 digit years.
So you probably need to first convert that number into an actual date.
date=input(put(number,z8.),yymmdd8.);
So putting them together you get:
data want;
set have;
datetime=dhms(input(put(number,z8.),yymmdd8.),0,0,input(string,time20.));
format datetime datetime20.;
run;
Make sure you attach a datetime type format to your new variable so the values print in way that humans will recognize.
Did you mean FROM instead of WITH?
A date value is a number of days since 1960. A time value is number of seconds since midnight. A datetime value is a number of seconds since 1960. To build a datetime value from a date and time value use the DHMS() function. Day,Hours,Minutes,Seconds.
datetime=dhms(date,0,0,time);
To convert you string into a time value using the TIME informat.
time=input(string,time20.);
Your number does NOT look like a date, unless you have attached the YYMMDD8. format (or one of the other date type formats that display dates in that pattern). The number 20,200,423 would be in the year 57,265. SAS formats only work with 4 digit years.
So you probably need to first convert that number into an actual date.
date=input(put(number,z8.),yymmdd8.);
So putting them together you get:
data want;
set have;
datetime=dhms(input(put(number,z8.),yymmdd8.),0,0,input(string,time20.));
format datetime datetime20.;
run;
Make sure you attach a datetime type format to your new variable so the values print in way that humans will recognize.
Hi Fae,
You can do it like this:
data _null_;
date = 20200423;
time = '14:21:17.7100000';
date1 = input(put(date,8.),yymmdd8.);
put date1= date9.;
time1 = input(time,time16.7);
put time1= time16.7;
dt = date1*60*60*24 + time1;
put dt=datetime24.7;
run;
Or combining all steps together:
data _null_;
date = 20200423;
time = '14:21:17.7100000';
dt = input(put(date,8.),yymmdd8.)*84400 + input(time,time16.7);
put dt=datetime24.7;
run;
Hope this helps.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.