PING  0.9
Statistical data handling and processing in production environment
list_replace

Find and replace all the occurrences of (an) element(s) in a list.

%let rlist=%list_replace(list, old, new, startind=1, startpos=1, casense=no, sep=%quote( ));

Arguments

  • list : a list of blank separated strings;
  • old : (list of) string(s) defining the pattern(s)/element(s) to replace for in the input list list;
  • new : (list of) string(s) defining the replacement pattern(s)/element(s) in the output list rlist; this list must be of length 1 or same length as old;
  • startind : (option) specifies the index of the item in list at which the search should start; incompatible with option startind above; default: startind, i.e. it is not considered;
  • startpos : (option) specifies the position in list at which the search should start; default: startpos=1; incompatible with option startind above; default: startpos, i.e. it is not considered;
  • casense : (option) boolean flag (yes/no) set to perform cases sensitive search/matching; default: casense=no, i.e. all lower- and upper-case occurrences of the pattern in old will be replaced;
  • sep : (option) character/string separator in input list; default: %quote( ), i.e. sep is blank.

Returns

rlist : output list of indexes where all the elements of old present in list have been replaced by the corresponding elements in new (i.e. in the same position in both old and new lists).

Examples

Let us consider a simple example:

%let list=NL UK AT DE AT BE AT;
%let rlist=%list_replace(&list, AT DE, FR IT);

which returns rlist=NL UK FR IT FR BE FR.

Run macro %_example_list_replace for more examples.

Notes

  1. Three configurations are accepted for the input lists old and new of lengths n and m respectively:
    • n=1 and m>=1: all the occurrences of the single item present in old will be replaced by the list new, or
    • m=1 and n>=1: items in list old will be all replaced by the single item in new, or
    • n=m: the ith item of old will be replaced by the ith item of new; otherwise, when n ^= m, an error is reported.
  2. In practice, when you run a single change on a list, e.g. something like:
%let rlist=%list_replace(&list, &old, &new);

with both old and new of same length, one should verify that for:

%let olist=%list_ones(%list_count(&list, &old), item=&new);
%let ind=%list_find(&list, &old);

the following equality holds: list_index(&rlist, &ind) = &olist.

See also

%list_find, %list_index, %list_remove, %list_count, %list_length, FIND.