BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
hhchenfx
Barite | Level 11

Hi All,

My data has a date column and time column.

I would like to create a column datetime with format yyyy-mm-dd hh:mm:ss.ms and export to csv.

Clearly, my time doesn't has millisecond and 00000 is fine.

Can you please help me to get the code?

Thanks,

HHC

data have;
input date time;
informat time time11.;
format date date9. time time11.;
datalines;

20130730 4:00:00
20130130 5:15:00
run;

 

 

proc format;
picture mydt   (default=24)
low-high='%Y-%0m-%0d %0H:%0M:%0S.0000' (datatype=datetime)
;
run;

data have;
   input date time;
   informat date yymmdd10. time time11.;
   format date date9. time time11.;
datalines;
20130730 4:00:00
20130130 5:15:00
run;

data want; set have;
   dt = dhms(date,0,0,time);
   format dt mydt.;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

See if this gets you started:

proc format;
picture mydt   (default=23)
low-high='%Y-%0m-%0d %0H:%0M:%0S.000' (datatype=datetime)
;
run;

data have;
   input date time;
   informat date yymmdd10. time time11.;
   format date date9. time time11.;
   dt = dhms(date,0,0,time);
   format dt mydt.;
datalines;
20130730 4:00:00
20130130 5:15:00
run;

 

What is going on: Read proc format documentation on the Picture statement. Your desired datetime appearance is kind of non-standard so you have to roll your own with proc format. The MYdt format will display any fractional seconds as .000 so if you actually have values to display good luck. The directives in the Picture statement, those things starting with % are case sensitive so read the documentation. This is one of the few places that the quotes need to be single quotes around the string of directives. If you use double quotes you will get a bunch of errors from the macro language processor. The characters other than the %, directive letter and 0 in between are all displayed literally.

 

Note that you have a choice between a 24 hour clock, used, and 12 hour. Also the choices shown place a 0 before some of the values when they would be less than 10, so you get 2022-08-03 for the date portion instead of 2022-8-3 and similar for the hour, minute and seconds.

 

There is a similar format supplied by SAS, E8601DTw.d but it places a T between the date and time portions instead of a space.

 

The code also shows how to read shown values as the believed date.

 

 

View solution in original post

3 REPLIES 3
ballardw
Super User

See if this gets you started:

proc format;
picture mydt   (default=23)
low-high='%Y-%0m-%0d %0H:%0M:%0S.000' (datatype=datetime)
;
run;

data have;
   input date time;
   informat date yymmdd10. time time11.;
   format date date9. time time11.;
   dt = dhms(date,0,0,time);
   format dt mydt.;
datalines;
20130730 4:00:00
20130130 5:15:00
run;

 

What is going on: Read proc format documentation on the Picture statement. Your desired datetime appearance is kind of non-standard so you have to roll your own with proc format. The MYdt format will display any fractional seconds as .000 so if you actually have values to display good luck. The directives in the Picture statement, those things starting with % are case sensitive so read the documentation. This is one of the few places that the quotes need to be single quotes around the string of directives. If you use double quotes you will get a bunch of errors from the macro language processor. The characters other than the %, directive letter and 0 in between are all displayed literally.

 

Note that you have a choice between a 24 hour clock, used, and 12 hour. Also the choices shown place a 0 before some of the values when they would be less than 10, so you get 2022-08-03 for the date portion instead of 2022-8-3 and similar for the hour, minute and seconds.

 

There is a similar format supplied by SAS, E8601DTw.d but it places a T between the date and time portions instead of a space.

 

The code also shows how to read shown values as the believed date.

 

 

hhchenfx
Barite | Level 11
Great post, @ballardw !
Thank you,
HHC

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 945 views
  • 0 likes
  • 3 in conversation