2
votes

I have a SAS code like this:

%if &tp. = kdb %then %do;
    %let sn = "&kdbsn.";
%end;
%if &tp. = bkb %then %do;
    %let sn = &bkbsn.;
%end;
%if &tp. = edw %then %do;
    %let sn = &edwsn.;
%end;
%if &tp. = odw %then %do;
    %let sn = &odwsn.;
%end;
%if &tp. = adw %then %do;
    %let sn = &adwsn.;
%end;

%put &sn;

I need to do this for many other cases and the pattern is always the same. Depending on the variable &tp. I set the variable &sn. to the same value as a variable with a name that has the first 3 characters equal to value of &tp. and two additional characters sn. Is there a function that would return the name of variable that I need so I don't need to have endless number of if statements?

1
Why does one add quotes and the other's don't?Tom

1 Answers

5
votes

You add more &. The macro processor resolves && to & and sets a note to itself to rescan the token for more macro processing.

%let tp=kdb;
%let kdbsn=1234;
%let sn=&&&tp.sn ;

So && -> & and &tp. -> kdb to get &kdbsn, which will resolve to 1234.