I'd like to use the WHICHN function in a macro. For instance, in the code below I want to set &test = 3.
data _null_;
array x(5) (2 4 6 8 10);
%let test = %sysfunc(whichn(6, of x(*)));
run;
Instead, I get the following error.
ERROR: Argument 2 to function WHICHN referenced by the %SYSFUNC or %QSYSFUNC macro function is
not a number.
Is there a way to make it work?
Indirectly, you can. For example:
data _null_;
array x(5) (2 4 6 8 10);
whichone = whichn(6, of x(*));
call symputx('test', whichone);
run;
But directly, no. Macro language does not try to interpret the values of variables in a SAS data set. So create a DATA step variable, then use CALL SYMPUTX to transfer that value to a macro variable.
Indirectly, you can. For example:
data _null_;
array x(5) (2 4 6 8 10);
whichone = whichn(6, of x(*));
call symputx('test', whichone);
run;
But directly, no. Macro language does not try to interpret the values of variables in a SAS data set. So create a DATA step variable, then use CALL SYMPUTX to transfer that value to a macro variable.
Thanks for the reply.
Unfortunately, I think I oversimplified my example code. My actual use case has WHICHN inside a macro program. Something like
%macro which();
%do i = 2 %to 10 %by 2;
whichone = whichn(&i, of x(*)); call symputx('test', whichone);
%put &=i &=test;
%end;
%mend;
data _null_;
array x(5) (2 4 6 8 10);
%which;
run;
I want test to take on the values 1 to 5 in order. Instead I get
I=2 TEST=1
I=4 TEST=1
I=6 TEST=1
I=8 TEST=1
I=10 TEST=1
I think I need to find a different workaround.
I cannot make sense of what you want to do. How does macro code even enter into the problem?
data _null_;
array x(5) (2 4 6 8 10);
do i=1 to dim(x);
test=whichn(i, of x[*]);
put i= test=;
end;
run;
Results:
1 data _null_;
2 array x(5) (2 4 6 8 10);
3 do i=1 to dim(x);
4 test=whichn(i, of x[*]);
5 put i= test=;
6 end;
7 run;
i=1 test=0
i=2 test=1
i=3 test=0
i=4 test=2
i=5 test=0
If you wanted to call a macro in the middle of that data step then it would need to be a macro that generated those statements.
%macro which;
do i=1 to dim(x);
test=whichn(i, of x[*]);
put i= test=;
end;
%mend;
data _null_;
array x(5) (2 4 6 8 10);
%which
run;
Results
8 %macro which; 9 do i=1 to dim(x); 10 test=whichn(i, of x[*]); 11 put i= test=; 12 end; 13 %mend; 14 15 options mprint; 16 data _null_; 17 array x(5) (2 4 6 8 10); 18 %which MPRINT(WHICH): do i=1 to dim(x); MPRINT(WHICH): test=whichn(i, of x[*]); MPRINT(WHICH): put i= test=; MPRINT(WHICH): end; 19 run; i=1 test=0 i=2 test=1 i=3 test=0 i=4 test=2 i=5 test=0
Hello,
You unfortunately, can't create Arrays in Macros. That being said your example and question is a little confusing. You can create macro variables within a dataset using call symputx routine.
Here is an alternative....
data _null_;
array x(5) (2 4 6 8 10);
call symputx("response",whichn(6, of x(*)));
run;
%put &=response;
Just insert the commas between the list of values.
%let test = %sysfunc(whichn(6,2,4,6,8,10));
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.