2

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
  • 1
    Why does one add quotes and the other's don't? Commented Oct 15, 2018 at 19:29

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.