Help using Base SAS procedures

Combining DATETIME22.3 and char

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

Combining DATETIME22.3 and char

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!!!


Accepted Solutions
Solution
‎02-05-2015 10:19 AM
Super User
Posts: 6,936

Re: Combining DATETIME22.3 and char

format var3 datetime22.3;

var3 = var1 + input(var2,time5.);

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Super User
Posts: 7,401

Re: Combining DATETIME22.3 and char

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.

Super User
Posts: 17,814

Re: Combining DATETIME22.3 and char

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.

Occasional Contributor
Posts: 9

Re: Combining DATETIME22.3 and char

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!!

Super User
Super User
Posts: 7,401

Re: Combining DATETIME22.3 and char

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);

Solution
‎02-05-2015 10:19 AM
Super User
Posts: 6,936

Re: Combining DATETIME22.3 and char

format var3 datetime22.3;

var3 = var1 + input(var2,time5.);

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 9

Re: Combining DATETIME22.3 and char

Thanks to all.

The simplest solution is the right one.

Thank you Kurt!

Super User
Posts: 6,936

Re: Combining DATETIME22.3 and char

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

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

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Respected Advisor
Posts: 3,124

Re: Combining DATETIME22.3 and char

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.)

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 585 views
  • 0 likes
  • 5 in conversation