BookmarkSubscribeRSS Feed
Mscarboncopy
Pyrite | Level 9

Hello,

 

Is there a way to apply this date format without having multiple rows of the same statement below? I will have hundreds of different dates and I need them to have this format so that I can further work with them .
DATE ='27AUG25:09:00:00'dt;
 
ID         DATE
1         27JUL25:09:00:00 
2         05AUG25:10:00:00
3         15AUG25:08:00:00
4         20AUG25:11:30:00
...
 
Thanks!
7 REPLIES 7
Cynthia_sas
Diamond | Level 26

Hi:

  I'm not sure what you want. Remember that a SAS date value is stored internally as a number and a SAS date/time value is also stored internally as a number. The format that you show is only how the variable is displayed in output, such as reports. Without any format being applied, you can prove this to yourself by running this program:

Cynthia_sas_1-1756330885492.jpeg

  That very large number that is stored internally represents the number of seconds between midnight on Jan 1 1960 and your date/time value. If you already have date/time variables stored in your data, then your assignment statement is NOT applying a format. If you want to apply a format to change the way your variable is displayed, you would use a FORMAT statement or  for the PUTLOG statement, you can specify the format after the variable to be displayed in the log. Note the difference between a format with a width of 20 and the format with a width of 18 -- that will give you the 2 digit year you seem to want. Just remember that your assignment statement is NOT storing your date/time value in a readable format --your numeric date/time value is ALWAYS stored as a number.

 

  Perhaps if you could explain a bit more about your data and what you need to do, it would be easier to understand why you think the assignment statement is necessary.

Cynthia

 

Tom
Super User Tom
Super User

There are two ways to produce that type of a listing.

 

If your variable named DATE has DATETIME values with the DATETIME format attached to it with a width between 16 and 18.  NOTE: the DATETIME format has a bug and you must specify at width of at least 19 for it to actually print the 18 characters needed to display datetime value with 4 digit years.

data have;
  input id date :datetime.;
  format date datetime16.;
cards;
1 27JUL25:09:00:00 
2 05AUG25:10:00:00
3 15AUG25:08:00:00
4 20AUG25:11:30:00
;

But then I do not know what you are asking for help with since you already have the value as a datetime.  If the variable is datetime already what is your actual question?

 

But perhaps instead you have the DATE variable defined as CHARACTER instead.

data have;
  input id date :$16.;
cards;
1 27JUL25:09:00:00 
2 05AUG25:10:00:00
3 15AUG25:08:00:00
4 20AUG25:11:30:00
;

Both of those will print the same way:

Tom_0-1756336519004.png

 

In that case to work with those values as actual datetime values you will need to first convert them.  And you will need to make a new variable since the variable DATE is already defined as a character variable.

data want;
  set have;
  datetime = input(date,datetime24.);
  format datetime datetime19.;
run;

Result:

Tom_1-1756336554025.png

Note it does not matter if you have 4 observations or hundreds or millions the code is the same.

Mscarboncopy
Pyrite | Level 9

@Tom @Cynthia_sas Sorry,  I should have added what I want to do with it.

I want to break the date var into several other vars (See outcome below).

It looks like I need the Date var to be formatted the way I asked in my previous message for this next part of my code to work.

 

proc format;
  value dayofweek
    1 = 'Sunday'
    2 = 'Monday'
    3 = 'Tuesday'
    4 = 'Wednesday'
    5 = 'Thursday'
    6 = 'Friday'
    7 = 'Saturday';
run;
data final;
  set test;
  dated = datepart(Date);
  time = timepart(Date);
  day_of_week = weekday(dated);
  month = month(dated);
  day = day(dated);
  year = year(dated);
  hour = hour(time);
  minute = minute(time);
  second = second(time);
format date date9.;
format time timeampm.;
format day_of_week dayofweek.;
run;
 
Result of this (showing only one obs) 
dated                     time                day_of_week month day year   hour minute
27AUG2025
9:00:00 AM
Wednesday
8
27
2025
9
0

 

The idea here is to use these variables elsewhere to show the following:

Wednesday, August 27, 2025 9:00 AM  

Aside from figuring out how to apply that format to hundreds of dates, I still need to:

1. label the Month to display the string not the numeric value 8 like I did for day_of_week.

2. remove the extra 00 in my time variable. 

