Help using Base SAS procedures

SAS select columns conditionally based on the table name

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 82
Accepted Solution

SAS select columns conditionally based on the table name

I have a proc step like this:

       proc print data=&tbl_name uniform label;

      var col_1 col_2 col_3;

          run;

          This step runs in a loop and every iteration picks up a different value of a tbl_name which is actually the name of a SAS table.

What I wish to do is:

      proc print data=&tbl_name uniform label;

     if &tbl_name = 'cust_table'  then

       var cust_id cust_name;

      else  if &tbl_name = 'salary_table'  then

        var amount currency;

        else

        /*if the tbl_name does not match any if condition above, then by default select all columns*/

         run;

How to do this?  I cannot find the syntax, I am just starting to learn  SAS.


Accepted Solutions
Solution
‎05-06-2014 12:58 PM
Super User
Posts: 11,343

Re: SAS select columns conditionally based on the table name

Posted in reply to eagles_dare13

You would need the macro version of upcase %upcase(&tbl_name) = CUST_TABLE. I think the forum software split the line in my previous post at the % for some unknown reason or my typing isn't quite right this morning.

Anyway make sure if you use an uppercase comparison that the value compared is in uppercase.

View solution in original post


All Replies
Super User
Posts: 11,343

Re: SAS select columns conditionally based on the table name

Posted in reply to eagles_dare13

If this is running within a macro then use macro conditional logic:

      proc print data=&tbl_name uniform label;

     %if &tbl_name = cust_table  %then %do;  /* this comparison will be case sensitive so make sure it matche OR use %upcase(&tbl_name) = CUST_TABLE if you have doubts about case of tbl_name. NOTE: the macro processor pretty much treats everything as a string and quotes to delimit a string in simple comparisons like this are not needed*/

       var cust_id cust_name;

     %end;

      %else  %if &tbl_name = salary_table  %then %do;

        var amount currency;

      %end;

               /*if the tbl_name does not match any if condition above, then by default select all columns*/

         run;

Frequent Contributor
Posts: 82

Re: SAS select columns conditionally based on the table name

Thank you.

I get error:

 

ERROR: Required operator not found in expression: upcase(&tbl_name) = cust_table

Solution
‎05-06-2014 12:58 PM
Super User
Posts: 11,343

Re: SAS select columns conditionally based on the table name

Posted in reply to eagles_dare13

You would need the macro version of upcase %upcase(&tbl_name) = CUST_TABLE. I think the forum software split the line in my previous post at the % for some unknown reason or my typing isn't quite right this morning.

Anyway make sure if you use an uppercase comparison that the value compared is in uppercase.

Frequent Contributor
Posts: 82

Re: SAS select columns conditionally based on the table name

Thank you. Can you please explain a bit why we need this % sign? What is the rule? If I am writing a macro, every keyword needs to be preceded with a % sign? Why the 'var' keyword does not need a % sign?

Super User
Posts: 11,343

Re: SAS select columns conditionally based on the table name

Posted in reply to eagles_dare13

The macro processor is a text generator. So any functions used by the processor use % to let the processor know this is a macro function. A confusing code but legal would be something like %if %upcase(&var) = VALUE %then upcase(something);

In your case, the "var" statement is normal SAS code that results with the condition. The bits between the %do; %end; are the results the macro processor passes to SAS as code to execute. The section could be multiple lines and even multiple procedure calls.

There are quite a few examples of macro coding in the online help and reasonably good documentation in the Macro Language:Reference under the Base SAS documentation.

Frequent Contributor
Posts: 82

Re: SAS select columns conditionally based on the table name

Thank you very much!

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 243 views
  • 6 likes
  • 2 in conversation