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;
%mend;
%checklist
** 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);
end;
drop i;
run;
%mend;
** Test;
%searchreplace(data=testdata);
---
This is my error log:
30 ** Options;
31 %let search = "P";
32 %let replace = "A";
33
34 ** Find coloums;
35 %macro checklist;
36 %global columnlist;
37 /* %if &columnlist eq %then %let columnlist=_character_; */
38 %mend checklist;
39 %checklist
40
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;
52
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.
Thanks
Kasper
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?
Throw
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.