<?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 DO LOOP, parsing a string with a given deliminator in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/MACRO-DO-LOOP-parsing-a-string-with-a-given-deliminator/m-p/535023#M146855</link>
    <description>I have seen them,  _all_, _local_, _global_, _AUTOMATIC_ ...none of them are quite the same as a string pattern match.  thanks for the pointers.  Local is very close though...</description>
    <pubDate>Tue, 12 Feb 2019 21:21:12 GMT</pubDate>
    <dc:creator>kjohnsonm</dc:creator>
    <dc:date>2019-02-12T21:21:12Z</dc:date>
    <item>
      <title>MACRO DO LOOP, parsing a string with a given deliminator</title>
      <link>https://communities.sas.com/t5/SAS-Programming/MACRO-DO-LOOP-parsing-a-string-with-a-given-deliminator/m-p/534089#M146480</link>
      <description>&lt;P&gt;Hello All,&lt;/P&gt;
&lt;P&gt;So I want to parse a string I hand off to a macro into keys for proc SQL to do some duplicate checking can someone give me some pointers?&lt;/P&gt;
&lt;P&gt;I have a counter to control the do loop but if there is an easer way I am game.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;have this code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;libname my_lib odbc dsn=my_db schema = dbo;

%macro count(list=);
 %eval(%sysfunc(count(%cmpres(&amp;amp;list),%str(|)))+1)
%mend count;

%macro dups_check(file, key);
%let My_count= %count(list=&amp;amp;key);
%put &amp;amp;My_count. ;
do i=1 to &amp;amp;My_count.;
	%let key&amp;amp;i. = %scan(key, &amp;amp;i., '|')
	%put key&amp;amp;i.
end;


/* more proc sql here to find dups etc */

/* I want to be able to use the parsed keys here &amp;amp;key1 - &amp;amp;keyN  in my proc sql automated code */

%mend dups_check;

/* The above code should be able to handle any kind of variation like these items below I want to execute */
&lt;BR /&gt;%dups_check(my_lib.my_file0,key1|key2|key3);
%dups_check(my_lib.my_file1,key1|key2);
/*...*/
%dups_check(my_lib.my_fileN,key1|key2|key3|key4|key5|...|keyN);&lt;BR /&gt;&lt;BR /&gt;/* I know there are limits to string length that the memory will handle but I am not getting over 7-8 keys so I think I am safe in all cases */

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 Feb 2019 23:25:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/MACRO-DO-LOOP-parsing-a-string-with-a-given-deliminator/m-p/534089#M146480</guid>
      <dc:creator>kjohnsonm</dc:creator>
      <dc:date>2019-02-08T23:25:24Z</dc:date>
    </item>
    <item>
      <title>Re: MACRO DO LOOP, parsing a string with a given deliminator</title>
      <link>https://communities.sas.com/t5/SAS-Programming/MACRO-DO-LOOP-parsing-a-string-with-a-given-deliminator/m-p/534090#M146481</link>
      <description>&lt;P&gt;oops and should handle a single key too.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Feb 2019 23:27:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/MACRO-DO-LOOP-parsing-a-string-with-a-given-deliminator/m-p/534090#M146481</guid>
      <dc:creator>kjohnsonm</dc:creator>
      <dc:date>2019-02-08T23:27:22Z</dc:date>
    </item>
    <item>
      <title>Re: MACRO DO LOOP, parsing a string with a given deliminator</title>
      <link>https://communities.sas.com/t5/SAS-Programming/MACRO-DO-LOOP-parsing-a-string-with-a-given-deliminator/m-p/534095#M146484</link>
      <description>&lt;P&gt;You may want to use COUNTW instead of count. Also for a large number of things the macro processor strips out leading and trailing spaces.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro dummy(list=);

%do i= 1 %to %sysfunc(countw(&amp;amp;list.,|));
   %let word= %scan(&amp;amp;list,&amp;amp;i.,|);
   %put Word &amp;amp;i. is &amp;amp;word.;
