moodymudskipper
moodymudskipper

Reputation: 47350

blanks inserted by do loops in macros

I'm an absolute beginner in SAS, just started learning about macros, this example comes from the macro language reference of SAS 9.3 (p9) :

%macro names(name= ,number= );
  %do n=1 %to &number;
    &name&n
  %end;
%mend names;

[... ]

data %names(name=dsn,number=5);

Submitting this statement produces the following complete DATA statement:

data dsn1 dsn2 dsn3 dsn4 dsn5;

As I understand macros just insert text in the code, so I would expect it to produce the following statement: data dsn1dsn2dsn3dsn4dsn5;

What's happening and how could I produce the above statement with the same call ?

Upvotes: 1

Views: 33

Answers (1)

Dirk Horsten
Dirk Horsten

Reputation: 3845

To understand this, run the following code

%macro names_whitespace(name= ,number= );
  %do n=1 %to &number; &name&n %end;
%mend names;

%put %names_whitespace(name=dsn,number=5);

%macro names_nospace(name= ,number= );
  %do n=1 %to &number;&name&n%end;
%mend names;

%put %names_nospace(name=dsn,number=5);

Actually, in your macro %names, you inclide a new line and some tabs or blanks for each name you insert. These are called white space characters When executing a macro, SAS replaces succeding white space characters with one blank.

Upvotes: 3

Related Questions