SASPYTHON
SASPYTHON

Reputation: 1621

How to concatenate string and numeric SAS Macro and use it in where statement

so I have a code like below

%let THIS_YEAR=2020;

%macro programall;
%do i = 2016 %to &THIS_YEAR;

%let num2  =%eval(&i-2000);
%let xxx= CAT("MP",&num2);

data t_&i.;
set table1;
where GROUP in ("&xxx");
run;

%end;

for example

when i=2016
    num2 = 2016-2000;
    num2 = 16;

and try to concatenate with "MP", so it should create xxx=MP16.

and try to use in where statement.

but it is causing error.

enter image description here

how can I create Macro Variable like "MP16"correctly, so I can use it in where clause?

Thanks

Upvotes: 2

Views: 5779

Answers (3)

Tom
Tom

Reputation: 51566

You cannot use functions in macro code, they are just treated as any other text to the macro processor. But there is no need to use a function to concatenate text in macro code. Just expand the macro variable where you want to use the text it contains.

%let xxx= MP&num2 ;

Upvotes: 1

Richard
Richard

Reputation: 27498

Try

%let THIS_YEAR=2020;

%macro programall;
  %local year;

  %do year = 2016 %to &THIS_YEAR;
    data t_&year.;
      set table1;
      where GROUP in ("MP%eval(&year-2000)");
    run;
  %end;
%mend;

options mprint;
%programall

Upvotes: 0

Joe
Joe

Reputation: 63424

Macro variables are just text (not strings, text as in the thing you type in). So to concatenate macro variables, just put them next to each other.

%let var1=Banana;
%let var2=Pepper;
%let var3=&var1. &var2.;

%put &=var3;

You don't actually have to use the third variable of course, you could just use "&var1. &var2." or whatever in your code directly.

Upvotes: 0

Related Questions