%end;
%mend;

%dummy(list= This |is|    varying  |numbers | of delimited | words);
&lt;/PRE&gt;
&lt;P&gt;If you add another character after the &amp;amp;word. in the %put such as &amp;amp;word.^ you will see there are no trailing spaces.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Feb 2019 23:40:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/MACRO-DO-LOOP-parsing-a-string-with-a-given-deliminator/m-p/534095#M146484</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-02-08T23:40:53Z</dc:date>
    </item>
    <item>
      <title>Re: MACRO DO LOOP, parsing a string with a given deliminator</title>
      <link>https://communities.sas.com/t5/SAS-Programming/MACRO-DO-LOOP-parsing-a-string-with-a-given-deliminator/m-p/534140#M146499</link>
      <description>&lt;P&gt;We can clean up your code a little using newer SAS functionality to call the COUNTW() function to get the number of keys.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro dups_check(file, key);
%local My_count i ;

%let My_count= %sysfunc(countw(&amp;amp;key,|));
%do i=1 %to &amp;amp;My_count;
  %local key&amp;amp;i ;
  %let key&amp;amp;i = %scan(&amp;amp;key, &amp;amp;i, |);
%end;

....
%mend dups_check;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that your COUNT() macro is not really needed but if you want to keep it you should upgrade it to use COUNTW() and allow caller to specify the delimiter(s).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro count(list=,dlm=|);
%sysfunc(countw(%superq(list),&amp;amp;dlm))
%mend count;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 09 Feb 2019 15:13:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/MACRO-DO-LOOP-parsing-a-string-with-a-given-deliminator/m-p/534140#M146499</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-02-09T15:13:57Z</dc:date>
    </item>
    <item>
      <title>Re: MACRO DO LOOP, parsing a string with a given deliminator</title>
      <link>https://communities.sas.com/t5/SAS-Programming/MACRO-DO-LOOP-parsing-a-string-with-a-given-deliminator/m-p/534954#M146843</link>
      <description>&lt;P&gt;Thank you both, I took a little from both of your samples. I think the second code sample fits my needs the best. I need to keep each macro var key for use in my proc sql later so this works the best for my needs. -KJ&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro dups_check(Mfile, Mkey);
%local My_count i ;
%let My_count= %sysfunc(countw(&amp;amp;Mkey,|));
%put &amp;amp;My_count.;
%do i=1 %to &amp;amp;My_count;
  %local Mkey&amp;amp;i ;
  %let Mkey&amp;amp;i = %scan(&amp;amp;Mkey, &amp;amp;i, |);
%end;
/*I don't like this one much if 5 or less keys I get warnings */
%put &amp;amp;Mkey1 &amp;amp;Mkey2 &amp;amp;Mkey3 &amp;amp;Mkey4 &amp;amp;Mkey5 &amp;amp;Mkey6;
%put &amp;amp;Mkey : ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;BR /&gt;/* over kill, too verbose */
/*%put _ALL_;*/

/*my Proc SQL here ...*/

%mend dups_check;

/*%dups_check(file, key1_word|key2_word_is|key3_word_varying|key_4_word_numbers|key_5_of delimited|key6_words);*/

%dups_check(file, key1_word|key2_word_is|key3_word_varying|key_4_word_numbers);

%dups_check(file, key1_word|key2_word_is|);
%dups_check(file, key1_word|key2_word_is);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Feb 2019 18:55:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/MACRO-DO-LOOP-parsing-a-string-with-a-given-deliminator/m-p/534954#M146843</guid>
      <dc:creator>kjohnsonm</dc:creator>
      <dc:date>2019-02-12T18:55:28Z</dc:date>
    </item>
    <item>
      <title>Re: MACRO DO LOOP, parsing a string with a given deliminator</title>
      <link>https://communities.sas.com/t5/SAS-Programming/MACRO-DO-LOOP-parsing-a-string-with-a-given-deliminator/m-p/535010#M146851</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;LI-CODE lang="sas"&gt;&lt;SPAN class="token comment"&gt;/*I don't like this one much if 5 or less keys I get warnings */&lt;/SPAN&gt;
