add2optn.sas
%macro add2optn( op, addn, at=front );
%* Modified version of Peter Crawfords addoptn found at
%* http://www.sas.com/service/techtips/quicktips/
%*
%* 03/20/02 Richard A. DeVenezia
%* Do not change mainpart if addn is already in mainpart.
%* Note: If addn prefix matches another different addn already
%* present in op, then addn will *NOT* be added to rebuild.
%* This is not robust behaviour. Invoke using shortest addn first
%* if you run into problems
%* 03/16/09 Peter.Crawford
%* upcase parameter on comparison and use
%* %sysfunc( indexW()) instead on %index()
%* supports searching for the whole parameter not just substring
%*;
%local mainpart rebuild ;
%let mainpart = %sysfunc( compress( %sysfunc( getoption( &op ) ), () )) ;
/* compress to remove the (parenthesis) */
%if %sysfunc( indexw (%upcase(&mainpart), %upcase(&addn))) > 0 %then %do;
%let rebuild = &mainpart ;
%end;
%else
%if &at= front %then %do;
%let rebuild = &addn &mainpart ;
%end;
%else %do;
%let rebuild = &mainpart &addn ;
%end;
&op = ( &rebuild )
%mend add2optn;