BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
eagles_dare13
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

6 REPLIES 6
ballardw
Super User

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;

eagles_dare13
Obsidian | Level 7

Thank you.

I get error:

 

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

ballardw
Super User

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.

eagles_dare13
Obsidian | Level 7

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?

ballardw
Super User

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.

eagles_dare13
Obsidian | Level 7

Thank you very much!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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