Home > database >  How to add leading zeros to macro variable?
How to add leading zeros to macro variable?

Time:09-30

I am trying to make a loop to do the same query on a set of tables with one table a year, from 2008 to 2020:

%macro sqlloop(start,end);
%DO year=&start. %TO &end.;

PROC SQL;
 CREATE TABLE WORK.RESULT_&year
 SELECT DISTINC ID
 FROM YEAR_&year.
QUIT;

%END;
%mend;

%sqlloop(start=8, end=20)

The issue is that I need a leading zero for the 2008 and 2009 tables, because their names are : YEAR_08, YEAR_09, YEAR_10...

CodePudding user response:

Use the Z format to generate numbers with leading zeros.

%do year=8 to 20;
   %let year=%sysfunc(putn(&year,Z2.));
   ...
  • Related