Help using Base SAS procedures

Formatting dates in SAS

Reply
Contributor
Posts: 59

Formatting dates in SAS

I have a variable date called mydate

format mydate mmddyy10.;

sample output 03/04/2013

11/03/2012

04/11/2012

I want to supress the month and day if there is a leading zero

(ie 3/4/2013

11/3/2012

4/11/2012

Occasional Contributor
Posts: 17

Re: Formatting dates in SAS

Posted in reply to omega1983

Hi,

Another way of doing it,

data sample;
input dt MMDDYY10. ;
datalines ;
11/03/2012
04/11/2012
;
run;

data sample1;
set sample;
dt1=compress(tranwrd(put(dt,MMDDYY10.),'0',''));
run;

Regards

Sylas.J

Super Contributor
Posts: 1,636

Re: Formatting dates in SAS

what about the '0' in year '2012'?

Occasional Contributor
Posts: 17

Re: Formatting dates in SAS

Yes Linlin,

My code won't handle the year, your code is correct and best to use.

Valued Guide
Posts: 2,177

Re: Formatting dates in SAS

Posted in reply to omega1983

have a look at the PICTURE statement of the FORMAT procedure.

That allows you to define the layout of your choice. The month and day can be defined to appear without a leading zero.

The link is to the PICTURE statement http://support.sas.com/documentation/cdl/en/proc/65145/HTML/default/p0n990vq8gxca6n1vnsracr6jp2c.htm

once there look down the webpage until you find the directives

I think you will want something like

     picture mydate low-high = '%m/%d/%Y'( datatype= date) ;

used like

    %put %sysfunc( today(), mydate ) ;

or

   stringdate = put( sas_date, mydate10. ) ;

but first read the doc and see what examples it provides

good luck

peterC

Valued Guide
Posts: 2,177

Re: Formatting dates in SAS


here is the user format approach, to work in any context

37   proc format ;
38   picture mydate low-high = '%m/%d/%Y'( datatype= date) ;

NOTE: Format MYDATE has been output.
39   run;

NOTE: PROCEDURE FORMAT used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


40
41   data new;
42   informat mydate mmddyy10.;
43   input mydate ;
44   format mydate mydate.;
45   put mydate= date9. mydate= ;
46   datalines;

mydate=04MAR2013 mydate=3/4/2013
mydate=03NOV2012 mydate=11/3/201
mydate=11APR2012 mydate=4/11/201
mydate=01JAN2011 mydate=1/1/2011
mydate=09MAY2009 mydate=5/9/2009
NOTE: The data set WORK.NEW has 5

New Contributor
Posts: 4

Re: Formatting dates in SAS

[ Edited ]

The Picture Format works very well, and it leaves the data as a SAS date for functions and calculations. Just don't forget to add the 10 when you use it or your dates will drop the last few digits. The digits get dropped from right so 11/11/2011 may become 11/11/20 on output if you do not set the length to 10.

Super User
Super User
Posts: 7,054

Re: Formatting dates in SAS

Posted in reply to omega1983

newdate=catx('/',month(date),day(date),year(date));

Super Contributor
Posts: 1,636

Re: Formatting dates in SAS

Thank you Tom! Your code is much better.

Super Contributor
Posts: 1,041

Re: Formatting dates in SAS

Hi,

CATX is used for removing trailing and leading spaces as well as to insert DELIMITERS....

But in this case it is removing leading ZEROS....

So my question is :

IS ZERO considered LEADING OR TRAILING SPACE???????

Regards

Super User
Super User
Posts: 7,054

Re: Formatting dates in SAS

Posted in reply to robertrao

catx is silently converting the numbers to strings. It is not adding any leading zeros. So numbers less than 10 will be only one digit.

The zeros are never generated, so CATX() is not removing any zeros.

Super Contributor
Posts: 1,041

Re: Formatting dates in SAS

Could you please elaborate it Tom....I still dint get it

Regards

Super User
Super User
Posts: 7,054

Re: Formatting dates in SAS

Posted in reply to robertrao

Consider this little program.

Note that even if tell SAS that Y should be displayed with leading zeros when CATX() still converts without using the Z format.

Note that C which is a character variable and hence does not undergo any conversion keeps all of its characters.

data _null_;

    x=1 ; y=2; c='   09' ;

    format y z3.;

    s=catx('/',x,y,c);

    put (_all_) (=);

run;

x=1 y=002 c=09 s=1/2/09

Super Contributor
Posts: 1,041

Re: Formatting dates in SAS

Hi,

If I am not wrong........

if a numeric value of 000900 in SAS is converted to char value using CATX.....its value will be '900'

ALSO

05/09/2009    is converted to 5/9/2009. if it was 09 instead of 2009 then is this correct???

05/09/09 is converted to 5/9/9

Super User
Super User
Posts: 7,054

Re: Formatting dates in SAS

Posted in reply to robertrao

To understand what the CATX() function is doing do not think about it is dates.  Also there is no such thing as a numeric value of 000900, at least not one that is different than 900 or any other way of expressing the number nine hundred ( 9*100, 1000-100, '384'x etc.).  So  in this statement

newdate=catx('/',month(date),day(date),year(date));

CATX is operating on three numbers.  They happen to be a month, a day and a year, but CATX() doesn't know that (or care).

So if DATE='09MAY2009'd then CATX receives the numbers 5,9 and 2009.

It converts them to the strings '5','9', and '2009'.

Then it sticks slashes between them to generate the final string '5/9/2009'.

Ask a Question
Discussion stats
  • 15 replies
  • 2118 views
  • 5 likes
  • 8 in conversation