<?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: Countw within a macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Countw-within-a-macro/m-p/298716#M62858</link>
    <description>&lt;P&gt;Megan,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Loko actually gave you the correct answer.&amp;nbsp; Look at the difference between the macro call that didn't work, vs. the one that did work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%test (varlist=); &amp;nbsp; /* did not work */&lt;/P&gt;
&lt;P&gt;%test;&amp;nbsp; /* did work */&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first macro call uses the macro but overrides the default value for &amp;amp;VARLIST, giving it a null value.&amp;nbsp; The second macro call uses the default value for &amp;amp;VARLIST from when the macro was defined.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;COUNTW will have trouble when it encounters a null value for &amp;amp;VARLIST.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Finally, note that you may not need this macro at all.&amp;nbsp; The IN operator in a WHERE clause does not require commas to separate values.&amp;nbsp; Spaces are usually fine.&lt;/P&gt;</description>
    <pubDate>Thu, 15 Sep 2016 16:28:03 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2016-09-15T16:28:03Z</dc:date>
    <item>
      <title>Countw within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Countw-within-a-macro/m-p/298678#M62846</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I saw a couple of other posts on countw within a macro, but none addressed what i'm seeing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've inherited code, and i understand what it's doing (added commas between the list of words), but i keep getting errors when i try to run it.&amp;nbsp; Don't ask why one macro variable is another macro variable that holds the list.&amp;nbsp; I havent' gotten that far in cleaning it up yet...&lt;BR /&gt;&lt;BR /&gt;%let _mClist &amp;nbsp;&amp;nbsp; &amp;nbsp;= PROVHCFA seq_keyc HCFASAF DISCSTAT PDGNS_CD;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;*define variables of interest;&lt;BR /&gt;&lt;BR /&gt;%macro test (varList = &amp;amp;_mClist);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;%put _mClist &amp;nbsp;&amp;nbsp; &amp;nbsp;= &amp;amp;varList;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;*define variables of interest;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;%let _mSelectedVar =;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;%do i = 1 %to %sysfunc(countw(&amp;amp;varList.));&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;%if &amp;amp;i &amp;lt; %sysfunc(countw(&amp;amp;varList.)) %then&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; %let _mSelectedVar = &amp;amp;_mSelectedVar %scan(&amp;amp;varList,%eval(&amp;amp;i)),;&amp;nbsp;&amp;nbsp; &amp;nbsp;/*if&amp;nbsp; &amp;lt; 5 then _mSelectedVar = _mSelectedVar + a comma*/&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;%else %let _mSelectedVar = &amp;amp;_mSelectedVar %scan(&amp;amp;varList,%eval(&amp;amp;i));&amp;nbsp;&amp;nbsp; &amp;nbsp;/*if last variable (ie, 5 &amp;lt; 5 will be false), then just _mSelectedVar, no comma*/&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;%end;&lt;BR /&gt;%mend;&lt;BR /&gt;%test (varlist=);&lt;BR /&gt;&lt;BR /&gt;When i run that, the log says:&lt;/P&gt;&lt;P&gt;ERROR: The function COUNTW referenced by the %SYSFUNC or %QSYSFUNC macro function has too few&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arguments.&lt;BR /&gt;ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; operand is required. The condition was: %sysfunc(countw(&amp;amp;varList.))&lt;BR /&gt;ERROR: The %TO value of the %DO I loop is invalid.&lt;BR /&gt;ERROR: The macro TEST will stop executing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But i've seen countw with just a single macro input before, so i dont' know what it thinks is missing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So then i replaced the macro variable with the actual variable list, and i get this in the log after each cycle:&lt;/P&gt;&lt;P&gt;ERROR: The function COUNTW referenced by the %SYSFUNC or %QSYSFUNC macro function has too few&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arguments.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;BR /&gt;Megan&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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Sep 2016 15:12:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Countw-within-a-macro/m-p/298678#M62846</guid>
      <dc:creator>MeganE</dc:creator>
      <dc:date>2016-09-15T15:12:19Z</dc:date>
    </item>
    <item>
      <title>Re: Countw within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Countw-within-a-macro/m-p/298682#M62849</link>
      <description>&lt;P&gt;So, messing around with it still, i removed the input macro reference on the %macro line, and set it up as a %let instead for testing, and it ran without error.&amp;nbsp; I changed nothing else.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So the code works now, but if someone knows WHY!?!?!?!?!?!?, that would be incredibly helpful.&amp;nbsp; &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let varlist &amp;nbsp;&amp;nbsp; &amp;nbsp;= PROVHCFA seq_keyc HCFASAF DISCSTAT PDGNS_CD;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;*define variables of interest;&lt;BR /&gt;&lt;BR /&gt;%macro test;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;%put _mClist &amp;nbsp;&amp;nbsp; &amp;nbsp;= &amp;amp;varList;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;*define variables of interest;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;%let _mSelectedVar =;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;%let end=%sysfunc(countw(&amp;amp;varList.));&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;%do i = 1 %to &amp;amp;end.;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;%if &amp;amp;i &amp;lt; %sysfunc(countw(&amp;amp;varList.)) %then&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; %let _mSelectedVar = &amp;amp;_mSelectedVar %scan(&amp;amp;varList,%eval(&amp;amp;i)),;&amp;nbsp;&amp;nbsp; &amp;nbsp;/*if 1 &amp;lt; 5 then _mSelectedVar = _mSelectedVar + a comma*/&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;%else %let _mSelectedVar = &amp;amp;_mSelectedVar %scan(&amp;amp;varList,%eval(&amp;amp;i));&amp;nbsp;&amp;nbsp; &amp;nbsp;/*if last variable (ie, 5 &amp;lt; 5 will be false), then just _mSelectedVar, no comma*/&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;%end;&lt;BR /&gt;%mend;&lt;BR /&gt;%test;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Sep 2016 15:19:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Countw-within-a-macro/m-p/298682#M62849</guid>
      <dc:creator>MeganE</dc:creator>
      <dc:date>2016-09-15T15:19:36Z</dc:date>
    </item>
    <item>
      <title>Re: Countw within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Countw-within-a-macro/m-p/298683#M62850</link>
      <description>&lt;P&gt;hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;when you call the macro like you do - &lt;STRONG&gt;%test (varlist=);&lt;/STRONG&gt; you set the value of the macro variable &lt;STRONG&gt;varlist&lt;/STRONG&gt; &lt;STRONG&gt;to blank.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;if you want it to have the value of _&lt;STRONG&gt;mClist, call the macro: %test ();&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Sep 2016 15:21:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Countw-within-a-macro/m-p/298683#M62850</guid>
      <dc:creator>Loko</dc:creator>
      <dc:date>2016-09-15T15:21:01Z</dc:date>
    </item>
    <item>
      <title>Re: Countw within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Countw-within-a-macro/m-p/298685#M62852</link>
      <description>I dont' know what you mean. When it wasn't working, varlist was called another macro:&lt;BR /&gt;(varList = &amp;amp;_mClist)</description>
      <pubDate>Thu, 15 Sep 2016 15:23:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Countw-within-a-macro/m-p/298685#M62852</guid>
      <dc:creator>MeganE</dc:creator>
      <dc:date>2016-09-15T15:23:55Z</dc:date>
    </item>
    <item>
      <title>Re: Countw within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Countw-within-a-macro/m-p/298700#M62855</link>
      <description>&lt;P&gt;HOW are you actually attempting to use this macro? I do not see how anything sees the result of this this variable unless you are dangerously expecting a global macro variable _mSelectedVar to exist before calling the macro.&lt;/P&gt;
