BookmarkSubscribeRSS Feed
bergsorensen
Calcite | Level 5

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

2 REPLIES 2
data_null__
Jade | Level 19

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?

Vince28_Statcan
Quartz | Level 8

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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 960 views
  • 0 likes
  • 3 in conversation