Dynamic replace function

Posts: 29

Dynamic replace function

Im trying to create a dynamic function to let me search and replace all coloumns in a specific datasheet.

Im having some trouble with my array.

This is the as-if code:

** Options;

%let search   = "P";

%let replace  = "A";

** Find coloums;

%macro checklist;

%global columnlist;



** S&R function;

%macro searchreplace(data);

data &data._new;

set &data;

array varlist{*} &columnlist;

do i = 1 to dim(varlist);

  varlist(i)=tranwrd(varlist(i), &search, &replace);


drop i;



** Test;



This is my error log:

30   ** Options;

31   %let search   = "P";

32   %let replace  = "A";


34   ** Find coloums;

35   %macro checklist;

36    %global columnlist;

37    /* %if &columnlist eq %then %let columnlist=_character_; */

38   %mend checklist;

39   %checklist


41   ** Søg og erstat funktion;

42   %macro searchreplace(data);

43   data &data._ny;

44    set &data;

45    array varlist{*} &columnlist;

46    do i = 1 to dim(varlist);

47     varlist(i)=tranwrd(varlist(i), &search, &replace);

48    end;

49    drop i;

50   run;

51   %mend;


53   ** Kør skidtet;

54   %searchreplace(data=testdata);

ERROR: The array varlist has been defined with zero elements.

ERROR: Too many array subscripts specified for array varlist.

ERROR: Too many array subscripts specified for array varlist.

Any input / help would be much appriciated.



Respected Advisor
Posts: 3,777

Re: Dynamic replace function

I would make &search &replace and &columnlist parameters of %searchreplace.

I don't get what you are trying to do with %checklist.  What options do you wan't to allow for specificaion of the variables(&columnlist) to be processed?

Super Contributor
Posts: 339

Re: Dynamic replace function


options symbolgen;

for debugging. According to your log, COLUMNLIST macro variable was never affected a value as the condition segment is in comments so you tried to define a variable-list array without providing any variables. This should be where your error comes from.

I would also add a columnlist param to the the macro as DN pointed out. you can embed your condition within the macro or do a simple error handling.

Ask a Question
Discussion stats
  • 2 replies
  • 3 in conversation