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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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