<?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 count number of variables in a list. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-count-number-of-variables-in-a-list/m-p/876697#M346340</link>
    <description>&lt;P&gt;How old is that paper?&amp;nbsp; Perhaps it predates the addition of the %SYSFUNC() macro function because you can just use it to call COUNTW() to count how many words are in a string.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since the other code is generating the list of names using SPACE as the delimiter you should tell COUNTW() to use SPACE as the delimiter.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't really need to create a macro for something so simple. Just replace:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%n_cntwrd(string=&amp;amp;thesedat, cntvar=VARCNT)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;With&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let VARCNT=%sysfunc(countw(&amp;amp;thesedat,%str( )));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you did want to mimic the N_CNTWRD macro perhaps it might look like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro n_cntwrd(string,cntvar);
%if not %symexist(&amp;amp;cntvar) %then %global &amp;amp;cntvar;
%let &amp;amp;cntvar=%sysfunc(countw(&amp;amp;string,%str( )));
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In reality for the example macro n that paper there was no need call that n_cntwrd macro at all.&lt;/P&gt;
&lt;P&gt;PROC SQL already counts for you into the macro variable SQLOBS.&amp;nbsp; So to get the count and the list of variables in a dataset into macro variables just use:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
%let thesedat=;
select nliteral(NAME)
  into :thesedat separated by ' '
  from DATES
  where libname=%upcase("&amp;amp;lib")
    and memname=%upcase("&amp;amp;indata")
;  
%let varcnt=&amp;amp;sqlobs;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 19 May 2023 16:42:14 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-05-19T16:42:14Z</dc:date>
    <item>
      <title>Macro to count number of variables in a list.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-count-number-of-variables-in-a-list/m-p/876690#M346336</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; I am going through the code at:&lt;/P&gt;&lt;P&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings/proceedings/sugi26/p098-26.pdf" target="_blank"&gt;https://support.sas.com/resources/papers/proceedings/proceedings/sugi26/p098-26.pdf&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In that, they are calling a macro&amp;nbsp;&amp;nbsp;N_CNTWRD&amp;nbsp; that counts the number of variables in a specified list. It takes two parameters, first one the list and second one to store the count.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone help me how to write this macro N_CNTWRD?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2023 15:45:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-count-number-of-variables-in-a-list/m-p/876690#M346336</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-05-19T15:45:23Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to count number of variables in a list.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-count-number-of-variables-in-a-list/m-p/876691#M346337</link>
      <description>&lt;P&gt;Here's a simple way to count number of variables in a list (although I assume the list is a macro variable itself, you really ought to give an example)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let list=temperature humidity pressure;

%let n_words=%sysfunc(countw(&amp;amp;list));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 19 May 2023 15:50:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-count-number-of-variables-in-a-list/m-p/876691#M346337</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-05-19T15:50:10Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to count number of variables in a list.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-count-number-of-variables-in-a-list/m-p/876697#M346340</link>
      <description>&lt;P&gt;How old is that paper?&amp;nbsp; Perhaps it predates the addition of the %SYSFUNC() macro function because you can just use it to call COUNTW() to count how many words are in a string.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since the other code is generating the list of names using SPACE as the delimiter you should tell COUNTW() to use SPACE as the delimiter.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't really need to create a macro for something so simple. Just replace:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%n_cntwrd(string=&amp;amp;thesedat, cntvar=VARCNT)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;With&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let VARCNT=%sysfunc(countw(&amp;amp;thesedat,%str( )));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you did want to mimic the N_CNTWRD macro perhaps it might look like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro n_cntwrd(string,cntvar);
%if not %symexist(&amp;amp;cntvar) %then %global &amp;amp;cntvar;
%let &amp;amp;cntvar=%sysfunc(countw(&amp;amp;string,%str( )));
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In reality for the example macro n that paper there was no need call that n_cntwrd macro at all.&lt;/P&gt;
&lt;P&gt;PROC SQL already counts for you into the macro variable SQLOBS.&amp;nbsp; So to get the count and the list of variables in a dataset into macro variables just use:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
%let thesedat=;
select nliteral(NAME)
  into :thesedat separated by ' '
  from DATES
  where libname=%upcase("&amp;amp;lib")
    and memname=%upcase("&amp;amp;indata")
;  
%let varcnt=&amp;amp;sqlobs;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2023 16:42:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-count-number-of-variables-in-a-list/m-p/876697#M346340</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-19T16:42:14Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to count number of variables in a list.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-count-number-of-variables-in-a-list/m-p/876698#M346341</link>
      <description>&lt;P&gt;Don't actually need a macro COUNTW is datastep function. Provide a list of values in a variable or literal string. You can even specify specific delimiters if needed.&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>Fri, 19 May 2023 16:08:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-count-number-of-variables-in-a-list/m-p/876698#M346341</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-05-19T16:08:30Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to count number of variables in a list.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-count-number-of-variables-in-a-list/m-p/876699#M346342</link>
      <description>&lt;P&gt;&lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p18xi2516ihygyn1qg1b1nby326k.htm" target="_self"&gt;COUNTW Function&lt;/A&gt;&amp;nbsp;documentation&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2023 16:19:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-count-number-of-variables-in-a-list/m-p/876699#M346342</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2023-05-19T16:19:59Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to count number of variables in a list.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-count-number-of-variables-in-a-list/m-p/876709#M346345</link>
      <description>Thank you very much Tom. Both the solutions for n_cntwrd worked.</description>
      <pubDate>Fri, 19 May 2023 17:06:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-count-number-of-variables-in-a-list/m-p/876709#M346345</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-05-19T17:06:16Z</dc:date>
    </item>
  </channel>
</rss>

