<?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: Macro to iterate through variables and apply String matching function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-iterate-through-variables-and-apply-String-matching/m-p/902365#M356606</link>
    <description>&lt;P&gt;When the need is to iterate through variables in a data set and do a similar operation with each the proper tool is an ARRAY. The array definition statement assigns a shorthand name associated with the listed variables. Then another index variable can be used to select which element of the array is used.&lt;/P&gt;
&lt;P&gt;So your code would become something like:&lt;/P&gt;
&lt;PRE&gt;data countvar;
set indata;
array s (*) Var_1 Var_2 /*list the names*/;
/* note that if the names are "nice" you can use a list like
   Var_1 - Var_25 if the names are indeed sequentially numbered
*/
length word $ 25;
None_flag=0;
do j=1 to dim(s);
   searchstr = s[j];
      do i=1 to countw(searchstr); 
          word=scan(searchstr,i);
          if findw("None",strip(word),'i')&amp;gt;=1 then do;
              None_flag=1; 
           leave; 
          end; 
      end; 
end;
drop i j; /*unless you see a need for these variables later*/
run; &lt;/PRE&gt;
&lt;P&gt;Another approach for something with only one reason to search for fixed text might be to concatenate all the variables together and search a single string&lt;/P&gt;
&lt;PRE&gt;data countvar;
set indata;
array s (*) Var_1 Var_2 /*list the names*/;
/* note that if the names are "nice" you can use a list like
   Var_1 - Var_25 if the names are indeed sequentially numbered
*/
 None_flag = findw("None",catx(' ',of S(*))) &amp;gt;=1;

run; &lt;/PRE&gt;
&lt;P&gt;This combines all of the variables into a string with spaces separating the elements and searches for the word one time in the long string.&lt;/P&gt;
&lt;P&gt;SAS will return&amp;nbsp; a value of 1 for true and 0 for false for a comparison so the if/then isn't really needed if the idea is to have 0/1 values on each observation.&lt;/P&gt;</description>
    <pubDate>Thu, 09 Nov 2023 17:11:48 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2023-11-09T17:11:48Z</dc:date>
    <item>
      <title>Macro to iterate through variables and apply String matching function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-iterate-through-variables-and-apply-String-matching/m-p/902359#M356600</link>
      <description>&lt;P&gt;I need to search through the 15 variables of a dataset for a specific substring (the word "None"). When I use the code below for just one variable, I can get the results I want. However, when I try to use a Macro to loop through all 15 variables, the code does not detect the word None - although there are no errors in the log.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Code without macro (just one variable):&lt;/P&gt;
&lt;PRE&gt;data countvar;
set indata;
length word $ 25;
searchstr = var_1;&lt;BR /&gt;** Count occurrences of substring None&lt;BR /&gt;None_flag=0; &lt;BR /&gt;   do i=1 to countw(searchstr); &lt;BR /&gt;       word=scan(searchstr,i);&lt;BR /&gt;       if findw("None",strip(word),'i')&amp;gt;=1 then do;&lt;BR /&gt;           None_flag=1; &lt;BR /&gt;        leave; &lt;BR /&gt;       end; &lt;BR /&gt;   end; &lt;BR /&gt;run; &lt;/PRE&gt;
&lt;DIV&gt;Now when I try to create a macrovariable list of all the variables in the dataset 'Indata' inside 'list' and pass the above function into a macro "loopit", the code runs without errors but fails to detect the substring "None" - the variable None_flag is zero for all observations in the dataset.&lt;/DIV&gt;
&lt;DIV&gt;&lt;LI-CODE lang="sas"&gt;%macro loopit(list);
	data countvar1;
	set Dat_q3_wide;
    %do i = 1 %to %sysfunc(countw(&amp;amp;list.));
    	%let word  = %scan(&amp;amp;list., &amp;amp;i.);
		None_flag=0;
	
   		%if %sysfunc(findw(None,strip(&amp;amp;word),ii))&amp;gt;=1 %then %do;
      		None_flag=1;
    	%end;
	%end;
	run;
