<?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: Concatenate macro variables within a do loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-macro-variables-within-a-do-loop/m-p/275627#M55133</link>
    <description>&lt;P&gt;Yes it is a bit messy isn't it &lt;span class="lia-unicode-emoji" title=":confused_face:"&gt;😕&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So sample dataset below:-&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data courses;&lt;BR /&gt; length course_long_name $200;&lt;BR /&gt; course_long_name="Creative Writing"; output;&lt;BR /&gt; course_long_name="Creative Industry"; output;&lt;BR /&gt; course_long_name="Creative and Cultural Industry"; output;&lt;BR /&gt; course_long_name="Creative Media"; output;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let filter_1 = CREATIVE INDUSTRY;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Resulting output dataset would extract&amp;nbsp;the second and third courses as they contain occurences of the requested filter keywords.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kim&lt;/P&gt;</description>
    <pubDate>Tue, 07 Jun 2016 11:58:34 GMT</pubDate>
    <dc:creator>kimdukes77</dc:creator>
    <dc:date>2016-06-07T11:58:34Z</dc:date>
    <item>
      <title>Concatenate macro variables within a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-macro-variables-within-a-do-loop/m-p/275610#M55125</link>
      <description>&lt;P&gt;Hope someone can help with a problem I am having. &amp;nbsp;Code below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My code reads in a job specfile that can contain up to ten keyword filters. &amp;nbsp;These need to be split into their component parts and then passed to a string to build a filter that later gets passed to a where statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if filter1 is CREATIVE CULTURAL INDUSTRY&amp;nbsp;it would be built&amp;nbsp;in&amp;nbsp;a string as follows:-&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;INDEX(UPCASE(course_long_name),"CREATIVE")&amp;gt;0 AND&amp;nbsp;&lt;SPAN&gt;INDEX(UPCASE(course_long_name),"CULTURAL")&amp;gt;0 AND&amp;nbsp;INDEX(UPCASE(course_long_name),"INDUSTRY")&amp;gt;0.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The problem I am having with my code is that it retains substr_1 but &amp;amp;&amp;amp;substr_&amp;amp;k just resolves to the last macro variable it encounters. &amp;nbsp;Does anyone know how to&amp;nbsp;iteratively pass the macro variables created in &amp;amp;substr_&amp;amp;i to a concatenated string?&lt;/SPAN&gt;&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;%macro splitter(string=,wordpfx=WORD,dlm=%str( ));&lt;BR /&gt;%do cnt=1 %to %sysfunc(countw(&amp;amp;string,&amp;amp;dlm));&lt;BR /&gt; %global &amp;amp;wordpfx&amp;amp;cnt;&lt;BR /&gt; %let &amp;amp;wordpfx&amp;amp;cnt = %scan(&amp;amp;string,&amp;amp;cnt,%str(&amp;amp;dlm));&lt;BR /&gt; %* echo macro var result to log window;&lt;BR /&gt; %put &amp;amp;wordpfx&amp;amp;cnt=&amp;amp;&amp;amp;&amp;amp;wordpfx&amp;amp;cnt;&lt;BR /&gt;%end;&lt;BR /&gt;%mend splitter;&lt;/P&gt;
