Hello
I want to create a new column called SAS_date_time with format of date time.
What is the way to combine the date+time fields together?
Data have ;
format SAS_date date9.;
input SAS_date : date9. num_time;
cards;
'19JAN2021'd 101602
'13AUG2020'd 164500
;
Run;
Data have2;
SET have;
string1 = put(num_time,z6.);
string2 = catx(":",substr(string1,1,2),substr(string1,3,2),substr(string1,5,2));
SAS_time = input(string2,time8.);
format SAS_time time8.;
KEEP SAS_date SAS_time;
Run;
No need to do all those character functions 🙂
Data have ;
format SAS_date date9.;
input SAS_date : date9. num_time;
cards;
'19JAN2021'd 101602
'13AUG2020'd 164500
;
Run;
data want;
set have;
dt = dhms(SAS_date, 0, 0, input(put(num_time, 6.), hhmmss6.));
format dt datetime20.;
run;
With the data you have, you can go directly from the variables you start with to the datetime value, using PUT and INPUT:
sas_datetime=input(put(sas_date,yymmddn8.)||'T'||put(num_time,z6.),B8601DT.);
There are many datetime informats, you can almost always find one that fits your needs.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.