0

I have a situation where I need to assign a table name with a macro variable whose value is derived from another macro variable.

However, the generated code in macro uses the derivation formula rather than the derived value itself.

    %macro testing;

    %let n=2;
    %let k=&n-1;
    %let opp=&k;

    PROC SQL;

    CREATE TABLE WORK.TABLE_&opp AS
    SELECT * from WORK.SOURCETABLE;

    QUIT;

    %mend;

    %testing

This resulted in error as the code generated

NOTE: Line generated by the macro variable "OPP".
34          WORK.TABLE_2-1
                    _
                    22
                    200
ERROR 22-322: Syntax error, 

But I am expecting the resultant table will be named as WORK.TABLE_1 instead of WORK.TABLE_2-1.

So how can I force SAS to identify variable opp=1 rather than opp=2-1?

Thank you

1

1 Answer 1

2

Try using %eval as shown under:

 %macro testing;

    %let n=2;
    %let k=&n-1;
    %let opp=%eval(&k);

    PROC SQL;

    CREATE TABLE WORK.TABLE_&opp AS
    SELECT * from WORK.SOURCETABLE;

    QUIT;

    %mend;

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

3 Comments

Yep, seems to do the trick. Need to read up more on macro functions. Thanks!
A macro variable is stored as a text string, so SAS doesn't recognise any calculation in a %let statement, unless a macro function is used. I'm not sure why you have variables k and opp, as they take the same value (unless this is purely for example purposes). You could put %let k=%eval(&n-1) to get the same answer
Just note that you need SYSEVALF if you are not performing integer operations

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.