%mend loopit;
%let list = Var_1 Var_2 Var_3 Var_4;
%loopit(list=&amp;amp;list)&lt;/LI-CODE&gt;&lt;/DIV&gt;
&lt;PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Here is some sample data:&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;ID&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;Var_1&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;Var_2&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;Var_3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="57px"&gt;
&lt;P&gt;11&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="25%" height="57px"&gt;None(0%)&lt;/TD&gt;
&lt;TD width="25%" height="57px"&gt;Nearly all(100%)&lt;/TD&gt;
&lt;TD width="25%" height="57px"&gt;None(0%)&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;12&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;Nearly all(100%)&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;None(0%)&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;None(0%)&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;13&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;None(0%)&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;Some (50%)&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;Nearly all(100%)&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Thu, 09 Nov 2023 16:52:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-iterate-through-variables-and-apply-String-matching/m-p/902359#M356600</guid>
      <dc:creator>nstdt</dc:creator>
      <dc:date>2023-11-09T16:52:02Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to iterate through variables and apply String matching function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-iterate-through-variables-and-apply-String-matching/m-p/902362#M356603</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards truncover dsd;
length id 8. var_1-var_3 $20.;
input ID  Var_1 $  Var_2  $ Var_3 $;
cards;
11, None(0%),    Nearly all(100%),    None(0%)
12,  Nearly all(100%),    None(0%),    None(0%)
13,  None(0%),    Some (50%) , Nearly all(100%)
14, Some(50%), Nearly all(50%), Some(20%)
;
run;

data want;
set have;
array _var(*) var_1-var_3;

if find(catx(" | ", of _var(*)), "None") &amp;gt; 0 then flag="1";
else flag = "0"; *probably use better names;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here's a tutorial on using Arrays in SAS&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/seminars/sas-arrays/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/seminars/sas-arrays/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Seems like a better usage for arrays than macros.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Nov 2023 17:02:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-iterate-through-variables-and-apply-String-matching/m-p/902362#M356603</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-11-09T17:02:56Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to iterate through variables and apply String matching function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-iterate-through-variables-and-apply-String-matching/m-p/902365#M356606</link>
      <description>&lt;P&gt;When the need is to iterate through variables in a data set and do a similar operation with each the proper tool is an ARRAY. The array definition statement assigns a shorthand name associated with the listed variables. Then another index variable can be used to select which element of the array is used.&lt;/P&gt;
&lt;P&gt;So your code would become something like:&lt;/P&gt;
&lt;PRE&gt;data countvar;
set indata;
array s (*) Var_1 Var_2 /*list the names*/;
/* note that if the names are "nice" you can use a list like
   Var_1 - Var_25 if the names are indeed sequentially numbered
*/
length word $ 25;
None_flag=0;
do j=1 to dim(s);
   searchstr = s[j];
      do i=1 to countw(searchstr); 
          word=scan(searchstr,i);
          if findw("None",strip(word),'i')&amp;gt;=1 then do;
              None_flag=1; 
           leave; 
          end; 
      end; 
end;
drop i j; /*unless you see a need for these variables later*/
run; &lt;/PRE&gt;
&lt;P&gt;Another approach for something with only one reason to search for fixed text might be to concatenate all the variables together and search a single string&lt;/P&gt;
&lt;PRE&gt;data countvar;
set indata;
array s (*) Var_1 Var_2 /*list the names*/;
/* note that if the names are "nice" you can use a list like
   Var_1 - Var_25 if the names are indeed sequentially numbered
*/
 None_flag = findw("None",catx(' ',of S(*))) &amp;gt;=1;

run; &lt;/PRE&gt;
&lt;P&gt;This combines all of the variables into a string with spaces separating the elements and searches for the word one time in the long string.&lt;/P&gt;
&lt;P&gt;SAS will return&amp;nbsp; a value of 1 for true and 0 for false for a comparison so the if/then isn't really needed if the idea is to have 0/1 values on each observation.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Nov 2023 17:11:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-iterate-through-variables-and-apply-String-matching/m-p/902365#M356606</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-11-09T17:11:48Z</dc:date>
    </item>
  </channel>
</rss>

