BookmarkSubscribeRSS Feed
knveraraju91
Barite | Level 11

Dear,

 

Some one from support helped me in the similar issue but that code is not getting the output i need in this issue. As specs changed so I have to update it.

I need to derive a numeric variable 'b' without imputing time when time is missing. I tried a few ways but did not get what i need.

Please sugest. Thank you

ouput needed;

01SEP17:15:10:00

04SEP17

 

data one;
  input a $16.;
datalines;
2017-09-01T15:10
2017-09-04
;
run; 

 

6 REPLIES 6
tomrvincent
Rhodochrosite | Level 12

Convert to an appropriate format depending on the length of a. 

 

Here's an example.

 

data one;
  format a datetime20.; input a E8601DT20.;
datalines;
2017-09-01T15:10
2017-09-04
;
run;

data two;
  format b datetime20.; input b E8601DN. ;
datalines;
2017-09-01T15:10
2017-09-04
;
run;

Shmuel
Garnet | Level 18

Thanks to @tomrvincent I have learned also new informats for me.

 

It can be done in the same step in any of the next two examples:

data one;
  input a $16.;
  if length(a) = 10 
then my_dt = input(substr(a,1,10), E8601DN10.); else my_dt = input(a, E8601DT16.); datalines; 2017-09-01T15:10 2017-09-04 ; run;

or alternatively by:

data one;
  input a $16.;
  if substr(a,11,1) = 'T' 
then my_dt = input(substr(a,1,10), E8601DN10.); else my_dt = input(a, E8601DT16.); datalines; 2017-09-01T15:10 2017-09-04 ; run;

 

 

tomrvincent
Rhodochrosite | Level 12

Glad I could help.  Please feel free to mark my post as a solution so that I get an extra mug of mead in SAS Valhalla. 🙂

RW9
Diamond | Level 26 RW9
Diamond | Level 26

As I said in the previous post on this question:

https://communities.sas.com/t5/Base-SAS-Programming/Date-time-informat-that-works-when-the-date-time...

(Which you failed to get back to me on)

 

It is quite simple.  Dates, times, datetimes are numbers, dates are number of days since cutoff, time is number of seconds since midnight, datetime is number of seconds since cuttoff.  So if you want a number that number needs to reflect the data which is stored, either date, time, or datetime.  You cannot store the number for date, then the number for datetime in one variable, this is not possible.  Either the variable contains date/time numbers or it contains date numbers.  This is also why, when applying a format, that format applies to every row in the variable, so if you say display as datetime, then all values will either be datetime or missing.  

 

So, from that you can only have one or the other in terms of a numeric variable.

You could of course switch it over an use a character variable, much like you have in your given example.  As that is text - and does not reflect any internal storage - it can contain anything you like, hence why ISO dates are text.  

 

Once again, numeric dates, times, datetimes need to contain all parts to be valid numeric variables.

Shmuel
Garnet | Level 18

@RW9, according to documentation I have, having a date in a form of yyyy-mm-dd 

using informat  E8601DN10. will assume time 00:00:00 and create a datetime variable.

Please check me.

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi @Shmuel.  You are indeed correct. When reading the data using this informat it does impute the missing 00:00:00.  However I thought from the posters original post that they wanted to display the datetime information after reading as two different date formats, which is a different matter.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 2778 views
  • 4 likes
  • 4 in conversation