BookmarkSubscribeRSS Feed
lkujawa
Calcite | Level 5

Hello,

 

I have date fields in my output SAS dataset, which are formatted as MMDDYY10. (i.e., 07/15/2021).  I need to be able to find a way to do a final format of these fields to a date format similar to MMDDYY10., but with the leading zeros suppressed for one-digit months and one-digit days (i.e., 7/15/2021 and 7/4/2021).  I've looked at the COMPRESS function, and that doesn't seem to work.  I used the MONTH, DAY, and YEAR functions to isolate those components and tried to concatenate them together via CATX.  The result is a text version of the date, but if I go to convert that to a date format, I'll run into the same issue with MMDDYY10. as before.

 

Is there a way to concatenate numbers with a delimiter, such as "/"?  Otherwise, is there a better alternative for solving the issue?  Please let me know.  Thank you! 

9 REPLIES 9
PaigeMiller
Diamond | Level 26

I think the best solution is to accept the leading zeros. It certainly is the easiest solution.

 

Why do you want leading zeros removed?

--
Paige Miller
lkujawa
Calcite | Level 5

The data is going to a specific customer, and the customer specs are very rigid.

lkujawa
Calcite | Level 5

Hi Reeza,

 

This looks very promising!  I will experiment with it and let you know how it works out.

 

Thank you,

lkujawa

ballardw
Super User

Roll your custom format with proc format

proc format;
picture mydate (default=10)
low-high ='%m/%d/%Y' (datatype=date);
run;

data junk;
   x= '02Jul2021'd;
   put x mydate.;
run;

The directives, those values with % such as %m %d %Y are case sensitive. Example using %M would mean "minutes" instead of month number, %y would mean year without the century (i.e. 2 digits). Also this is one place to use single quote ' instead of double quote " . With " the macro processor will think you are calling macros.

The / are literal characters that appear in the value. Change to - if you want a dash or other separator character.

 

Personally I would almost never use a format without the leading 0 as sort order of some output is going to look strange (October may come after January because 10 will come before 2/ for February)

lkujawa
Calcite | Level 5

I've been testing the logic, and the picture format as mentioned in both your reply and Reeza's works.  I have one issue though.  When I apply the PROC REPORT against the existing dataset, I get the date in the correct format and it can be right-aligned, but I still need the data to be in a raw dataset, not a formatted report.  If I use your logic in the DATA step and try to put that format against one of the existing date fields, it will put the data in a left-aligned character format.  Is there a way to resolve this?  Please advise.  Thanks!

Tom
Super User Tom
Super User

@lkujawa wrote:

I've been testing the logic, and the picture format as mentioned in both your reply and Reeza's works.  I have one issue though.  When I apply the PROC REPORT against the existing dataset, I get the date in the correct format and it can be right-aligned, but I still need the data to be in a raw dataset, not a formatted report.  If I use your logic in the DATA step and try to put that format against one of the existing date fields, it will put the data in a left-aligned character format.  Is there a way to resolve this?  Please advise.  Thanks!


I cannot figure out what you mean.   What is a "raw dataset"?  Do you mean a SAS dataset?  SAS only has two data types, fixed length character strings and floating point numbers.  So your choices are a numeric date field with custom format for display or a character variable.  You could produce the values in the character field as right aligned within the 10 bytes needed to hold any possible valid date.

lkujawa
Calcite | Level 5

I do mean a SAS dataset.  I know that using PROC REPORT along with picture can produce the date in the right format without the leading zeros for month and day and also right-aligned, but can that then be translated back to the actual SAS dataset in a numeric date format?  I know that one of the contributors posted sample logic using picture and a SAS dataset.  However, in that logic, the sample dataset had hard-coded date information.  I have a SAS dataset that already has a date formatted as DATE9.  If I try to use the picture logic to create a new user-defined format and then try to apply it to the already-formatted date, it doesn't seem to work.

Tom
Super User Tom
Super User

My first response would be ask them not just why the zeros matters, but also why they would want to display dates in a style that will confuse half of their audience?  Is your second date supposed to be July fourth or the seventh of April?

 

CATX() is the function that concatenates with a delimiter.

length datestr $10;
datestr=catx('/',month(date),day(date),year(date));

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
  • 9 replies
  • 4082 views
  • 3 likes
  • 5 in conversation