BookmarkSubscribeRSS Feed
Anuz
Quartz | Level 8

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 

3 REPLIES 3
Tom
Super User Tom
Super User

What did you try?  What datetime value do you think that string represents?

SASJedi
Ammonite | Level 13

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;
Check out my Jedi SAS Tricks for SAS Users
Tom
Super User Tom
Super User

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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 1185 views
  • 2 likes
  • 3 in conversation