Hi,
I have the below dataset
data have;
input original_date $50.;
datalines;
Mon Mar 11 12:01:41 UTC 2024
;
run;
Need a new column (string) with the original date in the format yyyy-mm-ddThh:mm:ssZ.
How can this be done. Thank you
What did you try? What datetime value do you think that string represents?
Try this:
data have;
input _Dow:$3. _M:$3. _D _T:$8. _Z:$3. _Y;
Original_date=_infile_;
Datetime_string=cats(put(_D,2.),_M,put(_Y,4.),'T',_t,_Z);
drop _:;
datalines;
Mon Mar 11 12:01:41 UTC 2024
;
run;
You will probably need to use SCAN() to break that string into its pieces so you can combine them together in the right way to make datetime value.
Example:
data have;
input original_date $50.;
datalines;
Mon Mar 11 12:01:41 UTC 2024
;
data want;
set have;
dayname=scan(original_date,1,' ');
monname=scan(original_date,2,' ');
day=input(scan(original_date,3,' '),?32.);
tod=input(scan(original_date,4,' '),time24.);
tz=scan(original_date,5,' ');
system_tz = getoption('timezone');
year=input(scan(original_date,6,' '),?32.);
date=input(cats(day,monname,year),date9.);
datetime=dhms(date,0,0,tod);
offset = tzoneoff(tz,datetime);
format tod tod8. date date9. datetime E8601dt.;
put (_all_) (=/);
run;
Result
original_date=Mon Mar 11 12:01:41 UTC 2024 dayname=Mon monname=Mar day=11 tod=12:01:41 tz=UTC system_tz= year=2024 date=11MAR2024 datetime=2024-03-11T12:01:41 offset=0
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: