BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mcs
Obsidian | Level 7 mcs
Obsidian | Level 7

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

5 REPLIES 5
Astounding
PROC Star

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.

mcs
Obsidian | Level 7 mcs
Obsidian | Level 7

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.

Tom
Super User Tom
Super User

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
CarmineVerrell
SAS Employee

 

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;

Tom
Super User Tom
Super User

Just insert the commas between the list of values.

%let test = %sysfunc(whichn(6,2,4,6,8,10));

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5 replies
  • 651 views
  • 3 likes
  • 4 in conversation