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

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

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

brulard
Pyrite | Level 9
thanks for the quick help RW9
ballardw
Super User

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

brulard
Pyrite | Level 9

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

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
  • 4 replies
  • 8274 views
  • 4 likes
  • 3 in conversation