/******************* URBAN INSTITUTE MACRO LIBRARY *********************
: ListDelete
Macro: Autocall macro to remove entries from a list of items.
Description
: Function
Use
: Peter Tatian
Author
***********************************************************************/
ListDelete(
%macro /* List of items */
list1, /* Items to remove from list */
list2, delim=%str( ) /* Delimiter for list (def. blank char) */
);
/*************************** USAGE NOTES *****************************
:
SAMPLE CALLListDelete( &list1, &list2 )
%in &list2 deleted from &list1
returns list with items *********************************************************************/
/*************************** UPDATE NOTES ****************************
02/23/11 PAT Added declaration for local macro vars.
*********************************************************************/
***** ***** ***** MACRO SET UP ***** ***** *****;
%
%local Err1 Err2 ListDelete scanlist1 scanlist2 i item;
***** ***** ***** ERROR CHECKS ***** ***** *****;
%
%if &delim = { or &delim = } %then %do;
= ER;
%let Err1 = ROR;
%let Err2 &Err1&Err2[ListDelete]: Curly braces { } cannot be used as list delimiters.;
%put
%goto exit;
%end;
%if %index( &list1, {{bol}} ) > 0 or %index( &list1, {{eol}} ) > 0 or
%index( &list2, {{bol}} ) > 0 or %index( &list2, {{eol}} ) > 0 %then %do;
= ER;
%let Err1 = ROR;
%let Err2 &Err1&Err2[ListDelete]: The text "{{bol}}" or "{{eol}}" must not appear in either list.;
%put
%goto exit;
%end;
***** ***** ***** MACRO BODY ***** ***** *****;
%
= ;
%let ListDelete = {{bol}}&delim&list1&delim{{eol}};
%let scanlist1 = {{bol}}&delim&list2&delim{{eol}};
%let scanlist2
= 2;
%let i %let item = %scan( &scanlist1, &i, &delim );
%do %while ( %length( &item ) > 0 and &item ~= {{eol}} );
%if %index( &scanlist2, &delim&item&delim ) = 0 %then %do;
%if %length( &ListDelete ) = 0 %then
= &item;
%let ListDelete else
%= &ListDelete&delim&item;
%let ListDelete
%end;%let i = %eval( &i + 1 );
%let item = %scan( &scanlist1, &i, &delim );
%end;
%let ListDelete = %unquote( &ListDelete );
&ListDelete
:
%exit
***** ***** ***** CLEAN UP ***** ***** *****;
%
%mend ListDelete;
/** End Macro Definition **/
/************************ UNCOMMENT TO TEST ***************************
options mprint nosymbolgen nomlogic;= Z A B C X D Y E Y Z F G X;
%let list1 = X Y Z;
%let list2 %let del = [%ListDelete( &list1, &list2 )];
%put _user_;= Z A B C X D Y E Y Z F G X;
%let list1 = XX Y Z;
%let list2 %let del = [%ListDelete( &list1, &list2 )];
%put _user_;= Z A B C XX D Y E Y Z F G X;
%let list1 = X Y Z;
%let list2 %let del = [%ListDelete( &list1, &list2 )];
%put _user_;= A B C;
%let list1 = X Y Z;
%let list2 %let del = [%ListDelete( &list1, &list2 )];
%put _user_;= A B C;
%let list1 = A B C;
%let list2 %let del = [%ListDelete( &list1, &list2 )];
%put _user_;= Z.A.B.C.X.D.Y.E.Y.Z.F.G.X;
%let list1 = X.Y.Z;
%let list2 %let del = [%ListDelete( &list1, &list2, delim=. )];
%put _user_;/**********************************************************************/
Delete List
Purpose: Autocall macro to remove entries from a list of items.