DATA Step, Macro, Functions and more

Formatting counter in SAS macro

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

Formatting counter in SAS macro

I have a series of files that I wish to concatenate ... named file01, file02, file03 ... file12.

If I use the code

set

%do i=01 to 12;

file&i;

%end;

;

the files are named file1, file2, ... file12.

How can I force the macro to use the 2-digit (zero-filled) form of the counter?

Thanks!


Accepted Solutions
Solution
‎04-08-2014 01:16 PM
Super User
Super User
Posts: 7,072

Re: Formatting counter in SAS macro

You can replace the value of I with a value with a leading zero inside the loop and it will not break the looping.  You can then use &I where you need it.

%macro test ;

%do i=1 %to 10 ;

  %let i=%sysfunc(putn(&i,Z2));

  %put i=&i ;

%end;

%mend ;

%test;

View solution in original post


All Replies
Trusted Advisor
Posts: 3,215

Re: Formatting counter in SAS macro

https://communities.sas.com/message/127712

---->-- ja karman --<-----
Solution
‎04-08-2014 01:16 PM
Super User
Super User
Posts: 7,072

Re: Formatting counter in SAS macro

You can replace the value of I with a value with a leading zero inside the loop and it will not break the looping.  You can then use &I where you need it.

%macro test ;

%do i=1 %to 10 ;

  %let i=%sysfunc(putn(&i,Z2));

  %put i=&i ;

%end;

%mend ;

%test;

Occasional Contributor
Posts: 7

Re: Formatting counter in SAS macro

Thanks to both Jaap and Tom ... I'm in business!

🔒 This topic is solved and locked.

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

Discussion stats
  • 3 replies
  • 349 views
  • 0 likes
  • 3 in conversation