BookmarkSubscribeRSS Feed
omega1983
Calcite | Level 5

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

16 REPLIES 16
Sylas
Fluorite | Level 6

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

Linlin
Lapis Lazuli | Level 10

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

Sylas
Fluorite | Level 6

Yes Linlin,

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

Peter_C
Rhodochrosite | Level 12

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

Peter_C
Rhodochrosite | Level 12


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

Bill_
Fluorite | Level 6

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.

Tom
Super User Tom
Super User

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

Linlin
Lapis Lazuli | Level 10

Thank you Tom! Your code is much better.

robertrao
Quartz | Level 8

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

Tom
Super User Tom
Super User

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.

robertrao
Quartz | Level 8

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

Regards

Tom
Super User Tom
Super User

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

robertrao
Quartz | Level 8

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

Tom
Super User Tom
Super User

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'.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 16 replies
  • 8375 views
  • 9 likes
  • 9 in conversation