<?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: Loop through a list of strings in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646264#M193287</link>
    <description>&lt;P&gt;I think another issue is that dataset "have" has more than 1000 rows. So when I run the macro, I got&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;WARNING: The quoted string currently being processed has become more than 262 bytes&lt;BR /&gt;long. You might have unbalanced quotation marks.&lt;BR /&gt;ERROR: The function COUNTW referenced by the %SYSFUNC or %QSYSFUNC macro function has&lt;BR /&gt;too many arguments.&lt;BR /&gt;ERROR: A character operand was found in the %EVAL function or %IF condition where a&lt;BR /&gt;numeric operand is required. The condition was: %sysfunc(countw(&amp;amp;names))&lt;BR /&gt;ERROR: The %TO value of the %DO I loop is invalid.&lt;BR /&gt;ERROR: The macro DOTHIS will stop executing.&lt;/P&gt;&lt;/BLOCKQUOTE&gt;</description>
    <pubDate>Fri, 08 May 2020 16:46:31 GMT</pubDate>
    <dc:creator>xyxu</dc:creator>
    <dc:date>2020-05-08T16:46:31Z</dc:date>
    <item>
      <title>Loop through a list of strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646057#M193209</link>
      <description>&lt;P&gt;I thought this was easy but got stuck. I have a dataset that is simply a list of strings. My macro needs to loop through this list, each time using one string from the list in a proc. What is an efficient way to make this work?&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;_______________________________________________________________________________________________________________________&lt;/P&gt;&lt;P&gt;Please see below for a minimum exampe. I have two datasets (their actual versions are much larger):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
	input string $6.;
	datalines;
AB C
DEF
G H I
;
run;

data have2;
	input issuer_id 3. security_name $20.;
	datalines;
101 AB C security 05
101 Ab C security 012
103 DEF security 02
104 DEF bond 03
104 Def bond 11
109 G H I security 09
112 G H I bond 04
;
run; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;What I want to do: for each value of string in have1, among all observations in have2 where security_name contains the value, count the number of unique issuer_id:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro count_id(in_string);
	proc sql;
		select count(unique issuer_id) as num_id
		from have2 (where = (index(lowcase(security_name), lowcase(&amp;amp;in_string)) &amp;gt;0));			
	quit;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want to put this count back to have1. The below is similar to suggestions by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12477"&gt;@RichardDeVen&lt;/a&gt;&amp;nbsp;:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have;
	count = dosubl('%count_id(string)');
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;However, the error message I got is "ERROR: Variable string is not on file WORK.HAVE2."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 May 2020 17:11:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646057#M193209</guid>
      <dc:creator>xyxu</dc:creator>
      <dc:date>2020-05-08T17:11:57Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through a list of strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646059#M193210</link>
      <description>&lt;P&gt;Please show us a portion of your data, and then show us the desired results.&lt;/P&gt;
&lt;DIV id="tap-translate"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Thu, 07 May 2020 23:04:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646059#M193210</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-05-07T23:04:18Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through a list of strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646073#M193219</link>
      <description>&lt;P&gt;Suppose that I have a dataset as follows:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input string $4.;
	datalines;
ABC
DEF
GHI
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I need a macro to loop through it. In each step, I will perform commands that use one value of string, starting from "ABC".&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 May 2020 00:42:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646073#M193219</guid>
      <dc:creator>xyxu</dc:creator>
      <dc:date>2020-05-08T00:42:27Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through a list of strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646075#M193220</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro dothis;
Proc sql;
    Select distinct string into :strings separated by ' ' from have;
Quit;
%do i=1 %to %sysfunc(countw(&amp;amp;strings));
    %let thisstring=%scan(&amp;amp;strings,&amp;amp;i,%str( ));
    /* Perform some action using macro variable &amp;amp;thisstring */
%end;
%mend;
%dothis&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV id="tap-translate"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV id="tap-translate"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Fri, 08 May 2020 00:53:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646075#M193220</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-05-08T00:53:02Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through a list of strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646076#M193221</link>
      <description>&lt;P&gt;The core essence is that you are submitting code written by some process.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The DOSUBL function is one approach for submitting dynamic code whose essence relies on values in a data set.&amp;nbsp; In your case the dynamic code is 'the code that invokes a macro'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data _null_;
  set have;
  sasCode = cats ( '%myMacro(', string, ');' );   /* dynamic code which is source for invoking a macro */
  rc = doSubL (sasCode);
run;&lt;/PRE&gt;
&lt;P&gt;From inside DATA step there are other ways to launch dynamically generated macro invocation code:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;CALL EXECUTE Routine&lt;/LI&gt;
&lt;LI&gt;RESOLVE Function&lt;/LI&gt;
&lt;LI&gt;PUT Statements with step followed by %INCLUDE&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 May 2020 01:04:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646076#M193221</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-05-08T01:04:54Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through a list of strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646083#M193223</link>
      <description>&lt;P&gt;I tried to work on this template but it did not work as expected. One potential reason is that my strings include spaces. So for example,&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input string $10.;
	datalines;
