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

Dear All,

I have a dataset containing a variable DATETIME22.3 with only the date part.

I have a variable $15. with the time part (like 10:00).

How can I combine these two variables in a single Datetime variable?

Thank you for any help...

BR

Lorenzo!!!

1 ACCEPTED SOLUTION
8 REPLIES 8
RW9
Diamond | Level 26 RW9
Diamond | Level 26

So you want to merge based on the datepart only?  Then you can use

on put(datepart(dt22var),date9.)=substr(textdate,1,9)

Assuming of course both use date9. style format.

So basically just put the datepart into the same format as your text and compare, or convert the text one to number and compare datepart with datepart.

Reeza
Super User

How can a datetime variable have only a date part?

It may only show the date but it should have a time part as well.

To create a datetime variable use the dhms() function.

Assuming it's actually a date:

new_var=dhms(date_var, hour(time_var), minute(time_var), second(time_var));

If its not a date and is a date time var you can use datepart() function to get the date portion only.

Lorenzom
Calcite | Level 5

You are right.

In the variable DATETIME22.3 you have only the date set, the time part is 00:00:00.

I want to obtain something like this:

Var1 DATETIME22.3  is 22JAN2012:00:00:00.000

Var2 Char(15)  is 10:00

Var3 DATETIME22.3 that will be Var1 and var2 combined so 22JAN2012:10:00:00.000


Thank you for any help!!

RW9
Diamond | Level 26 RW9
Diamond | Level 26

If var3 is to be text

var3=put(datepart(var1,date9.)||":"||strip(var2)||"00.000";

If num

var3=input(put(datepart(var1,date9.)||":"||strip(var2)||"00.000",datetime22.3);

Lorenzom
Calcite | Level 5

Thanks to all.

The simplest solution is the right one.

Thank you Kurt!

Kurt_Bremser
Super User

Convert the time value in the character variable into a real time value (like newval=input(oldval,time8.);)

Then you just add the time value (which is seconds after midnight) to the datetime value (which is seconds after 01jan1960:00:00:00)

Haikuo
Onyx | Level 15

We probably need more information to give you precise answer, but my assumption is:

" variable DATETIME22.3 with only the date part." meaning something like this: "01JAN2015 00:00:00"dt, the time part is always zero., but the variable is numeric.

if this is the case, then you can go either of the following ways:

1. modified solution from ,

new_var=dhms(datepart(date_var), 0, 0, input(time_var, time15.));

2. new_var=input(catx(' ', put(datepart(date_var),date9.),time_var), anydtdtm23.)

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 8 replies
  • 2334 views
  • 0 likes
  • 5 in conversation