<?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: Check array for list of values in macro variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Check-array-for-list-of-values-in-macro-variable/m-p/747468#M234602</link>
    <description>You probably need to just add the TRIM as indicated by BALLARDW to your macro variables. It probably has spaces but it also worked for me, so there's something else that could be the issue as well.</description>
    <pubDate>Fri, 11 Jun 2021 18:48:35 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2021-06-11T18:48:35Z</dc:date>
    <item>
      <title>Check array for list of values in macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-array-for-list-of-values-in-macro-variable/m-p/747417#M234583</link>
      <description>&lt;P&gt;I have a macro variable with multiple text values such as below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data macro_variable_data;
input code $5.;
format code $5.;
datalines;
56849
98746
23654
12345
;
run;

proc sql;
	select code
	into :codes separated by " "
	from macro_variable_data
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;What I want to do is use this macro variable in my array processing to check if any of the values in the macro variable exist in the columns making up my array. See example data below.&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;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input code1 $6. code2 $6. code3 $6. code4 $6.;
datalines;
43501 93762 56849 10912 
43502 12345 45678 90432
44563 92311 76320 13240
23450 28100 78910 23654
;
run;

data want;
input code1 $6. code2 $6. code3 $6. code4 $6. flag;
datalines;
43501 93762 56849 10912 1
43502 12345 45678 90432 1
44563 92311 76320 13240 0
23450 28100 78910 23654 1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I have attempted to accomplish this like below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
array d code1-code4;
if &amp;amp;codes in d then flag=1;
else flag=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But this produces the below error because it spells out all the elements of the macro variable at once.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;25         GOPTIONS ACCESSIBLE;
26         data want;
27         set have;
28         array d code1-code4;
29         if &amp;amp;codes in d then flag=1;
NOTE: Line generated by the macro variable "CODES".
29          56489 98746 23654 12345
                  _____
                  388
                  200
ERROR 388-185: Expecting an arithmetic operator.

ERROR 200-322: The symbol is not recognized and will be ignored.

30         else flag=0;
31         run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Any help or suggestions for other techniques is appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jun 2021 17:50:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-array-for-list-of-values-in-macro-variable/m-p/747417#M234583</guid>
      <dc:creator>A_SAS_Man</dc:creator>
      <dc:date>2021-06-11T17:50:50Z</dc:date>
    </item>
    <item>
      <title>Re: Check array for list of values in macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-array-for-list-of-values-in-macro-variable/m-p/747451#M234593</link>
      <description>&lt;P&gt;Your first flag in the WANT data set is wrong I believe (56849 vs 56489), if so, this should work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. You have character variables so you need to ensure your array and macro variables are properly formatted for character comparisons (ie quotes)&lt;/P&gt;
&lt;P&gt;2. Loop through each value and check if it's present.&lt;/P&gt;
&lt;P&gt;3. This is actually much easier in a long format - but I know many epidemiologists/statisticians prefer a wide format. Long format for data processing/wrangling, wide is for modeling/analytics.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data macro_variable_data;
input code $5.;
format code $5.;
datalines;
56489
98746
23654
12345
;
run;



data have;
input code1 $6. code2 $6. code3 $6. code4 $6.;
datalines;
43501 93762 56849 10912 
43502 12345 45678 90432
44563 92311 76320 13240
23450 28100 78910 23654
;
run;

proc sql;
	select quote(code)
	into :codes separated by ", "
	from macro_variable_data
;
quit;

data want;
set have;
array _codes (*) code1-code4;

flag=0;

do i=1 to dim(_codes) while(flag=0);
if whichc(_codes(i), &amp;amp;codes) &amp;gt;1 then flag=1;
end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/234586"&gt;@A_SAS_Man&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a macro variable with multiple text values such as below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data macro_variable_data;
input code $5.;
format code $5.;
datalines;
56489
98746
23654
12345
;
run;

proc sql;
	select code
	into :codes separated by " "
	from macro_variable_data
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What I want to do is use this macro variable in my array processing to check if any of the values in the macro variable exist in the columns making up my array. See example data below.&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;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input code1 $6. code2 $6. code3 $6. code4 $6.;
datalines;
43501 93762 56849 10912 
43502 12345 45678 90432
44563 92311 76320 13240
23450 28100 78910 23654
;
run;

data want;
input code1 $6. code2 $6. code3 $6. code4 $6. flag;
datalines;
43501 93762 56849 10912 1
43502 12345 45678 90432 1
44563 92311 76320 13240 0
23450 28100 78910 23654 1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I have attempted to accomplish this like below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
array d code1-code4;
if &amp;amp;codes in d then flag=1;
else flag=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But this produces the below error because it spells out all the elements of the macro variable at once.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;25         GOPTIONS ACCESSIBLE;
26         data want;
27         set have;
28         array d code1-code4;
29         if &amp;amp;codes in d then flag=1;
NOTE: Line generated by the macro variable "CODES".
29          56489 98746 23654 12345
                  _____
                  388
                  200
ERROR 388-185: Expecting an arithmetic operator.

ERROR 200-322: The symbol is not recognized and will be ignored.

