Macro code for Case or Select

From sasCommunity
Jump to: navigation, search

This is a SAS-ware Ballot Item created on Mar 11, 2015 12:19 PM

Macro Statement: %SELECT or %CASE

Add a statement to Macro Language for functionality similar to SELECT statement in Data Step or CASE statement in SQL

Code

For Example

%macro foo;   
%local foo; 
%let foo=abc;   
%select(%upcase(&foo.)); 
   %when(XYZ) %put >>>blah; 
   %otherwise %put >>>nope; 
   %end;   
%mend;

This is easy to code and readable, too.

Branch Value is Text

%macro go_to(label=abc);
%goto &label;
%abc:
   %put actions for &=label;
   %goto exit;
%def:
   %put actions for &=label;
   %goto exit;
%ghi:
   %put actions for &=label;
   %goto exit;
%exit:
%mend;
%go_to()
%go_to(label=def)
%go_to(label=ghi)

Branch Value is False or True (not False)

%macro go_to(value=x,testing=1);
%let testing = %eval(0 ne &testing);
%let branch  = %eval(0 ne &value);
%goto _&branch;
%_0:
   %put actions for false &=value &=branch;
   %goto exit;
%_1:
   %put actions for true &=value &=branch;
   %goto exit;
%exit:
%mend;
%go_to()
%go_to(value=13)
%go_to(value=0)


--Ronald_J._Fehd macro.maven == the radical programmer (talk) 14:42, 26 May 2015 (CDT)