%macro chopwrd(var, /* required positional parm names the variable to be edited */ /* IMPORTANT NOTE: This macro generates the assignment statement. It cannot be used as a function, invoked on the right side of an assignment statement (it would generate "logical garbage".) */ dlms=%str( ,), /* a list of characters to be used as delimiters of "words" for this macro */ unless=, /* specify a value which will cause the macro NOT to chop it. I.e. if the last word has the value &unless then it will remain as part of the edited variable */ outvar=, /* By default the edited value is assigned back to &var but if you specify a value for this parm then the edited value is assigned to &outvar. */ debug=0); %*--Chop off last work in variable &var as delimited by &dlms unless that word matches the value of the unless parm. Assigns result to outvar, which by default is the same as var. It references variables _lastwd_ and _ix_ which in most cases should be dropped by invoking module. John Blodgett, June 2002. Last revised: 20Nov2002 --*; %if %quote(&var) eq %str() %then %do; %local emsg; %let emsg=Required positional parm "var" was not specified; %goto errmsg; %end; %if %quote(&outvar) eq %str() %then %let outvar=&var; %let dlms="&dlms"; %local lastword; _lastwd_=scan(&var,-1,&dlms); if _lastwd_ ne ' ' %if %bquote(&unless) ne %str() %then %do; and _lastwd_ ne "&unless" %end; then do; if _lastwd_=&outvar then &outvar=' '; else &outvar= substr( &var,1, max(1,length(&var) - length(_lastwd_) -1)); _ix_=length(&outvar); if &outvar ne ' ' then do while(index(&dlms,trim(substr(&outvar,_ix_,1)))); %*--look for 1st non-delimiter character--; substr(&outvar,_ix_)=' '; _ix_=_ix_-1; if &outvar=' ' then leave; %if &debug %then %str(put /_all_;); end; end; %if &outvar ne &var %then %do; else &outvar=&var; %end; %goto endmac; %errmsg: %put *****Error using the chopwrd macro***** ; %put ***** &emsg *****; This bad line of code generated by the chopwrd macro; _error_=1; stop; %endmac: %mend chopwrd;