&lt;P&gt;And what was the value of &amp;amp;_mClist when the error occurs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You might want to set Option Mprint Symbolgen; then re-run the code and show us the log.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Sep 2016 15:53:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Countw-within-a-macro/m-p/298700#M62855</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-09-15T15:53:49Z</dc:date>
    </item>
    <item>
      <title>Re: Countw within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Countw-within-a-macro/m-p/298702#M62856</link>
      <description>I didn't write the code, i'm just trying to recreate it. It builds a list of variables that don't have commas, with commas, so they can be used in a proc sql step a bit further on. I got it to run though, thanks.</description>
      <pubDate>Thu, 15 Sep 2016 15:55:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Countw-within-a-macro/m-p/298702#M62856</guid>
      <dc:creator>MeganE</dc:creator>
      <dc:date>2016-09-15T15:55:28Z</dc:date>
    </item>
    <item>
      <title>Re: Countw within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Countw-within-a-macro/m-p/298716#M62858</link>
      <description>&lt;P&gt;Megan,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Loko actually gave you the correct answer.&amp;nbsp; Look at the difference between the macro call that didn't work, vs. the one that did work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%test (varlist=); &amp;nbsp; /* did not work */&lt;/P&gt;
&lt;P&gt;%test;&amp;nbsp; /* did work */&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first macro call uses the macro but overrides the default value for &amp;amp;VARLIST, giving it a null value.&amp;nbsp; The second macro call uses the default value for &amp;amp;VARLIST from when the macro was defined.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;COUNTW will have trouble when it encounters a null value for &amp;amp;VARLIST.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Finally, note that you may not need this macro at all.&amp;nbsp; The IN operator in a WHERE clause does not require commas to separate values.&amp;nbsp; Spaces are usually fine.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Sep 2016 16:28:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Countw-within-a-macro/m-p/298716#M62858</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-09-15T16:28:03Z</dc:date>
    </item>
    <item>
      <title>Re: Countw within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Countw-within-a-macro/m-p/298718#M62859</link>
      <description>As far as i know, you can't separate variables in the select statement of proc sql without commas. That's where this string is being used.</description>
      <pubDate>Thu, 15 Sep 2016 16:30:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Countw-within-a-macro/m-p/298718#M62859</guid>
      <dc:creator>MeganE</dc:creator>
      <dc:date>2016-09-15T16:30:00Z</dc:date>
    </item>
    <item>
      <title>Re: Countw within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Countw-within-a-macro/m-p/298719#M62860</link>
      <description>&lt;P&gt;You're right about that.&amp;nbsp; I was picturing values of a variable.&amp;nbsp; The variable names require the commas.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Sep 2016 16:32:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Countw-within-a-macro/m-p/298719#M62860</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-09-15T16:32:09Z</dc:date>
    </item>
  </channel>
</rss>

