DATA Step, Macro, Functions and more

How to create leading zeros in a macro?

Accepted Solution Solved
Reply
Contributor
Posts: 52
Accepted Solution

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
Respected Advisor
Posts: 3,777

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

View solution in original post


All Replies
Super User
Posts: 5,082

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: 52

Re: How to create leading zeros in a macro?

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

Super User
Posts: 5,082

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
Respected Advisor
Posts: 3,777

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: 52

Re: How to create leading zeros in a macro?

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

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 2795 views
  • 0 likes
  • 3 in conversation