AB C
DEF
G H I
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Would this code still work?&lt;/P&gt;</description>
      <pubDate>Fri, 08 May 2020 03:18:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646083#M193223</guid>
      <dc:creator>xyxu</dc:creator>
      <dc:date>2020-05-08T03:18:20Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through a list of strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646092#M193227</link>
      <description>&lt;P&gt;This approach looks cool but I wasn't able to feed the value of string in "have" into the macro. I got "ERROR: Variable string is not on file WORK.XXXX."&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 May 2020 04:30:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646092#M193227</guid>
      <dc:creator>xyxu</dc:creator>
      <dc:date>2020-05-08T04:30:05Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through a list of strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646104#M193232</link>
      <description>&lt;P&gt;use next example to loop thru list of strings:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let row= abc def gehi kl;
%macro loop(line);
   %do i=1 %to %sysfunc(countw(&amp;amp;line));
      %let word = %scan(&amp;amp;line,&amp;amp;i);
      %put word=&amp;amp;word;
   %end;
%mend loop;
%loop(&amp;amp;row);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 May 2020 05:38:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646104#M193232</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-05-08T05:38:50Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through a list of strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646105#M193233</link>
      <description>&lt;P&gt;that doesn't create a list but a dataset with each string in different observation.&lt;/P&gt;
&lt;P&gt;the loop is implicit inside the data step.&lt;/P&gt;</description>
      <pubDate>Fri, 08 May 2020 05:41:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646105#M193233</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-05-08T05:41:31Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through a list of strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646107#M193235</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set have;
call execute(cats('action_part_one',string,'action_part_two'));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;How you create the 'action' around the string depends on what the 'action' really is, but you haven't told us that.&lt;/P&gt;</description>
      <pubDate>Fri, 08 May 2020 05:54:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646107#M193235</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-05-08T05:54:07Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through a list of strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646109#M193237</link>
      <description>&lt;P&gt;You have to show the macro source code, or a close facsimile.&amp;nbsp; A lot depends on what the macro is doing and what the parameters are in the macro definition.&lt;/P&gt;</description>
      <pubDate>Fri, 08 May 2020 06:19:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646109#M193237</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-05-08T06:19:27Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through a list of strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646218#M193270</link>
      <description>&lt;P&gt;I suggest a minor improvement on the first suggestion:&lt;/P&gt;
&lt;P&gt;Don't separate your values blank, but use a tilde ~&amp;nbsp; or # or any&amp;nbsp;Or use any character that wont show up in your data&lt;/P&gt;
&lt;P&gt;Just avoid characters that have meanings in macros, such as quotes,&amp;nbsp; parenthesis, % &amp;amp;&amp;nbsp; etc.&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your strings have such special characters, then you need to mask them.&amp;nbsp; &amp;nbsp;But I will first assume that they are not an issue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* replaced the blank in scan and separarated by with a tilde   ~ 
Or use  any character that wont show up in your data */
%macro dothis;
Proc sql;
    Select distinct string into :strings separated by '~' from have;
Quit;
%do i=1 %to %sysfunc(countw(&amp;amp;strings));
    %let thisstring=%scan(&amp;amp;strings,&amp;amp;i,%str(~));
    /* Perform some action using macro variable &amp;amp;thisstring */
%end;
%mend;
%dothis&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 May 2020 14:07:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646218#M193270</guid>
      <dc:creator>DavePrinsloo</dc:creator>
      <dc:date>2020-05-08T14:07:23Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through a list of strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646264#M193287</link>
      <description>&lt;P&gt;I think another issue is that dataset "have" has more than 1000 rows. So when I run the macro, I got&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;WARNING: The quoted string currently being processed has become more than 262 bytes&lt;BR /&gt;long. You might have unbalanced quotation marks.&lt;BR /&gt;ERROR: The function COUNTW referenced by the %SYSFUNC or %QSYSFUNC macro function has&lt;BR /&gt;too many arguments.&lt;BR /&gt;ERROR: A character operand was found in the %EVAL function or %IF condition where a&lt;BR /&gt;numeric operand is required. The condition was: %sysfunc(countw(&amp;amp;names))&lt;BR /&gt;ERROR: The %TO value of the %DO I loop is invalid.&lt;BR /&gt;ERROR: The macro DOTHIS will stop executing.&lt;/P&gt;&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Fri, 08 May 2020 16:46:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646264#M193287</guid>
      <dc:creator>xyxu</dc:creator>
      <dc:date>2020-05-08T16:46:31Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through a list of strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646267#M193289</link>
      <description>&lt;P&gt;At this point it will help for you to show the code you actually tried.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If %SYSFUNC(COUNT()) is see too many parameters then you gave it unmasked/unquoted/unprotected commas.&amp;nbsp; Don't do that.&lt;/P&gt;</description>
      <pubDate>Fri, 08 May 2020 17:24:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646267#M193289</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-05-08T17:24:10Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through a list of strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646290#M193300</link>
      <description>&lt;P&gt;Consider a metaphor that information is like wind.&amp;nbsp; We have new information about the problem and it is time to change tack.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A SQL query will much more cleanly perform the desired counting.&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table want as
  select 
    string
  , count(distinct issuer_id) as uniq_id_count
  from 
    have1
  join 
    have2
  on 
    lowcase(security_name) like lowcase(cats('%',string,'%'))
  group by
    string
  ;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 May 2020 18:35:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-list-of-strings/m-p/646290#M193300</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-05-08T18:35:37Z</dc:date>
    </item>
  </channel>
</rss>

