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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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