<?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: calculate number of arguments in  parameter &amp;amp;vector. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-arguments-in-parameter-amp-vector/m-p/474349#M121869</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you!&lt;/P&gt;
&lt;P&gt;As I understand it is enough to write the code:&lt;/P&gt;
&lt;P&gt;%let count= %sysfunc(countw(&amp;amp;vector));&lt;BR /&gt;%put &amp;amp;count.;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is the purpose of &amp;nbsp;the code:&lt;/P&gt;
&lt;P&gt;data _null_;&lt;BR /&gt;count = countw("&amp;amp;vector");&lt;BR /&gt;put count=;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;??&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;A PS here: as you can see from other posts, you need to take some precaution against the case where &amp;amp;vector is defined but empty if you do the count in macro. In the data step version, it simply does not matter. One of the reasons I like to do such manipulations/calculations/transforms in a data step with a final call symput:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
count = countw("&amp;amp;vector");
/* if needed, add further processing here */
call symputx('count',put(count,best.));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Data step programming simply does not have as many pitfalls.&lt;/P&gt;</description>
    <pubDate>Fri, 29 Jun 2018 06:05:43 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2018-06-29T06:05:43Z</dc:date>
    <item>
      <title>calculate number of arguments in  parameter &amp;vector.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-arguments-in-parameter-amp-vector/m-p/473976#M121711</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;Let's say that I define a parameter as following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let Vector=Reason1701 Reason1702 Reason1703 Reason1704 Reason1705 Reason1706&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Reason1707 Reason1708 Reason1709 Reason1710 Reason1711 Reason1712&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Reason1801 Reason1802 Reason1803 Reason1804 Reason1805;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to create a new parameter that will get value of number of arguments in parameter &amp;amp;vector.&lt;/P&gt;&lt;P&gt;I want that SAS will calculate number of arguments in&amp;nbsp;&lt;SPAN&gt; parameter &amp;amp;vector. (in this example:17 arguments)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Does anyone know how to do it?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jun 2018 06:01:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-arguments-in-parameter-amp-vector/m-p/473976#M121711</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2018-06-28T06:01:35Z</dc:date>
    </item>
    <item>
      <title>Re: calculate number of arguments in  parameter &amp;vector.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-arguments-in-parameter-amp-vector/m-p/473981#M121712</link>
      <description>&lt;P&gt;The countw function counts words in a string:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
count = countw("&amp;amp;vector");
put count=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since there is no %countw macro function, you have to use %sysfunc in a macro context:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let count = %sysfunc(countw(&amp;amp;vector));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 28 Jun 2018 06:48:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-arguments-in-parameter-amp-vector/m-p/473981#M121712</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-06-28T06:48:27Z</dc:date>
    </item>
    <item>
      <title>Re: calculate number of arguments in  parameter &amp;vector.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-arguments-in-parameter-amp-vector/m-p/473991#M121716</link>
      <description>&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;As I understand it is enough to write the code:&lt;/P&gt;&lt;P&gt;%let count= %sysfunc(countw(&amp;amp;vector));&lt;BR /&gt;%put &amp;amp;count.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What is the purpose of &amp;nbsp;the code:&lt;/P&gt;&lt;P&gt;data _null_;&lt;BR /&gt;count = countw("&amp;amp;vector");&lt;BR /&gt;put count=;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;??&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jun 2018 08:05:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-arguments-in-parameter-amp-vector/m-p/473991#M121716</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2018-06-28T08:05:08Z</dc:date>
    </item>
    <item>
      <title>Re: calculate number of arguments in  parameter &amp;vector.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-arguments-in-parameter-amp-vector/m-p/473992#M121717</link>
      <description>&lt;P&gt;Its called a datastep.&amp;nbsp; Its part of Base SAS programing - this being the core of the SAS product and the actual product.&amp;nbsp; The syntax is a lot simpler, not having all the %&amp;amp;%&amp;amp;'s, and its a lot more robust.&amp;nbsp; Always use Base SAS programming, and only use Macro when its adds some value to the code.&amp;nbsp; Lists of data are best put into datasets - these are things designed to hold data.&amp;nbsp; This makes working with that data far simpler and more robust.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jun 2018 08:14:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-arguments-in-parameter-amp-vector/m-p/473992#M121717</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-06-28T08:14:26Z</dc:date>
    </item>
    <item>
      <title>Re: calculate number of arguments in  parameter &amp;vector.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-arguments-in-parameter-amp-vector/m-p/474002#M121722</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you!&lt;/P&gt;
