Solved
Contributor
Posts: 58

# How to create leading zeros in a macro?

In a macro I am using the code
------------------------------

%SYSFUNC(catx(_,&number ,%SYSFUNC(Intnx(qtr, &dvar, 0, end), DDMMYYP10.)))

-----------------------------

to create the output

-----------------------------

5_30.06.2013

-----------------------------

How can I create a dynamic leading zero?

Accepted Solutions
Solution
‎05-22-2012 01:22 PM
Posts: 3,852

## Re: How to create leading zeros in a macro?

Looks like CATX is treating the number like a number.  You don't need CATX.

405 %let number=5;

406 %let dvar=%sysfunc(today());

407 %PUT NOTE: %sysfunc(putN(&number,z2))_%SYSFUNC(Intnx(qtr, &dvar, 0, end), DDMMYYP10.);

NOTE: 05_30.06.2012

All Replies
Super User
Posts: 6,785

## Re: How to create leading zeros in a macro?

Try replacing &number with:

%sysfunc(putn(&number, z2))

Messy, but it should work.

This assumes that &number is always less than 100, and you only want a leading zero when &number < 10.

Contributor
Posts: 58

## Re: How to create leading zeros in a macro?

I tried exactly that, but it does not work :-(

Super User
Posts: 6,785

## Re: How to create leading zeros in a macro?

OK, you actually made me test my code.  It does work for me.  There are subtle parts of the syntax, such as PUTN instead of PUT, and no period at the end of the format name.  Can you post what you coded, and the results?

This is what worked:

%let n=5;

%let newn = %sysfunc(putn(&n, z2));

Solution
‎05-22-2012 01:22 PM
Posts: 3,852

## Re: How to create leading zeros in a macro?

Looks like CATX is treating the number like a number.  You don't need CATX.

405 %let number=5;

406 %let dvar=%sysfunc(today());

407 %PUT NOTE: %sysfunc(putN(&number,z2))_%SYSFUNC(Intnx(qtr, &dvar, 0, end), DDMMYYP10.);

NOTE: 05_30.06.2012

Contributor
Posts: 58

## Re: How to create leading zeros in a macro?

Ok, cool, thx! It works perfectly without catx... :-)

🔒 This topic is solved and locked.