BookmarkSubscribeRSS Feed
gzr2mz39
Quartz | Level 8
Is there a date format that creates mm-dd or mm/dd or mmmdd?
For example, 09-10 or 09/10 or Sep10.
Basically I would like to create a date without a year.
Thank you.
12 REPLIES 12
Peter_C
Rhodochrosite | Level 12
you could read through the manual, or on-line doc at the formats by category at http://support.sas.com/documentation/cdl/en/lrdict/63026/HTML/default/viewer.htm#a001263753.htm where date and time formats are documented together

but we have the SAS System, so why not use it to search for you !?

(in)format hunter
as posted here and on SAS-L
adapted for a "time" format search[pre]*********************************************************
* format hunter *
********************************************************;
%let have_time= 23:15:00 ;
%let required = 23:15 ;

data test ;
set sashelp.vformat ; *listed in this data set, are all formats and in formats;
where fmttype='F' ; *want just formats;
where also fmtname ne:'$' ; * and not character formats;
length str $40 ;
str = putn( "&have_time"t, fmtname );

if not _error_ ;
if left(str) = "&required" ;
put fmtname= str= ;
option errors = 0;
run;[/pre]in my log the results presented (among other lines) were

NOTE: Limit set by ERRORS= option reached. Further errors of this type will not be printed.
fmtname=HHMM str=23:15
NOTE: There were 589 observations read from the data set SASHELP.VFORMAT.

The search reveals
fmtname=HHMM str=23:15
So try format HHMM.

It took very little time to adapt and run the "format hunter"

no need to worry about that "NOTE: Limit set by ERRORS= " (this time)
Ksharp
Super User
Hi.
I try many times ,Finally got it.
I really think it is no easy to achieve.

[pre]
data _null_;
x=put('10aug2009'd,mmddyy5.);
put 'NOTE:' x=;
run;
[/pre]
and

[pre]
data _null_;
x=put('10aug2009'd,mmddyyd5.);
put 'NOTE:' x=;
run;
[/pre]

Ksharp Message was edited by: Ksharp
Peter_C
Rhodochrosite | Level 12
> Hi.
> I try many times ,Finally got it.
> I really think it is no easy to achieve.
>
> Message was edited by: Ksharp

are you able to offer a log showing what was 'no easy' ?

Seeking a format to provide the HH and MM,
I thought checking first for a format named HHMM would be really easy!
Ksharp
Super User
Hi.Peter.
I try your code.But it does not find anything.
'not easy' means I try many times and look up the SAS documentation all over.
I also think your code is right. Maybe does not suit this situation.


Regards.

Ksharp
Peter_C
Rhodochrosite | Level 12
Ksharp
thank you
your experience and the posting from ArtC have sent me back to reviewing and improve my "format hunter"
Not yet a macro,( I modify the basic data step each time I'm hunting), the hunter now tries twice and reveals more that one result (at last)[pre]%let required = 23/12 ;
%let have_ = 23Dec2010;

data date_format_hunter_found( keep= fmtname str );
set sashelp.vformat ;
where fmttype='F' ; *just formats;
where also fmtname ne:'$' ; * and not character formats;
retain width %sysfunc( length( %superq(required)) ) ;

str = putn( "&have_"d, fmtname ) ; *default format width ;

if left(str) NE "&required"
and not _error_
and minw LE width LE maxw
then
str = putn( "&have_"d, fmtname, width );
* using requested width ;

if _error_ then put 'NOTE- ' fmtname= ;
_error_=0 ;
if left(str) NE "&required" then delete ;

* report on successes ;
put "&have_ &required " fmtname= str= width= ;
run;[/pre]This version finds formats for dd/mm as follows[pre]23Dec2010 23/12 fmtname=CATDFDD str=23/12 width=5
23Dec2010 23/12 fmtname=CSYDFDD str=23/12 width=5
23Dec2010 23/12 fmtname=DDMMYY str=23/12 width=5
23Dec2010 23/12 fmtname=DDMMYYS str=23/12 width=5
23Dec2010 23/12 fmtname=FRADFDD str=23/12 width=5
NOTE: Argument 2 to function PUTN at line 1256 column 9 is invalid.
fmtname=FREE
23Dec2010 23/12 fmtname=FRSDFDD str=23/12 width=5
23Dec2010 23/12 fmtname=ITADFDD str=23/12 width=5
23Dec2010 23/12 fmtname=PTGDFDD str=23/12 width=5
NOTE: There were 589 observations read [/pre]
For dd-mm layout formats found were[pre]23Dec2010 23-12 fmtname=DDMMYYD str=23-12 width=5
NOTE: Argument 2 to function PUTN at line 1281 column 9 is invalid.
fmtname=FREE
23Dec2010 23-12 fmtname=NLDDFDD str=23-12 width=5
23Dec2010 23-12 fmtname=POLDFDD str=23-12 width=5[/pre] and for mm-dd only one format was revealed[pre]23Dec2010 12-23 fmtname=MMDDYYD str=12-23 width=5[/pre]
Of course, you might prefer to create your own format with PROC FORMAT and the PICTURE statement "directives" documented at http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/a002473467.htm#a000530223 and since the online-doc examples of the PICTURE statement don't demonstrate these date directives, here is one:
proc format ;
picture ksharp_date other = '%d-%m'( datatype= date ) ;
run;
%put demo %sysfunc( today(), ksharp_date. ) ;
which produced the "demo"[pre]1325 %put demo %sysfunc( today(), ksharp_date. ) ;
demo 13-11[/pre]
Ksharp
Super User
Peter.
I really adore your spirit of discovery. 😉


Best Regards.
Ksharp
gzr2mz39
Quartz | Level 8
Thank you.
ArtC
Rhodochrosite | Level 12
The DATE5. format will give a similar result. More on date formats can be found in Derek Morgan's book "The Essential Guide to SAS Dates and Times" https://support.sas.com/pubscat/bookdetails.jsp?pc=59411
Ksharp
Super User
Hi.Art Carpenter.
I have read several books you write, they are pretty good, and learned a lot from them.

Regards.

Ksharp
jcfinck
Calcite | Level 5

It's been a few years since the question was first aske. But using SAS 9.4 , this is fairly simple.

 

DATA DATE4;
FORMAT	DT 		mmddyy10. 
		N4 		mmddyyN4.
		D5 		mmddyyD5.
		S5 		mmddyyS5.
		P5 		mmddyyP5.
		DATE5	DATE5.;
DT=TODAY();
N4=DT;
D5=DT;
S5=DT;
P5=DT;
DATE5=DT;
	PUT DT=;
	PUT N4=;
	PUT S5=;
	PUT P5=;
	PUT DATE5=;
RUN;

That code produces this output in the log:

 

DT=09/07/2017
N4=0907
S5=09/07
P5=09.07
DATE5=07SEP
NOTE: The data set WORK.DATE4 has 1 observations and 6 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds

 

You could do the same thing with PUT Statements, if you want the values to show up in a character vs. date format.

vhickin64
Calcite | Level 5
I highly recommend this book - The most used of all my SAS books.
Morgan's book "The Essential Guide to SAS Dates and Times"
It shows on page 21
Format Name Result Comment
DDMMYY5. 26/05
DDMMYY6. 260502


MMDDYY5.
vhickin64
Calcite | Level 5
Sorry it posted before I finished typing
I change the order to MMDDY5.
Which give me reverse 05/26

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 12 replies
  • 8569 views
  • 1 like
  • 6 in conversation