&lt;P&gt;As I understand it is enough to write the code:&lt;/P&gt;
&lt;P&gt;%let count= %sysfunc(countw(&amp;amp;vector));&lt;BR /&gt;%put &amp;amp;count.;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is the purpose of &amp;nbsp;the code:&lt;/P&gt;
&lt;P&gt;data _null_;&lt;BR /&gt;count = countw("&amp;amp;vector");&lt;BR /&gt;put count=;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;??&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It's just there to show you how the countw() function works in its natural environment, which is the data step.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jun 2018 08:40:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-arguments-in-parameter-amp-vector/m-p/474002#M121722</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-06-28T08:40:46Z</dc:date>
    </item>
    <item>
      <title>Re: calculate number of arguments in  parameter &amp;vector.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-arguments-in-parameter-amp-vector/m-p/474003#M121723</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;I need this new parameter (number of arguments) in order to create a flexible SAS program .&lt;/P&gt;&lt;P&gt;In this program user&amp;nbsp;define only one parameter called &amp;nbsp;vector and &amp;nbsp;SAS create another parameter automatically(count parameter).&lt;/P&gt;&lt;P&gt;As I see&amp;nbsp;I only use count as a parameter and don't use it in data step.&lt;/P&gt;&lt;P&gt;So actually I will only use code:&lt;/P&gt;&lt;P&gt;%let count= %sysfunc(countw(&amp;amp;vector));&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Thank you so much &amp;nbsp;and I have learned a lot from this forum and i love SAS&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, 28 Jun 2018 08:42:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-arguments-in-parameter-amp-vector/m-p/474003#M121723</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2018-06-28T08:42:13Z</dc:date>
    </item>
    <item>
      <title>Re: calculate number of arguments in  parameter &amp;vector.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-arguments-in-parameter-amp-vector/m-p/474007#M121724</link>
      <description>&lt;P&gt;So your not actually using that list of values?&amp;nbsp; If so why not just get the user to enter the number?&amp;nbsp; The reason why is your going to have to - to make any kind of robust programming - create a whole set of error checking on that macro variable.&amp;nbsp; What if the user enters:&lt;/P&gt;
&lt;P&gt;xyy123,4 xyz124 ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This will break your code.&amp;nbsp; If they enter too many your going to hit maximum line length.&amp;nbsp; There is a lot that can go wrong which you need to check for - needlessly if you not even using it.&amp;nbsp; Good planning, process planning is the way to do flexible programs.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jun 2018 09:13:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-arguments-in-parameter-amp-vector/m-p/474007#M121724</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-06-28T09:13:08Z</dc:date>
    </item>
    <item>
      <title>Re: calculate number of arguments in  parameter &amp;vector.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-arguments-in-parameter-amp-vector/m-p/474025#M121733</link>
      <description>&lt;P&gt;If your vector might sometimes have 0 elements, you will get an error:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;1    %let Vector=;
2    %put &amp;gt;&amp;gt;%sysfunc(countw(&amp;amp;vector))&amp;lt;&amp;lt; ;
ERROR: The function COUNTW referenced by the %SYSFUNC or %QSYSFUNC macro function has too few
       arguments.
&amp;gt;&amp;gt;.&amp;lt;&amp;lt;
&lt;/PRE&gt;
&lt;P&gt;I suppose it's because %sysfunc has no argument to pass to COUNTW.&amp;nbsp; As I'm often in the situation where I want to allow a list to have 0 or more elements, I often specify the word delimiter for COUNTW.&amp;nbsp; That way there is always an argument, and it's happy to return 0. :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;3    %put &amp;gt;&amp;gt;%sysfunc(countw(&amp;amp;vector,%str( )))&amp;lt;&amp;lt; ; *Space-delimited list;
&amp;gt;&amp;gt;0&amp;lt;&amp;lt;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jun 2018 11:18:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-arguments-in-parameter-amp-vector/m-p/474025#M121733</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2018-06-28T11:18:01Z</dc:date>
    </item>
    <item>
      <title>Re: calculate number of arguments in  parameter &amp;vector.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-arguments-in-parameter-amp-vector/m-p/474345#M121857</link>
      <description>&lt;P&gt;Adding the actual delimiter you want is useful since it can avoid wrong counts.&lt;/P&gt;
