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

Hello Team,

 

I have date in one field and time in another..like shown in different formats. How can i combine both

 

DATE                                      TIME                                 ID

(DATETIME20. Formt)         (DATETIME22.3 FMT)              

17DEC2014:00:00:00        01JAN190013:26:00.000    101

 26DEC2014:00:00:00      01JAN1900:19:36:00.000    102

 

 

WANT: DATE_TIME LIKE SHOWN

DATE_TIME                                 ID  

17DEC2014:13:26                     101

26DEC2014:19:36                     102

 

Regards

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
jklaverstijn
Rhodochrosite | Level 12

Hi @robertrao,

 

If both variables are datetimes you can use DATEPART() on the one and TIMEPART() on the other. Now you can combine them with DHMS(). In one line:

 

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

Hope this helps,

- Jan.

View solution in original post

6 REPLIES 6
jklaverstijn
Rhodochrosite | Level 12

Hi @robertrao,

 

If both variables are datetimes you can use DATEPART() on the one and TIMEPART() on the other. Now you can combine them with DHMS(). In one line:

 

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

Hope this helps,

- Jan.

robertrao
Quartz | Level 8
datetime=dhms(datepart(date), 0, 0, timepart(time));

 

The above work pretty well. But I dint find an explanation online as to why the two zeros are being used in between. The documentation didnt explain it either.

Could you please explain ? 

 

Thanks

jklaverstijn
Rhodochrosite | Level 12

The doc does explain this kind of use of DHMS() and especially using a time variable for the fourth parameter.

 

See the online doc of the DHMS function:

 

" The DHMS function returns a numeric value that represents a SAS datetime value. This numeric value can be either positive or negative.
You can also use the DHMS function to combine a SAS date value with a SAS time value to produce a SAS time value. Because a SAS time value is stored in seconds, you can specify 0 for the hour variable and 0 for the minute variable to return the correct value. Here is the syntax:"

DHMS(SAS date, 0, 0, SAS time).

Hope this clears things up a bit,

- Jan.

jklaverstijn
Rhodochrosite | Level 12

Also it looks like you have not accepted my solution but instead a quote from it by yourself.

jklaverstijn
Rhodochrosite | Level 12

Thanks @robertrao for setting that right. 

ballardw
Super User

You may also want to determine what in your process is creating the time variable as a datetime. The earlier in data streams issues like this are corrected the better in the long run.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 6 replies
  • 1236 views
  • 3 likes
  • 3 in conversation