<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: %SYSFUNC and WHICHN in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SYSFUNC-and-WHICHN/m-p/742961#M232509</link>
    <description>&lt;P&gt;Indirectly, you can.&amp;nbsp; For example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
array x(5) (2 4 6 8 10);
whichone = whichn(6, of x(*));&lt;BR /&gt;call symputx('test', whichone);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But directly, no.&amp;nbsp; Macro language does not try to interpret the values of variables in a SAS data set.&amp;nbsp; So create a DATA step variable, then use CALL SYMPUTX to transfer that value to a macro variable.&lt;/P&gt;</description>
    <pubDate>Fri, 21 May 2021 16:46:34 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2021-05-21T16:46:34Z</dc:date>
    <item>
      <title>%SYSFUNC and WHICHN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SYSFUNC-and-WHICHN/m-p/742957#M232507</link>
      <description>&lt;P&gt;I'd like to use the WHICHN function in a macro.&amp;nbsp; For instance, in the code below I want to set &amp;amp;test = 3.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
array x(5) (2 4 6 8 10);
%let test = %sysfunc(whichn(6, of x(*)));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Instead, I get the following error.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ERROR: Argument 2 to function WHICHN referenced by the %SYSFUNC or %QSYSFUNC macro function is
       not a number.
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is there a way to make it work?&lt;/P&gt;</description>
      <pubDate>Fri, 21 May 2021 16:43:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SYSFUNC-and-WHICHN/m-p/742957#M232507</guid>
      <dc:creator>mcs</dc:creator>
      <dc:date>2021-05-21T16:43:02Z</dc:date>
    </item>
    <item>
      <title>Re: %SYSFUNC and WHICHN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SYSFUNC-and-WHICHN/m-p/742961#M232509</link>
      <description>&lt;P&gt;Indirectly, you can.&amp;nbsp; For example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
array x(5) (2 4 6 8 10);
whichone = whichn(6, of x(*));&lt;BR /&gt;call symputx('test', whichone);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But directly, no.&amp;nbsp; Macro language does not try to interpret the values of variables in a SAS data set.&amp;nbsp; So create a DATA step variable, then use CALL SYMPUTX to transfer that value to a macro variable.&lt;/P&gt;</description>
      <pubDate>Fri, 21 May 2021 16:46:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SYSFUNC-and-WHICHN/m-p/742961#M232509</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2021-05-21T16:46:34Z</dc:date>
    </item>
    <item>
      <title>Re: %SYSFUNC and WHICHN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SYSFUNC-and-WHICHN/m-p/742974#M232515</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;You unfortunately, can't create Arrays in Macros. That being said your example and question is a little confusing.&amp;nbsp; You can create&amp;nbsp; macro variables within a dataset using call symputx routine.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an alternative....&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data _null_;&lt;BR /&gt;array x(5) (2 4 6 8 10);&lt;BR /&gt;call symputx("response",whichn(6, of x(*)));&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;%put &amp;amp;=response;&lt;/P&gt;</description>
      <pubDate>Fri, 21 May 2021 17:10:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SYSFUNC-and-WHICHN/m-p/742974#M232515</guid>
      <dc:creator>CarmineVerrell</dc:creator>
      <dc:date>2021-05-21T17:10:47Z</dc:date>
    </item>
    <item>
      <title>Re: %SYSFUNC and WHICHN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SYSFUNC-and-WHICHN/m-p/743036#M232540</link>
      <description>&lt;P&gt;Thanks for the reply.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Unfortunately, I think I oversimplified my example code.&amp;nbsp; My actual use case has WHICHN inside a macro program.&amp;nbsp; Something like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro which();
	%do i = 2 %to 10 %by 2;
		whichone = whichn(&amp;amp;i, of x(*)); call symputx('test', whichone);
		%put &amp;amp;=i &amp;amp;=test;
	%end;
%mend;

data _null_;
array x(5) (2 4 6 8 10);
%which;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want test to take on the values 1 to 5 in order.&amp;nbsp; Instead I get&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;I=2 TEST=1
I=4 TEST=1
I=6 TEST=1
I=8 TEST=1
I=10 TEST=1&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I think I need to find a different workaround.&lt;/P&gt;</description>
      <pubDate>Fri, 21 May 2021 19:59:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SYSFUNC-and-WHICHN/m-p/743036#M232540</guid>
      <dc:creator>mcs</dc:creator>
      <dc:date>2021-05-21T19:59:16Z</dc:date>
    </item>
    <item>
      <title>Re: %SYSFUNC and WHICHN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SYSFUNC-and-WHICHN/m-p/743038#M232541</link>
      <description>&lt;P&gt;I cannot make sense of what you want to do.&amp;nbsp; How does macro code even enter into the problem?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;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
&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 May 2021 20:15:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SYSFUNC-and-WHICHN/m-p/743038#M232541</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-21T20:15:59Z</dc:date>
    </item>
    <item>
      <title>Re: %SYSFUNC and WHICHN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SYSFUNC-and-WHICHN/m-p/743072#M232559</link>
      <description>&lt;P&gt;Just insert the commas between the list of values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let test = %sysfunc(whichn(6,2,4,6,8,10));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 May 2021 22:36:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SYSFUNC-and-WHICHN/m-p/743072#M232559</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-21T22:36:16Z</dc:date>
    </item>
  </channel>
</rss>

