Concatenating Date and Time

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 84
Accepted Solution

Concatenating Date and Time

[ Edited ]

Hello,

 

I want to create a new value datetime derived from existing values. Existing values are formatted as follows: Date, date9. (24MAY2016) and Time, time8. (9:10:07)

 

Result I am looking get may be as follows: 24MAY2016:09:10:07. What is important is that I am able to compare this column datetime against itself, using < > functions.

 

I found the following code online, but it is giving me errors:

Data want; 
attrib newdate format=datetime25.;
set have ;
newdate=input(put(date,date9.)||put(time,time8.),datetime.);run;

 

Here is the sas error message I receive: “Invalid argument to function INPUT at line 190 column 9.

 

Any help would be appreciated, thanks


Accepted Solutions
Solution
‎08-16-2016 01:13 PM
Esteemed Advisor
Esteemed Advisor
Posts: 7,232

Re: Concatenating Date and Time

You have a typo in the string, the | should read ||.

data want; 
  attrib newdate format=datetime25.;
  date="01jan2016"d;
  time=input("9:10:07",time8.);
  newdate=input(catx("T",put(date,date9.),put(time,time8.)),datetime.);
  if datepart(newdate)=date then chk1=1;
  if timepart(newdate)=time then chk2=1;
run;

The above code shows what you want.

View solution in original post


All Replies
Solution
‎08-16-2016 01:13 PM
Esteemed Advisor
Esteemed Advisor
Posts: 7,232

Re: Concatenating Date and Time

You have a typo in the string, the | should read ||.

data want; 
  attrib newdate format=datetime25.;
  date="01jan2016"d;
  time=input("9:10:07",time8.);
  newdate=input(catx("T",put(date,date9.),put(time,time8.)),datetime.);
  if datepart(newdate)=date then chk1=1;
  if timepart(newdate)=time then chk2=1;
run;

The above code shows what you want.

Frequent Contributor
Posts: 84

Re: Concatenating Date and Time

thanks for the quick help RW9
Grand Advisor
Posts: 10,215

Re: Concatenating Date and Time

The easiest way to get a datetime from a date and a time valued variable I believe to be:

 

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

Frequent Contributor
Posts: 84

Re: Concatenating Date and Time

hey Ballardw... thanks for this suggestion... very simple and works great!

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 360 views
  • 2 likes
  • 3 in conversation