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

Hello,

 

Please help me to change the numeric format, DATETIME22.3 (12JAN2023:18:05:44.700) to mmddyy10 (10/12/2023).   Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Your variable that is formatted as DATETIME22.3 is a Date/Time variable, which is internally the number of second between midnight at the start of 01JAN1960. If you want it as MMDDYY10, this would have to be a Date variable which is internally the number of days since 01JAN1960. You cannot use a Date/Time variable. So you need to create a new variable that is Date.

 

data want;
    set have;
    newvariable=datepart(oldvariable);
    format newvariable mmddyy10.;
run;

 

Note: If you don't insist on mmddyy10, but could live with a date formatted as 01JAN1960, then you just need to assign the DTDATE9. format, you would not need to create a new variable.

 

data want;
    set have;
    format oldvariable dtdate9.;
run;

 

 

or even better

 

proc datasets library=work nolist;
    modify have;
    format oldvariable dtdate9.;
run; quit;
--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

Your variable that is formatted as DATETIME22.3 is a Date/Time variable, which is internally the number of second between midnight at the start of 01JAN1960. If you want it as MMDDYY10, this would have to be a Date variable which is internally the number of days since 01JAN1960. You cannot use a Date/Time variable. So you need to create a new variable that is Date.

 

data want;
    set have;
    newvariable=datepart(oldvariable);
    format newvariable mmddyy10.;
run;

 

Note: If you don't insist on mmddyy10, but could live with a date formatted as 01JAN1960, then you just need to assign the DTDATE9. format, you would not need to create a new variable.

 

data want;
    set have;
    format oldvariable dtdate9.;
run;

 

 

or even better

 

proc datasets library=work nolist;
    modify have;
    format oldvariable dtdate9.;
run; quit;
--
Paige Miller
ballardw
Super User

If you want to use a DATETIME value show a date type appearance SAS did not provide then roll your own format. Proc Format has a bunch of directives to use with the PICTURE statement to show just about any element of a date, time or datetime value you may reasonably want.

 

This example creates a DT_mmddyy format analogous to the SAS supplied DTDATE format.

proc format library=work;
picture dt_mmddyy (default=10)
   low-high = '%0m/%0d/%Y'   (datatype=datetime);
run;

data example;
   x=dhms(today(),0,0,time());
   format x dt_mmddyy.;
run;

Note that picture statements are one place that you pretty much need to use the single quotes otherwise the directives will be treated as macro references and cause errors.

You did not specify that the month and day should be preceded by zero when single digits. To closer appear like MMDDYY10 I used the 0s. The / and other characters would be literals. So if you want 10-12-2023 (dashes between bits) use the - character in the directive list. Or a space, comma, decimal or drive people nuts with ^ or < as separators.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 2 replies
  • 419 views
  • 2 likes
  • 3 in conversation