3. I do not need the hour and the minute vars but I created them just in case.

I do not know another way to achieve this output unless I format my Date var the way I showed.

My date variable is the long SAS number which I am converting to a date format (the one you see in my cards)

 

Thank you!

 

Tom
Super User Tom
Super User

Why do you need to make so many new variables?

Why not just apply different formats for different levels of granularity?

 

You can get very close to that desired output using a PICTURE format. 

data have;
  input id date :datetime.;
  format date datetime16.;
cards;
1 27JUL25:09:00:00
2 05AUG25:10:00:00
3 15AUG25:08:00:00
4 20AUG25:21:30:00
;

/*
Wednesday, August 27, 2025 9:00 AM  
*/
proc format ;
 picture mydt (default=40)
 low-high= '%F, %C %d, %Y %H:%0M %p' (datatype=datetime);
;
run;

proc print data=have;
 format date mydt.;
run;

Result:

Obs    id                     date

 1      1     Sunday   , July      27, 2025 9:00 AM
 2      2     Tuesday  , August    5, 2025 10:00 AM
 3      3     Friday   , August    15, 2025 8:00 AM
 4      4    Wednesday, August    20, 2025 21:30 PM

You might want to run it though the COMPBL() function however.

data want;
  set have;
  string = left(compbl(put(date,mydt.)));
run;

Result

Obs    id                date                  string

 1      1    27JUL25:09:00:00    Sunday , July 27, 2025 9:00 AM
 2      2    05AUG25:10:00:00    Tuesday , August 5, 2025 10:00 AM
 3      3    15AUG25:08:00:00    Friday , August 15, 2025 8:00 AM
 4      4    20AUG25:21:30:00    Wednesday, August 20, 2025 21:30 PM

Might need to add a TRANWRD() function call also.

  string = tranwrd(left(compbl(put(date,mydt.))),' ,',',');
Ksharp
Super User

Tom,

There is a problem in your output, was it right ?

 1      1    27JUL25:09:00:00    Sunday , July 27, 2025 9:00 AM
 2      2    05AUG25:10:00:00    Tuesday , August 5, 2025 10:00 AM
 3      3    15AUG25:08:00:00    Friday , August 15, 2025 8:00 AM
 4      4    20AUG25:21:30:00    Wednesday, August 20, 2025 21:30 PM

Anyway , there is another way to get this format.


proc fcmp outlib=work.math.func;
function fmt(datetime) $;
length fmt $ 60;
fmt=catx(' ',put(datepart(datetime),weekdate32.),put(timepart(datetime),timeampm8.));
return (fmt);
endsub;
run;
options cmplib=work.math;
proc format;
value fmt(default=60)
other=[fmt()]
;
run;

data have;
  input id date :datetime.;
  format date fmt.;
cards;
1 27JUL25:09:00:00
2 05AUG25:10:00:00
3 15AUG25:08:00:00
4 20AUG25:21:30:00
;

proc print data=have;
run;
Tom
Super User Tom
Super User

Perhaps this is what you are trying to do?

First let's name you variable that has DATETIME values as DATETIME instead of DATE to avoid confusion in our example.  And display it with 4 digit years for the same reason.

data have;
  input id datetime :datetime.;
  format datetime datetime19.;
cards;
1 27JUL25:09:00:00
2 05AUG25:10:00:00
3 15AUG25:08:00:00
4 20AUG25:21:30:00
;

Now we can make that complex string using the CATX() and PUT functions.  It does make the code simpler to first make a variable with just the DATE (number of days since 1960).

data want;
  set have;
  length string $50;
  date=datepart(datetime);
  format date date9.;
  string = catx(', '
           ,put(date,downame.)
           ,catx(' ',put(date,monname.),day(date))
           ,put(datetime,timeampm.));
run;

Result

Obs    id               datetime                 string                      date

 1      1     27JUL2025:09:00:00    Sunday, July 27, 9:00:00 AM         27JUL2025
 2      2     05AUG2025:10:00:00    Tuesday, August 5, 10:00:00 AM      05AUG2025
 3      3     15AUG2025:08:00:00    Friday, August 15, 8:00:00 AM       15AUG2025
 4      4     20AUG2025:21:30:00    Wednesday, August 20, 9:30:00 PM    20AUG2025

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 7 replies
  • 3015 views
  • 3 likes
  • 4 in conversation