<?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 Count function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-function/m-p/69361#M15025</link>
    <description>Hi all,&lt;BR /&gt;
&lt;BR /&gt;
I have the following problem. I have a string variable which i want to search for a list of specific expressions with the help of the count function. My expressions contain sometimes "," or "(" or ")" or other signs which result in an error for this function. I checked the documentation for the count function, however it sounds like greek to me &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &lt;BR /&gt;
How do I have to specify the function in order to work properly with these other signs?&lt;BR /&gt;
&lt;BR /&gt;
Thanks,&lt;BR /&gt;
&lt;BR /&gt;
Valentin</description>
    <pubDate>Wed, 25 May 2011 14:49:24 GMT</pubDate>
    <dc:creator>Valentin_HU</dc:creator>
    <dc:date>2011-05-25T14:49:24Z</dc:date>
    <item>
      <title>Count function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-function/m-p/69361#M15025</link>
      <description>Hi all,&lt;BR /&gt;
&lt;BR /&gt;
I have the following problem. I have a string variable which i want to search for a list of specific expressions with the help of the count function. My expressions contain sometimes "," or "(" or ")" or other signs which result in an error for this function. I checked the documentation for the count function, however it sounds like greek to me &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &lt;BR /&gt;
How do I have to specify the function in order to work properly with these other signs?&lt;BR /&gt;
&lt;BR /&gt;
Thanks,&lt;BR /&gt;
&lt;BR /&gt;
Valentin</description>
      <pubDate>Wed, 25 May 2011 14:49:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-function/m-p/69361#M15025</guid>
      <dc:creator>Valentin_HU</dc:creator>
      <dc:date>2011-05-25T14:49:24Z</dc:date>
    </item>
    <item>
      <title>Re: Count function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-function/m-p/69362#M15026</link>
      <description>Hello Valentin,&lt;BR /&gt;
&lt;BR /&gt;
This is an example of using COUNT:&lt;BR /&gt;
[pre]&lt;BR /&gt;
data i;&lt;BR /&gt;
  s=",,,,)))((";&lt;BR /&gt;
  comma=COUNT(s,",");&lt;BR /&gt;
  close=COUNT(s,")");&lt;BR /&gt;
  open=COUNT(s,"(");&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
Sincerely,&lt;BR /&gt;
SPR</description>
      <pubDate>Wed, 25 May 2011 15:45:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-function/m-p/69362#M15026</guid>
      <dc:creator>SPR</dc:creator>
      <dc:date>2011-05-25T15:45:19Z</dc:date>
    </item>
    <item>
      <title>Re: Count function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-function/m-p/69363#M15027</link>
      <description>HI SPR,&lt;BR /&gt;
&lt;BR /&gt;
thanks for your answer. However, I think I didn't make my question clear enqough. Sorry for that! I'm searching in a variable for a list of expressions (the list is a macro variable that contains expressions which are delimited by a "*" and each word is searched with the help of a loop), and some of these expressions contain closed or open brackets as well as commas (e.g., "have, in the past,"). When I search for these expressions in a macro (text is the variable I am searching) with &lt;BR /&gt;
count_word=count(text,&amp;amp;expressions);&lt;BR /&gt;
then SAS gives back an error: "More positional parameters found than defined."&lt;BR /&gt;
&lt;BR /&gt;
Here is my complete code:&lt;BR /&gt;
&lt;BR /&gt;
%macro count_exp_gen(list);&lt;BR /&gt;
    %let count=1;&lt;BR /&gt;
	%let delim=*;&lt;BR /&gt;
    %let express=%scan(&amp;amp;list,&amp;amp;count,&amp;amp;delim);&lt;BR /&gt;
    %do %while (%length(&amp;amp;express) ne 0);&lt;BR /&gt;
		&lt;BR /&gt;
		count_gen_&amp;amp;count =  countc(text,"&amp;amp;express",'w') ;&lt;BR /&gt;
		&lt;BR /&gt;
		%let count=%eval(&amp;amp;count + 1);&lt;BR /&gt;
        %let express=%scan(&amp;amp;list,&amp;amp;count,&amp;amp;delim);&lt;BR /&gt;
      %end;&lt;BR /&gt;