30         else flag=0;
31         run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Any help or suggestions for other techniques is appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jun 2021 17:50:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-array-for-list-of-values-in-macro-variable/m-p/747451#M234593</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-06-11T17:50:11Z</dc:date>
    </item>
    <item>
      <title>Re: Check array for list of values in macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-array-for-list-of-values-in-macro-variable/m-p/747456#M234595</link>
      <description>&lt;P&gt;First start with non-macro code that works.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your statement&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;if &amp;amp;codes in d then flag=1;&lt;/LI-CODE&gt;
&lt;P&gt;generates a line that looks like&lt;/P&gt;
&lt;PRE&gt;if 56489 98746 23654 12345 in d then flag=1;&lt;/PRE&gt;
&lt;P&gt;Does that look like valid code to you?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The IN operator takes a list of values, not variables so "in d" would not work in general.&lt;/P&gt;
&lt;P&gt;Second, you created numeric values and your code variables are character, so you were attempting to compare numbers to characters and that often fares poorly because of behavior of implicit conversions.&lt;/P&gt;
&lt;P&gt;So your SQL code would have to look like:&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;proc sql;
	select quote(strip(code))
	into :codes separated by " "
	from macro_variable_data
;
quit;&lt;/LI-CODE&gt;
&lt;P&gt;to get quotes around the value and Strip to suppress the trailing blanks likely to appear for some if not all values in more complex data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Which would allow something like:&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   array d code1-code4;
   flag=0;
   do i=1 to dim(d);
      if d[i] in ( &amp;amp;codes) then do;
         flag=1;
         leave;
      end;&lt;BR /&gt;   end;
   drop i;
run;&lt;/PRE&gt;
&lt;P&gt;which tests each value for your array values one at a time to the list and then leaves the loop testing the first time a value match is found.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jun 2021 18:00:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-array-for-list-of-values-in-macro-variable/m-p/747456#M234595</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-06-11T18:00:28Z</dc:date>
    </item>
    <item>
      <title>Re: Check array for list of values in macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-array-for-list-of-values-in-macro-variable/m-p/747457#M234596</link>
      <description>&lt;P&gt;Thank you for catching that, I have updated my question with corrected values. I seem to be having a little trouble after I update it like below. All I did was change the value in the macro variable data so it would match what's in the have set. But it doesn't flag the first line still for me, am I missing something here?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data macro_variable_data;
input code $5.;
format code $5.;
datalines;
56849
98746
23654
12345
;
run;

proc sql;
	select quote(code)
	into :codes separated by ", "
	from macro_variable_data
;
quit;

data have;
input code1 $6. code2 $6. code3 $6. code4 $6.;
datalines;
43501 93762 56849 10912 
43502 12345 45678 90432
44563 92311 76320 13240
23450 28100 78910 23654
;
run;

data want;
set have;
array _codes (*) code1-code4;

flag=0;

do i=1 to dim(_codes) while(flag=0);
if whichc(_codes(i), &amp;amp;codes) &amp;gt;1 then flag=1;
end;

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 Jun 2021 18:02:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-array-for-list-of-values-in-macro-variable/m-p/747457#M234596</guid>
      <dc:creator>A_SAS_Man</dc:creator>
      <dc:date>2021-06-11T18:02:52Z</dc:date>
    </item>
    <item>
      <title>Re: Check array for list of values in macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-array-for-list-of-values-in-macro-variable/m-p/747460#M234598</link>
      <description>&lt;P&gt;I won't choose this as the solution as it seems less elegant/robust than the others provided here but I wanted show another method I figured out as I was working through this that was pretty simple. I just changed my macro variable creation sql statement to the following.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	select code
	into :codes separated by " in d or "
	from macro_variable_data
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 Jun 2021 18:11:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-array-for-list-of-values-in-macro-variable/m-p/747460#M234598</guid>
      <dc:creator>A_SAS_Man</dc:creator>
      <dc:date>2021-06-11T18:11:08Z</dc:date>
    </item>
    <item>
      <title>Re: Check array for list of values in macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-array-for-list-of-values-in-macro-variable/m-p/747468#M234602</link>
      <description>You probably need to just add the TRIM as indicated by BALLARDW to your macro variables. It probably has spaces but it also worked for me, so there's something else that could be the issue as well.</description>
      <pubDate>Fri, 11 Jun 2021 18:48:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-array-for-list-of-values-in-macro-variable/m-p/747468#M234602</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-06-11T18:48:35Z</dc:date>
    </item>
    <item>
      <title>Re: Check array for list of values in macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-array-for-list-of-values-in-macro-variable/m-p/747470#M234604</link>
      <description>&lt;P&gt;You can if you want keep the information on which of the CODE variables in HAVE is the first one that matched any of the search terms.&amp;nbsp; And you can even keep track of which search term it matched.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select quote(trim(code)) into :terms separated by ','
  from macro_variable_data
;
%let n=&amp;amp;sqlobs;
quit;

data want ;
  set have ;
  array code code1-code4 ;
  do index=1 to dim(code) until(found);
    found=whichc(code[index], &amp;amp;terms);
  end;
  if not found then index=0;
run;;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;Obs    code1    code2    code3    code4    index    found

 1     43501    93762    56849    10912      3        1
 2     43502    12345    45678    90432      2        4
 3     44563    92311    76320    13240      0        0
 4     23450    28100    78910    23654      4        3
&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 Jun 2021 19:02:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-array-for-list-of-values-in-macro-variable/m-p/747470#M234604</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-06-11T19:02:07Z</dc:date>
    </item>
  </channel>
</rss>

