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
SAS Super FREQ

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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 416 views
  • 2 likes
  • 3 in conversation