BookmarkSubscribeRSS Feed
swioak
Fluorite | Level 6

I have a date var and I'm trying to create a new var with a format like: 10/02/2023 Mon.  I thought the following would work, but the format for the vars are not kept in the new var.  Concatenating the vars keeps the original numeric format. 

RECEIVE_DT=DATEPART(SPECIMEN_RECV_TIME);
FORMAT RECEIVE_DT MMDDYY10.;
DAYOFWEEK=WEEKDAY(RECEIVE_DT);
FORMAT DAYOFWEEK WEEKDATE3.;
REC_DT=RECEIVE_DT||DAYOFWEEK;

10 REPLIES 10
PaigeMiller
Diamond | Level 26

Please show us:

  1. A typical value of this variable SPECIMEN_RECV_TIME as seen in PROC PRINT or viewing it in SAS.
  2. PROC CONTENTS output about your date variable named SPECIMEN_RECV_TIME.

 

That's the only way we can really know what what type of variable this is and how it is formatted.

--
Paige Miller
ballardw
Super User

Let's parse your data manipulation:

DAYOFWEEK=WEEKDAY(RECEIVE_DT);

The Weekday function from the online help:

Details

The WEEKDAY function produces an integer that represents the day of the week, where 1=Sunday, 2=Monday, ..., 7=Saturday.

Dates are the number of days from 1 Jan 1960. So you apply a format that expects numeric values from about -138061 to  6588970 to a value that only is 1 to 7. So when apply a date format to the value then SAS thinks you want the date from 01JAN1960 to 07Jan1960. Which are very likely not to match the days of the week for about 6 of 7 days of the week.

 

How about showing what you actually want to display? It may be easier to create a custom format than to write fragile code and manipulate values.

 

This shows an example of creating a format to use with datetime values directly to create mm/yy/dd with a 3 letter abbreviation of the day of the week and another to use a date value.

proc format;
picture mydatetime_dow
low-high ='%0m/%0d/%Y %3a' (datatype=datetime);
picture mymmddyy_dow
low-high ='%0m/%0d/%Y %3a' (datatype=date);
run;

data example;
   x= dhms(today(),0,0,time());
   put x= mydatetime_dow.;  
   y=today();
   put y= mymmddyy_dow.;
run;
swioak
Fluorite | Level 6

My new var, REC_DT became a character var.  I'd like to format this variable as 10/02/2013 Mon or 10/02/2023 (Mon)

PaigeMiller
Diamond | Level 26

@ballardw wrote:

 

How about showing what you actually want to display? It may be easier to create a custom format than to write fragile code and manipulate values.


Agreeing with @ballardw , are not the built-in formats sufficient, wouldn't it be acceptable and equally understandable to use the DTWKDATX format or the WEEKDATX format, which produce dates like this: 

 

Wednesday, 28 March 2018 

--
Paige Miller
swioak
Fluorite | Level 6

Yes, the WEEKDATX. format would work, but I'm trying to customize my date/day of the week format to look like:

10/02/2023 Mon or 10/02/2023 (Mon)

ballardw
Super User

@swioak wrote:

Yes, the WEEKDATX. format would work, but I'm trying to customize my date/day of the week format to look like:

10/02/2023 Mon or 10/02/2023 (Mon)


Run the Proc Format code in my previous post.

Use the MYdatetime_dow format with your datetime variable. No need to create anything.

If you really want () around the day of the week place the ( ) around the %3a in the format picture statement. WARNING: These directives in the picture statement are case sensitive. %a is abbreviated day of week (defaults to 2 characters, the %3a says use 3 characters), %A is the full name of the weekday. Mix up %m and %M and you can end up with minutes instead of months. The zeroes mean that single digit month or day values will appear in the format with a leading zero  09/03/2023 for Sep 3 2023.

PaigeMiller
Diamond | Level 26

@swioak wrote:

Yes, the WEEKDATX. format would work, but I'm trying to customize my date/day of the week format to look like:

10/02/2023 Mon or 10/02/2023 (Mon)


Great, but you did not provide the two pieces of information which I asked for earlier. Until we have that information, we cannot give definitive answers.

--
Paige Miller
swioak
Fluorite | Level 6

My apologies.  

specimen_recv_time as from proc print

swioak_0-1696285771007.png

from proc contents:

swioak_1-1696285850691.png

 

Tom
Super User Tom
Super User

So your variable does not contain DATE values.  It has DATETIME values.

data want;
  set have;
  length string $16 ;
  string=catx(' ',put(datepart(specimen_recv_time),mmddyy10.)
        ,cats('(',put(datepart(specimen_recv_time),downame3.),')')
        )
  ;
run;

Example:

Obs     specimen_recv_time         string

 1      02OCT2023:20:11:03    10/02/2023 (Mon)

PS Displaying dates in MDY order will confuse 50% of your audience, plus the strings will not sort in chronological order.  Better to use YMD order.  Plus you can generate a date string in YMD order directly from a datetime value.

  string=catx(' ',put(specimen_recv_time,e8601dn10.)
        ,cats('(',put(datepart(specimen_recv_time),downame3.),')')
        )
  ;
Obs     specimen_recv_time         string

 1      02OCT2023:20:15:31    2023-10-02 (Mon)

 

Patrick
Opal | Level 21

@ballardw already provided a working solution "long ago". What prevents you from using it?

proc format;
  picture mydatetime_dow
    low-high ='%0m/%0d/%Y %3a' (datatype=datetime)
    ;
run;

data example;
  specimen_recv_time=datetime();
  specimen_recv_time_2=datetime();
  format specimen_recv_time_2 mydatetime_dow.;
run;

proc print data=example;
  format specimen_recv_time mydatetime_dow.;
run;

proc contents data=example;
run;

Patrick_0-1696298606111.pngPatrick_1-1696298628961.png

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 10 replies
  • 2220 views
  • 0 likes
  • 5 in conversation