%mend;&lt;BR /&gt;
&lt;BR /&gt;
The macro variable is given by:&lt;BR /&gt;
&lt;BR /&gt;
GLOBAL GENERALS&lt;BR /&gt;
INTHEFUTURE*FORSPECULAT*FORTRAD*HOWANDWHY*SPECULATIVETRAD*ACCOUNTFOR*MAY*HEDGINGOFOURCOMMONSTOCK*APPRO&lt;BR /&gt;
PRIATE*WOULDREQUIREDISCLOSURE*&lt;BR /&gt;
&lt;BR /&gt;
Hope this helps to understand my question.&lt;BR /&gt;
&lt;BR /&gt;
Thanks,&lt;BR /&gt;
&lt;BR /&gt;
Valentin</description>
      <pubDate>Wed, 25 May 2011 15:55:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-function/m-p/69363#M15027</guid>
      <dc:creator>Valentin_HU</dc:creator>
      <dc:date>2011-05-25T15:55:57Z</dc:date>
    </item>
    <item>
      <title>Re: Count function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-function/m-p/69364#M15028</link>
      <description>sas macro processing is designed to mingle program instructions with data, &lt;BR /&gt;
and you are expected to provide control when there might be doubt like whether [pre]("have, in the past,"). [/pre] provides program or data.&lt;BR /&gt;
The macro quoting fuinctions are intended to provide this control.&lt;BR /&gt;
If your data in the list in the macro variable are reliable, then %str(&amp;amp;list) might&lt;BR /&gt;
provide enough protection, otherwise use stronger quoting, like: &lt;BR /&gt;
%superQ(list) and %Qscan(%superQ(list), &amp;amp;count, &amp;amp;delim)</description>
      <pubDate>Wed, 25 May 2011 16:36:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-function/m-p/69364#M15028</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2011-05-25T16:36:48Z</dc:date>
    </item>
    <item>
      <title>Re: Count function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-function/m-p/69365#M15029</link>
      <description>I made some changes in the code. Now it works but what is the output? count?&lt;BR /&gt;
[pre]&lt;BR /&gt;
%macro count_exp_gen(list);&lt;BR /&gt;
  %let count=1;&lt;BR /&gt;
  %let delim=*;&lt;BR /&gt;
  %let express=%scan(&amp;amp;list,&amp;amp;count,&amp;amp;delim);&lt;BR /&gt;
  %do %while (%length(&amp;amp;express) ne 0);&lt;BR /&gt;
    %let count_gen_&amp;amp;count = countc(&amp;amp;list,"&amp;amp;express",'w') ;&lt;BR /&gt;
    %let count=%eval(&amp;amp;count + 1);&lt;BR /&gt;
    %let express=%scan(&amp;amp;list,&amp;amp;count,&amp;amp;delim);&lt;BR /&gt;
  %end;&lt;BR /&gt;
%mend;&lt;BR /&gt;
%let generals=INTHEFUTURE*FORSPECULAT*FORTRAD*HOWANDWHY*SPECULATIVETRAD*ACCOUNTFOR*MAY*HEDGINGOFOURCOMMONSTOCK*APPROPRIATE*WOULDREQUIREDISCLOSURE*;&lt;BR /&gt;
%count_exp_gen(&amp;amp;generals);&lt;BR /&gt;
[/pre] &lt;BR /&gt;
SPR</description>
      <pubDate>Wed, 25 May 2011 17:30:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-function/m-p/69365#M15029</guid>
      <dc:creator>SPR</dc:creator>
      <dc:date>2011-05-25T17:30:50Z</dc:date>
    </item>
    <item>
      <title>Re: Count function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-function/m-p/69366#M15030</link>
      <description>First of all, thanks a lot for your answers.&lt;BR /&gt;
