BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Fae
Obsidian | Level 7 Fae
Obsidian | Level 7

I want to create a datetime variable with numeric date and char time show below.  Would anyone please help.

 

Date variable (Numeric) :   20200423

Time variable (String):         14:21:17.7100000

 

Thanks,

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Did you mean FROM instead of WITH?

A date value is a number of days since 1960.  A time value is number of seconds since midnight. A datetime value is a number of seconds since 1960.  To build a datetime value from a date and time value use the DHMS() function. Day,Hours,Minutes,Seconds.

datetime=dhms(date,0,0,time);

To convert you string into a time value using the TIME informat.

time=input(string,time20.);

Your number does NOT look like a date, unless you have attached the YYMMDD8. format (or one of the other date type formats that display dates in that pattern).  The number 20,200,423 would be in the year 57,265.  SAS formats only work with 4 digit years.

So you probably need to first convert that number into an actual date.

date=input(put(number,z8.),yymmdd8.);

So putting them together you get:

data want;
  set have;
  datetime=dhms(input(put(number,z8.),yymmdd8.),0,0,input(string,time20.));
  format datetime datetime20.;
run;

Make sure you attach a datetime type format to your new variable so the values print in way that humans will recognize.

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

Did you mean FROM instead of WITH?

A date value is a number of days since 1960.  A time value is number of seconds since midnight. A datetime value is a number of seconds since 1960.  To build a datetime value from a date and time value use the DHMS() function. Day,Hours,Minutes,Seconds.

datetime=dhms(date,0,0,time);

To convert you string into a time value using the TIME informat.

time=input(string,time20.);

Your number does NOT look like a date, unless you have attached the YYMMDD8. format (or one of the other date type formats that display dates in that pattern).  The number 20,200,423 would be in the year 57,265.  SAS formats only work with 4 digit years.

So you probably need to first convert that number into an actual date.

date=input(put(number,z8.),yymmdd8.);

So putting them together you get:

data want;
  set have;
  datetime=dhms(input(put(number,z8.),yymmdd8.),0,0,input(string,time20.));
  format datetime datetime20.;
run;

Make sure you attach a datetime type format to your new variable so the values print in way that humans will recognize.

LeonidBatkhan
Lapis Lazuli | Level 10

Hi Fae,

 

You can do it like this:

 

data _null_;
   date = 20200423;
   time = '14:21:17.7100000';

   date1 = input(put(date,8.),yymmdd8.);
   put date1= date9.;

   time1 = input(time,time16.7);
   put time1= time16.7;

   dt = date1*60*60*24 + time1;
   put dt=datetime24.7;
run;

Or combining all steps together:

 

data _null_;
   date = 20200423;
   time = '14:21:17.7100000';

   dt = input(put(date,8.),yymmdd8.)*84400 + input(time,time16.7);
   put dt=datetime24.7;
run;

Hope this helps.

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
  • 2 replies
  • 7525 views
  • 1 like
  • 3 in conversation