&lt;P&gt;But to avoid the error when the macro variable is empty you just need to add ANYTHING.&amp;nbsp; So all of these will also work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%put %sysfunc(countw(&amp;amp;x,));
%put %sysfunc(countw(%str()&amp;amp;x));
%put %sysfunc(countw(&amp;amp;x%str()));
%put %sysfunc(countw(%str(&amp;amp;x)));
%put %sysfunc(countw(%superq(x)));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Jun 2018 05:33:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-arguments-in-parameter-amp-vector/m-p/474345#M121857</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-06-29T05:33:10Z</dc:date>
    </item>
    <item>
      <title>Re: calculate number of arguments in  parameter &amp;vector.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-arguments-in-parameter-amp-vector/m-p/474349#M121869</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you!&lt;/P&gt;
&lt;P&gt;As I understand it is enough to write the code:&lt;/P&gt;
&lt;P&gt;%let count= %sysfunc(countw(&amp;amp;vector));&lt;BR /&gt;%put &amp;amp;count.;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is the purpose of &amp;nbsp;the code:&lt;/P&gt;
&lt;P&gt;data _null_;&lt;BR /&gt;count = countw("&amp;amp;vector");&lt;BR /&gt;put count=;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;??&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;A PS here: as you can see from other posts, you need to take some precaution against the case where &amp;amp;vector is defined but empty if you do the count in macro. In the data step version, it simply does not matter. One of the reasons I like to do such manipulations/calculations/transforms in a data step with a final call symput:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
count = countw("&amp;amp;vector");
/* if needed, add further processing here */
call symputx('count',put(count,best.));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Data step programming simply does not have as many pitfalls.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jun 2018 06:05:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-arguments-in-parameter-amp-vector/m-p/474349#M121869</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-06-29T06:05:43Z</dc:date>
    </item>
    <item>
      <title>Re: calculate number of arguments in  parameter &amp;vector.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-arguments-in-parameter-amp-vector/m-p/474397#M121891</link>
      <description>&lt;P&gt;Actually %sysfunc(countw(&amp;amp;x,)) doesn't work.&amp;nbsp; I think it accepts a null value as the argument for the delimiter, so it returns 1 any time the first argument is not null, instead of using the default delimiters.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;1    %let x=hello world.how|are-you ;
2    %put &amp;gt;&amp;gt;%sysfunc(countw(&amp;amp;x,))&amp;lt;&amp;lt;;         *Null delimiter, returns 1 ;
&amp;gt;&amp;gt;1&amp;lt;&amp;lt;
3    %put &amp;gt;&amp;gt;%sysfunc(countw(&amp;amp;x,%str()))&amp;lt;&amp;lt; ;  *Null delimiter, returns 1 ;
&amp;gt;&amp;gt;1&amp;lt;&amp;lt;
4    %put &amp;gt;&amp;gt;%sysfunc(countw(&amp;amp;x,%str( )))&amp;lt;&amp;lt; ; *Blank delimiter, returns 2 ;
&amp;gt;&amp;gt;2&amp;lt;&amp;lt;
5    %put &amp;gt;&amp;gt;%sysfunc(countw(%str()&amp;amp;x))&amp;lt;&amp;lt;;    *Default delimiters, returns 5 ;
&amp;gt;&amp;gt;5&amp;lt;&amp;lt;
6    %put &amp;gt;&amp;gt;%sysfunc(countw(&amp;amp;x%str()))&amp;lt;&amp;lt;;    *Default delimiters, returns 5 ;
&amp;gt;&amp;gt;5&amp;lt;&amp;lt;
7    %put &amp;gt;&amp;gt;%sysfunc(countw(%str(&amp;amp;x)))&amp;lt;&amp;lt;;    *Default delimiters, returns 5 ;
&amp;gt;&amp;gt;5&amp;lt;&amp;lt;
8    %put &amp;gt;&amp;gt;%sysfunc(countw(%superq(x)))&amp;lt;&amp;lt;;  *Default delimiters, returns 5 ;
&amp;gt;&amp;gt;5&amp;lt;&amp;lt;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jun 2018 11:12:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-arguments-in-parameter-amp-vector/m-p/474397#M121891</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2018-06-29T11:12:49Z</dc:date>
    </item>
  </channel>
</rss>