&lt;P&gt;%macro quotelist(value, /* list of strings to be quoted */&lt;BR /&gt; sep=%NRSTR( ) /* separator character(s) between strings in value */&lt;BR /&gt; );&lt;BR /&gt; %local s1 s2 v1 v2 out;&lt;BR /&gt; &lt;BR /&gt; %let s1 = %str(%'%");&lt;BR /&gt; %let s2 = %str(%"%');&lt;BR /&gt; &lt;BR /&gt; %let v1 = %qsysfunc(translate(&amp;amp;value, &amp;amp;s1., &amp;amp;s2.));&lt;BR /&gt; %let v2 = %qsysfunc(tranwrd(%superq(v1), &amp;amp;sep., %nrstr(%" %") ));&lt;BR /&gt; %let out = %str(%")&amp;amp;v2.%str(%");&lt;BR /&gt; %unquote(&amp;amp;out.)&lt;BR /&gt;%mend;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let filter_1 = CREATIVE INDUSTRIES;&lt;BR /&gt;%let filter_2 = CULTURAL INDUSTRIES;&lt;BR /&gt;%let filter_3 = CREATIVE AND CULTURAL INDUSTRIES;&lt;BR /&gt;%let filter_4 = CREATIVE INDUSTRY;&lt;BR /&gt;%let filter_5 = CULTURAL INDUSTRY;&lt;BR /&gt;%let filter_6 = CREATIVE AND CULTURAL INDUSTRY;&lt;BR /&gt;%let filter_7 = MATH;&lt;BR /&gt;%let filter_8=;&lt;BR /&gt;%let filter_9=;&lt;BR /&gt;%let filter_10=;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro loop;&lt;BR /&gt;%let string=;&lt;/P&gt;
&lt;P&gt;/* up to ten filters can be submitted */&lt;BR /&gt;%do i = 1 %to 1;&lt;BR /&gt; %if %length(&amp;amp;&amp;amp;filter_&amp;amp;i) %then %do; /* we only want to loop over a filter if it contains a value */&lt;BR /&gt; %splitter(string=&amp;amp;&amp;amp;filter_&amp;amp;i,wordpfx=idx,dlm=' ');&lt;BR /&gt; %let word_cnt=%sysfunc(countw(&amp;amp;&amp;amp;filter_&amp;amp;i));&lt;BR /&gt; %put There are &amp;amp;word_cnt words in the string "&amp;amp;&amp;amp;filter_&amp;amp;i"; /* if the filter has a value then we have split out the keywords into their component parts */&lt;/P&gt;
&lt;P&gt;%do j = 1 %to &amp;amp;word_cnt;&lt;BR /&gt; %let substr_&amp;amp;j = INDEX(UPCASE(course_long_name),%quotelist(%qsysfunc(compbl(&amp;amp;&amp;amp;idx&amp;amp;j)))) &amp;gt;0;&lt;BR /&gt; %put Substr number &amp;amp;j is &amp;amp;&amp;amp;substr_&amp;amp;j;&lt;BR /&gt; %end;&lt;/P&gt;
&lt;P&gt;%if &amp;amp;word_cnt = 1 %then %do;&lt;BR /&gt; %let string&amp;amp;i = &amp;amp;substr_1;&lt;BR /&gt; %put STRING IS &amp;amp;&amp;amp;string&amp;amp;i;&lt;BR /&gt; %end;&lt;/P&gt;
&lt;P&gt;%if &amp;amp;word_cnt &amp;gt;1 %then %do k = 2 %to &amp;amp;word_cnt;&lt;BR /&gt; %let string = &amp;amp;substr_1 AND &amp;amp;&amp;amp;substr_&amp;amp;k;&lt;BR /&gt; %put STRING IS &amp;amp;string;&lt;BR /&gt; %end;&lt;BR /&gt; %end;&lt;BR /&gt;%end;&lt;BR /&gt;%mend loop;&lt;BR /&gt;%loop;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2016 10:43:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-macro-variables-within-a-do-loop/m-p/275610#M55125</guid>
      <dc:creator>kimdukes77</dc:creator>
      <dc:date>2016-06-07T10:43:40Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate macro variables within a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-macro-variables-within-a-do-loop/m-p/275614#M55126</link>
      <description>&lt;P&gt;You are really fighting the SAS system there with all that macro code. &amp;nbsp;Post an example test dataset for your have (in the form of a datastep) and what you want the output to look like. &amp;nbsp;From what I can gather from you text (as I am not trying to decipher that mass of %'s) you have a file with some values in, you read that in and then generate a proc sql with a where clause on each word. &amp;nbsp;So:&lt;/P&gt;
&lt;PRE&gt;/* Simulate data from file */
data have;
  length string $200;
  string="ACURA,AUDI"; output;
  string="BMW,CADILAC"; output;
run;

/* Generate necessary code */
data _null_;
  set have;
  call execute(cat('proc sql; create table OUT',strip(_n_),' as select * from SASHELP.CARS where upcase(MAKE) in ('));
  do i=1 to countw(string);
    if i &amp;gt; 1 then call execute(',');
    call execute(quote(scan(string,i,',')));
  end;
  call execute(');quit;');
run;&lt;/PRE&gt;
&lt;P&gt;This uses simple Base SAS code to create the code necessary to subset sashelp.cars based on each string in the input dataset. &amp;nbsp;So the first row it will create a proc sql, subset where in ("AUCRA","AUDI"), then next creates a proc sql for the next obs and so on.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2016 10:56:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-macro-variables-within-a-do-loop/m-p/275614#M55126</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-06-07T10:56:16Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate macro variables within a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-macro-variables-within-a-do-loop/m-p/275627#M55133</link>
      <description>&lt;P&gt;Yes it is a bit messy isn't it &lt;span class="lia-unicode-emoji" title=":confused_face:"&gt;😕&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So sample dataset below:-&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data courses;&lt;BR /&gt; length course_long_name $200;&lt;BR /&gt; course_long_name="Creative Writing"; output;&lt;BR /&gt; course_long_name="Creative Industry"; output;&lt;BR /&gt; course_long_name="Creative and Cultural Industry"; output;&lt;BR /&gt; course_long_name="Creative Media"; output;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let filter_1 = CREATIVE INDUSTRY;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Resulting output dataset would extract&amp;nbsp;the second and third courses as they contain occurences of the requested filter keywords.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kim&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2016 11:58:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-macro-variables-within-a-do-loop/m-p/275627#M55133</guid>
      <dc:creator>kimdukes77</dc:creator>
      <dc:date>2016-06-07T11:58:34Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate macro variables within a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-macro-variables-within-a-do-loop/m-p/275638#M55136</link>
      <description>&lt;P&gt;Well, I still don't see why filter is in a macro variable, easier to put these type of data elements in a dataset - thats what datasets are for. &amp;nbsp;However per your guidance, this should work:&lt;/P&gt;
&lt;PRE&gt;%let filter_1 = CREATIVE INDUSTRY;
data courses;
  length course_long_name $200;
  course_long_name="Creative Writing"; output;
  course_long_name="Creative Industry"; output;
  course_long_name="Creative and Cultural Industry"; output;
  course_long_name="Creative Media"; output;
run;

data want;
  set courses;
  found=0;
  do i=1 to countw("&amp;amp;filter_1.");
    if index(upcase(course_long_name),scan("&amp;amp;filter_1.",i," ")) &amp;gt; 0 then found=found+1;
  end;
  if found=countw("&amp;amp;filter_1.") then result="Found";
run;&lt;/PRE&gt;</description>
      <pubDate>Tue, 07 Jun 2016 12:57:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-macro-variables-within-a-do-loop/m-p/275638#M55136</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-06-07T12:57:34Z</dc:date>
    </item>
  </channel>
</rss>