&lt;SPAN class="token macrostatement"&gt;%put&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;Mkey1 &lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;Mkey2 &lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;Mkey3 &lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;Mkey4 &lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;Mkey5 &lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;Mkey6&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token macrostatement"&gt;%put&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;Mkey : &lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/LI-CODE&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Not sure why you are putting them to the log, but if you want to let that %PUT statement run without error then make sure that at least those 6 macro variables exist.&amp;nbsp; Add them to your first %LOCAL statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%local My_count i Mkey1 Mkey2 Mkey3 Mkey4 Mkey5 Mkey6;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Feb 2019 21:04:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/MACRO-DO-LOOP-parsing-a-string-with-a-given-deliminator/m-p/535010#M146851</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-02-12T21:04:05Z</dc:date>
    </item>
    <item>
      <title>Re: MACRO DO LOOP, parsing a string with a given deliminator</title>
      <link>https://communities.sas.com/t5/SAS-Programming/MACRO-DO-LOOP-parsing-a-string-with-a-given-deliminator/m-p/535019#M146852</link>
      <description>&lt;P&gt;I was checking the status on the fly... I thought something like the maro var pattern &amp;amp;Mkey: ; would display what is in memory for items1 - - n, but it doesn't. It was just an assumption it would work since these two commands are valid.&lt;BR /&gt;&lt;BR /&gt;proc datasets lib=work;&lt;BR /&gt;delete&lt;BR /&gt;MY_ipeds_ethnic_group_NULL_SET&lt;BR /&gt;MY_ipeds_ethnic_group_NULL_SET1&lt;BR /&gt;MY_NULL_XD_SCHOOL&lt;BR /&gt;;RUN;quit;&lt;BR /&gt;&lt;BR /&gt;proc datasets lib=work;&lt;BR /&gt;delete MY_: ;&lt;BR /&gt;RUN;quit;&lt;BR /&gt;&lt;BR /&gt;I only dislike the fact I cannot seem to find a one line-er to display my results for testing. sorry if I confused you. thank you the code works just fine as it is, just me OCD-ing.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Feb 2019 21:16:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/MACRO-DO-LOOP-parsing-a-string-with-a-given-deliminator/m-p/535019#M146852</guid>
      <dc:creator>kjohnsonm</dc:creator>
      <dc:date>2019-02-12T21:16:32Z</dc:date>
    </item>
    <item>
      <title>Re: MACRO DO LOOP, parsing a string with a given deliminator</title>
      <link>https://communities.sas.com/t5/SAS-Programming/MACRO-DO-LOOP-parsing-a-string-with-a-given-deliminator/m-p/535020#M146853</link>
      <description>%put _local_;&lt;BR /&gt;</description>
      <pubDate>Tue, 12 Feb 2019 21:17:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/MACRO-DO-LOOP-parsing-a-string-with-a-given-deliminator/m-p/535020#M146853</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-02-12T21:17:38Z</dc:date>
    </item>
    <item>
      <title>Re: MACRO DO LOOP, parsing a string with a given deliminator</title>
      <link>https://communities.sas.com/t5/SAS-Programming/MACRO-DO-LOOP-parsing-a-string-with-a-given-deliminator/m-p/535023#M146855</link>
      <description>I have seen them,  _all_, _local_, _global_, _AUTOMATIC_ ...none of them are quite the same as a string pattern match.  thanks for the pointers.  Local is very close though...</description>
      <pubDate>Tue, 12 Feb 2019 21:21:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/MACRO-DO-LOOP-parsing-a-string-with-a-given-deliminator/m-p/535023#M146855</guid>
      <dc:creator>kjohnsonm</dc:creator>
      <dc:date>2019-02-12T21:21:12Z</dc:date>
    </item>
  </channel>
</rss>

