DATA Step, Macro, Functions and more

Issue Combining Date and Time Variables into Single Numeric Datetime Variable

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Issue Combining Date and Time Variables into Single Numeric Datetime Variable

I have numeric date and time variables that I would like to combine into a single numeric datetime variable. The code below works on some of the data, but not all of it (more specifically it won't calculate a datetime for any row where the time variable has a two digit hour - see example output below Proc SQL code). The SAS log gives two notes: "Invalid year value" and "Invalid argument to function INPUT. Missing values may be generated." I have tried changing the datetime informat of the input statement but that doesn't help. Any suggestions on how to fix this using Proc SQL (I'm using SAS 9.2) would be appreciated.

 

proc sql ;
   select a.injury_date
	 ,a.injury_time
	 ,input(put(a.injury_date,date9.)||put(a.injury_time,time8.),datetime17.) as injury_datetime format = datetime19.
     from injury_data ;

 

injury_dateinjury_timeinjury_datetime
14SEP20061:40:0014SEP2006:01:40:00
29JUN200621:02:00.
02AUG200616:31:00.
02OCT20069:44:0002OCT2006:09:44:00

Accepted Solutions
Solution
‎07-27-2016 12:08 PM
Super User
Posts: 10,548

Re: Issue Combining Date and Time Variables into Single Numeric Datetime Variable

If the variables are actual SAS date and Time valued then you can use

dhms(a.injury_date, 0,0,a.injury_time) as injury_datetime

 

View solution in original post


All Replies
Super User
Super User
Posts: 7,430

Re: Issue Combining Date and Time Variables into Single Numeric Datetime Variable

Use TOD format:

input(cats(put(a.injury_date,date9.),put(a.injury_time,tod8.)),datetime17.) as injury_datetime format = datetime19.
New Contributor
Posts: 2

Re: Issue Combining Date and Time Variables into Single Numeric Datetime Variable

Thanks! I copied your code directly from your post, but it didn't work. The new variable injury_datetime had a missing value for all data rows, and I got the same notes in the SAS log.

Solution
‎07-27-2016 12:08 PM
Super User
Posts: 10,548

Re: Issue Combining Date and Time Variables into Single Numeric Datetime Variable

If the variables are actual SAS date and Time valued then you can use

dhms(a.injury_date, 0,0,a.injury_time) as injury_datetime

 

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 273 views
  • 0 likes
  • 3 in conversation