&lt;BR /&gt;
I will try once again to make clear what my problem is.&lt;BR /&gt;
&lt;BR /&gt;
I have a dataset that includes a variable with the name "text" and I have a list of  expressions (this list is given by the macro variable "generals"). Now the task I want to perform is to generate one variable in my initial dataset for each expression in "generals" that gives back the number of times that expression appears in the variable "text".&lt;BR /&gt;
This works as long as there are no commas or brackets in the expression list.&lt;BR /&gt;
&lt;BR /&gt;
I will try to give you a simplified version of my full code:&lt;BR /&gt;
&lt;BR /&gt;
data temp;&lt;BR /&gt;
length text $ 50;&lt;BR /&gt;
input Text $;&lt;BR /&gt;
datalines;&lt;BR /&gt;
text1,text2,(text3)&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
%let expressions=text1*text2,*(text3)*;&lt;BR /&gt;
&lt;BR /&gt;
%macro count_exp_gen(list);&lt;BR /&gt;
 %let count=1;&lt;BR /&gt;
%let delim=*;&lt;BR /&gt;
    %let express=%scan(&amp;amp;list,&amp;amp;count,&amp;amp;delim);&lt;BR /&gt;
    %do %while (%length(&amp;amp;express) ne 0);&lt;BR /&gt;
		&lt;BR /&gt;
		count_gen_&amp;amp;count =  count(text,"&amp;amp;express") ;&lt;BR /&gt;
		&lt;BR /&gt;
		%let count=%eval(&amp;amp;count + 1);&lt;BR /&gt;
        %let express=%scan(&amp;amp;list,&amp;amp;count,&amp;amp;delim);&lt;BR /&gt;
      %end;&lt;BR /&gt;
%mend;&lt;BR /&gt;
&lt;BR /&gt;
data temp2;&lt;BR /&gt;
set work.temp;&lt;BR /&gt;
	%count_exp_gen(&amp;amp;expressions);&lt;BR /&gt;
run;</description>
      <pubDate>Thu, 26 May 2011 08:43:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-function/m-p/69366#M15030</guid>
      <dc:creator>Valentin_HU</dc:creator>
      <dc:date>2011-05-26T08:43:56Z</dc:date>
    </item>
    <item>
      <title>Re: Count function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-function/m-p/69367#M15031</link>
      <description>Hello Valentin,&lt;BR /&gt;
&lt;BR /&gt;
I do not see a problem with brackets but commas are a problem for sure because if macro variable EXPRESSIONS contains commas then function %SCAN in this statement&lt;BR /&gt;
&lt;BR /&gt;
%let express=%scan(&amp;amp;list,&amp;amp;count,&amp;amp;delim);&lt;BR /&gt;
&lt;BR /&gt;
gets additional unexpected arguments. &lt;BR /&gt;
&lt;BR /&gt;
I think that as remedy one can replace commas with underscores, for example, like this&lt;BR /&gt;
[pre]&lt;BR /&gt;
data temp;&lt;BR /&gt;
  length text $ 50;&lt;BR /&gt;
  input Text $;&lt;BR /&gt;
  text=TRANWRD(text,",","_");&lt;BR /&gt;
datalines;&lt;BR /&gt;
text1,text2,(text3)&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
%let expressions=text1_*text2_*(text3)*;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
Sincerely,&lt;BR /&gt;
SPR

Message was edited by: SPR</description>
      <pubDate>Thu, 26 May 2011 14:36:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-function/m-p/69367#M15031</guid>
      <dc:creator>SPR</dc:creator>
      <dc:date>2011-05-26T14:36:32Z</dc:date>
    </item>
    <item>
      <title>Re: Count function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-function/m-p/69368#M15032</link>
      <description>Ah ok! Thank you, I will implement your solution!</description>
      <pubDate>Thu, 26 May 2011 15:24:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-function/m-p/69368#M15032</guid>
      <dc:creator>Valentin_HU</dc:creator>
      <dc:date>2011-05-26T15:24:38Z</dc:date>
    </item>
    <item>
      <title>Re: Count function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-function/m-p/69369#M15033</link>
      <description>I think you need automatic macro variable 'parmbuff'&lt;BR /&gt;
Such as : %macro test /parmbuff; ...... &amp;amp;syspbuff....&lt;BR /&gt;
Or using call execute() which is macro interface  with dataset.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Mon, 30 May 2011 09:28:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-function/m-p/69369#M15033</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-05-30T09:28:39Z</dc:date>
    </item>
  </channel>
</rss>

