SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ger15xxhcker
Quartz | Level 8

Hi!

 

How can i trasform from this string-> '2019-06-13-15.55.04.392000' to a date?

 

Thanks a lot!

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi @ger15xxhcker 

 


data test;
dt_char= '2019-06-13-15.55.04.392000';
dt_time_num=input(dt_char,ymddttm24.);
format dt_time_num datetime20.;
run;

 

So basically, the idea is apply the right informat to read nonstandard char datetime value and convert the same  to a numeric datetime value i.e number of seconds from Jan1,1960

View solution in original post

6 REPLIES 6
ger15xxhcker
Quartz | Level 8

Datetime and i have to filter in the where statement  where date_column < 2000-01-01-00.00.00.000000

ballardw
Super User

@ger15xxhcker wrote:

Datetime and i have to filter in the where statement  where date_column < 2000-01-01-00.00.00.000000


SAS restricts how you provide a date, time or datetime literal value.

Date "ddMONyy"d or "ddMONyyyy"d: example "01JAN2001"D  the quotes and the D tell SAS to treat the string as a SAS date value.

Datetime "ddMONyy:hh:mm:ss"dt or "01JAN2001:00:00:00"DT  the quotes plus dt indicate datetime value.

 

So after you have a SAS datetime value in your date_column you would want

 

where date_column < "01JAN2001:00:00:00"DT;  

or alternatively

where datepart(date_column) < "01JAN2001"D; 

 

 

Personally I consider in suboptimal to call a variable with a time component a "date" and prefer a variable name with "DT" or "datetime" to avoid confusion as to the content of the variable. The SAS Date related functions often have issues when fed datetimes as dates use days for value and datetime uses seconds. So many datetime values exceed the valid domain of the date related functions and formats besides yielding incorrect results (except possibly near midnight on 1Jan1960 which  is the base date for both).

 

https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.

Kurt_Bremser
Super User

To a datetime:

data test;
charts = '2019-06-13-15.55.04.392000';
date = input(substr(charts,1,10),yymmdd10.);
time = input(substr(charts,12),time15.6);
numts = date*86400 + time;
format numts e8601dt26.6;
run;
novinosrin
Tourmaline | Level 20

Hi @ger15xxhcker 

 


data test;
dt_char= '2019-06-13-15.55.04.392000';
dt_time_num=input(dt_char,ymddttm24.);
format dt_time_num datetime20.;
run;

 

So basically, the idea is apply the right informat to read nonstandard char datetime value and convert the same  to a numeric datetime value i.e number of seconds from Jan1,1960

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 8986 views
  • 8 likes
  • 5 in conversation