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

Hi,

How to get YYYY-MM-DDThh:mm:ss from date (yymmdd10.) and time ( tod8.)

 

My approch is mentioned below which is not proper for 1st record.

 

data xyz;
date="12-Aug-2020";
output;
Time= "11:45 PM";
output;
run;

data abc;
set xyz;
date1=put(input(date,anydtdte20.),yymmdd10.);
time1 = put(input(time,time8.),tod8.);
date_time = catx ("T",date1,time1);
run;

 

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

In SAS, YOU DO NOT STORE DATES OR TIMES AS CHARACTER.

Any format for display can be used whenever needed (eg for reports or export), but storing such values as character deprives you of the use of date&time functions and formats and necessitates re-conversion whenever a calculation needs to be done.

On top of that all, you need much more space to store a datetime as character (19 characters here) vs. numeric (only 8 bytes). Same for dates (minimum 8 characters without delimiters), which need only 4 numeric bytes.

Storing dates and times as character is plain dumb, to be VERY polite.

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

First of all, look at your dataset xyz; it has one observation with a missing time, and another with date and time.

Remove the OUTPUT statements if you  want to have exactly one observation.

 

The proper way to create a timestamp (datetime) out of a separate time and date is the DHMS function:

data abc;
set xyz;
date1=input(date,anydtdte20.);
time1 = input(time,time8.);
date_time = dhms(date1,0,0,time1);
format date_time e8601dt19.;
run;
 
pdhokriya
Pyrite | Level 9
date1 and time1 are char not num and required date_time would be in same format (char with 20 length).

time1 has blank record in given data , i have kept it intentionally.

( i think dhms function works for num only, plz correct me if i am wrong)
Kurt_Bremser
Super User

In SAS, YOU DO NOT STORE DATES OR TIMES AS CHARACTER.

Any format for display can be used whenever needed (eg for reports or export), but storing such values as character deprives you of the use of date&time functions and formats and necessitates re-conversion whenever a calculation needs to be done.

On top of that all, you need much more space to store a datetime as character (19 characters here) vs. numeric (only 8 bytes). Same for dates (minimum 8 characters without delimiters), which need only 4 numeric bytes.

Storing dates and times as character is plain dumb, to be VERY polite.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 2188 views
  • 2 likes
  • 2 in conversation