DATA Step, Macro, Functions and more

date format mm-dd

Reply
Regular Contributor
Posts: 199

date format mm-dd

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.
Valued Guide
Posts: 2,177

Re: date format mm-dd

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)
Super User
Posts: 10,023

Re: date format mm-dd

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
Valued Guide
Posts: 2,177

Re: date format mm-dd

> 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!
Super User
Posts: 10,023

Re: date format mm-dd

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
Valued Guide
Posts: 2,177

Re: date format mm-dd

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]
Super User
Posts: 10,023

Re: date format mm-dd

Peter.
I really adore your spirit of discovery. ;-)


Best Regards.
Ksharp
Regular Contributor
Posts: 199

Re: date format mm-dd

Thank you.
Valued Guide
Posts: 634

Re: date format mm-dd

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
Super User
Posts: 10,023

Re: date format mm-dd

Hi.Art Carpenter.
I have read several books you write, they are pretty good, and learned a lot from them.

Regards.

Ksharp
New Contributor
Posts: 2

Re: date format mm-dd

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.

Ask a Question
Discussion stats
  • 10 replies
  • 2095 views
  • 0 likes
  • 5 in conversation