BookmarkSubscribeRSS Feed
PROCDATARUN
Obsidian | Level 7

Hello SAS Community,

 

Running into the following result when attempting to convert character UTC Date to Datefield.

Input data

ActivationUTC = 08-FEB-23 10.54.32 PM UTC

 

(INPUT(t2.ActivationUTC , datetime20.)) FORMAT=DESDFDT7. AS ActivationDate 

 

Output data  = 18Jul19

Expected data = 08Feb23

Can you correct my code ?

 

 

 

4 REPLIES 4
Patrick
Opal | Level 21

Code like below should work for you.

data have;
  ActivationUTC = '08-FEB-23 10.54.32 PM UTC';
  format want_dttm datetime20.;
  want_dttm=input(ActivationUTC,datetime18.);
  if findw(ActivationUTC,'PM') then
    want_dttm=want_dttm + 43200;
run;

A SAS datetime value is the count of seconds since 01Jan1960. If PM is found in the source string then half a day gets added to the SAS datetime value (43200 seconds). 

 

There is no specific SAS informat for your source string and it also doesn't comply with any ISO pattern. It especially doesn't contain any time offset so be aware that the SAS value will be for Zulu time. You will need to increase/decrease the SAS datetime value for your time zone if that's what you need.

 

 

Oligolas
Barite | Level 11

Hi,

it's one step easyer actually:

data want;
ActivationUTC = '08-FEB-23 10.54.32 PM UTC';
wantdt = input(ActivationUTC,datetime21.);
format wantdt datetime21.;
run;
________________________

- Cheers -

Patrick
Opal | Level 21

@Oligolas Oh! The datetime informat can also deal with AM/PM. Didn't realize this and it makes things of course even easier.

@PROCDATARUN Use what @Oligolas proposed. 

LinusH
Tourmaline | Level 20

You learn something every day. I thought this would be a perfect job for a picture informat...but there is no such thing...?

Either I can't find it now, or there shoud be a ballot entry for this. Or is this use case too narrow?

Data never sleeps

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 4 replies
  • 1307 views
  • 1 like
  • 4 in conversation