<?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: count parts of a string in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/count-parts-of-a-string/m-p/677351#M204306</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/208370"&gt;@emaneman&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello Reeza,&lt;/P&gt;
&lt;P&gt;unfortunately that does not work either. it gives me:&lt;/P&gt;
&lt;P&gt;2&lt;/P&gt;
&lt;P&gt;2&lt;/P&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;P&gt;Which is weired because in the first observation there is only one 3, part of 23:&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;23,55,58,81,82,99,110,119&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Eman&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Countw, counts words, not a specific word, so used that way defines '3' as the delimiter to use in defining words. So '2' is the first word and everything after 3 in the first value of choices is the second word&lt;/P&gt;</description>
    <pubDate>Mon, 17 Aug 2020 21:11:21 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-08-17T21:11:21Z</dc:date>
    <item>
      <title>count parts of a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-parts-of-a-string/m-p/677346#M204301</link>
      <description>&lt;P&gt;Dear all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a variable (CHOICES)that contains several codes, separated by a comma:&lt;/P&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default dgrid-selected ui-state-active"&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;CHOICES &lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;23,55,58,81,82,99,110,119&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3,9,23,27,55,58,61,82,98,99,105,106,110,118,119&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;34&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;i need to create a new variable that has value 1 only when CHOICES contains the single code 3.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I use;&lt;/P&gt;&lt;P&gt;NEW = count( choices , '3');&amp;nbsp;&lt;/P&gt;&lt;P&gt;I get&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;NEW &lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Because SAS counts every instance of 3. So 23 and 34 also get counted.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like this output instead:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;NEW &lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any suggestion would be most welcome!&lt;/P&gt;&lt;P&gt;Thank you in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Eman&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Mon, 17 Aug 2020 20:32:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-parts-of-a-string/m-p/677346#M204301</guid>
      <dc:creator>emaneman</dc:creator>
      <dc:date>2020-08-17T20:32:25Z</dc:date>
    </item>
    <item>
      <title>Re: count parts of a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-parts-of-a-string/m-p/677347#M204302</link>
      <description>Have you tried using COUNTW() instead which checks for words, not just the string. &lt;BR /&gt;&lt;BR /&gt;new = countw(choices, '3');</description>
      <pubDate>Mon, 17 Aug 2020 20:39:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-parts-of-a-string/m-p/677347#M204302</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-08-17T20:39:16Z</dc:date>
    </item>
    <item>
      <title>Re: count parts of a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-parts-of-a-string/m-p/677348#M204303</link>
      <description>&lt;P&gt;Hello Reeza,&lt;/P&gt;&lt;P&gt;unfortunately that does not work either. it gives me:&lt;/P&gt;&lt;P&gt;2&lt;/P&gt;&lt;P&gt;2&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;Which is weired because in the first observation there is only one 3, part of 23:&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;23,55,58,81,82,99,110,119&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Eman&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Aug 2020 20:49:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-parts-of-a-string/m-p/677348#M204303</guid>
      <dc:creator>emaneman</dc:creator>
      <dc:date>2020-08-17T20:49:29Z</dc:date>
    </item>
    <item>
      <title>Re: count parts of a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-parts-of-a-string/m-p/677349#M204304</link>
      <description>&lt;P&gt;Try&lt;/P&gt;
&lt;PRE&gt;data example;
   input choices :$50.;
   new = (findw(choices,'3')&amp;gt; 0);
datalines;
23,55,58,81,82,99,110,119
3,9,23,27,55,58,61,82,98,99,105,106,110,118,119
34
;
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 17 Aug 2020 21:06:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-parts-of-a-string/m-p/677349#M204304</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-08-17T21:06:27Z</dc:date>
    </item>
    <item>
      <title>Re: count parts of a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-parts-of-a-string/m-p/677350#M204305</link>
      <description>&lt;P&gt;COUNTW() counts the number of words, the second parameter is the delimiter, in this case 3.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's definitely the wrong function so that's my bad. I can't recall another function to count the number of words so I guess a loop is the best option unfortunately.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	length choices $50.;
	choices="23,55,58,81,82,99,110,119";
	output;
	choices="3,9,23,27,55,58,61,82,98,99,105,106,110,118,119";
	output;
	choices="34";
	output;
run;

data want;
	set have;
	
	*initialize count to 0 for each row;
	number3=0;

    *check if any 3s are present if so, count them;
	if indexw(choices, '3', ',') then
	     /*loop counter based on number of words using COUNTW()*/
		do i=1 to countw(choices);

			if scan(choices, i, ',')='3' then
				number3+1; /*implicit retain with a sum statement*/
		end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 17 Aug 2020 21:06:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-parts-of-a-string/m-p/677350#M204305</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-08-17T21:06:43Z</dc:date>
    </item>
    <item>
      <title>Re: count parts of a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-parts-of-a-string/m-p/677351#M204306</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/208370"&gt;@emaneman&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello Reeza,&lt;/P&gt;
&lt;P&gt;unfortunately that does not work either. it gives me:&lt;/P&gt;
&lt;P&gt;2&lt;/P&gt;
&lt;P&gt;2&lt;/P&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;P&gt;Which is weired because in the first observation there is only one 3, part of 23:&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;23,55,58,81,82,99,110,119&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Eman&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Countw, counts words, not a specific word, so used that way defines '3' as the delimiter to use in defining words. So '2' is the first word and everything after 3 in the first value of choices is the second word&lt;/P&gt;</description>
      <pubDate>Mon, 17 Aug 2020 21:11:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-parts-of-a-string/m-p/677351#M204306</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-08-17T21:11:21Z</dc:date>
    </item>
    <item>
      <title>Re: count parts of a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-parts-of-a-string/m-p/677352#M204307</link>
      <description>&lt;P&gt;This works!&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It is a bit cumbersome, since I have to look for 65 different substrings (3 was only one of them), and create 65 new corresponding variables with value 0 or 1 depending on whether the specific substring is present, but this is excellent.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you Reeza.&lt;/P&gt;</description>
      <pubDate>Mon, 17 Aug 2020 21:19:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-parts-of-a-string/m-p/677352#M204307</guid>
      <dc:creator>emaneman</dc:creator>
      <dc:date>2020-08-17T21:19:06Z</dc:date>
    </item>
    <item>
      <title>Re: count parts of a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-parts-of-a-string/m-p/677355#M204308</link>
      <description>&lt;P&gt;hello Ballardw,&lt;/P&gt;&lt;P&gt;this is much easier, thanks you.&lt;/P&gt;&lt;P&gt;The only issue at present is that I already have a file with the variable choice defined.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Of course I can recreate it with a datalines statement, but is there a way I can put the equivalent of "choice:$50." without having to create a new datafile using datalines?&lt;/P&gt;&lt;P&gt;Eman&lt;/P&gt;</description>
      <pubDate>Mon, 17 Aug 2020 21:31:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-parts-of-a-string/m-p/677355#M204308</guid>
      <dc:creator>emaneman</dc:creator>
      <dc:date>2020-08-17T21:31:51Z</dc:date>
    </item>
    <item>
      <title>Re: count parts of a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-parts-of-a-string/m-p/677357#M204309</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/208370"&gt;@emaneman&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;hello Ballardw,&lt;/P&gt;
&lt;P&gt;this is much easier, thanks you.&lt;/P&gt;
&lt;P&gt;The only issue at present is that I already have a file with the variable choice defined.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course I can recreate it with a datalines statement, but is there a way I can put the equivalent of "choice:$50." without having to create a new datafile using datalines?&lt;/P&gt;
&lt;P&gt;Eman&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I provided example data in the form of a data step to demonstrate the code behavior. You would typically use a SET statement to bring an existing data set in to the working data set.&lt;/P&gt;
&lt;P&gt;Data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set have; /* the name of your existing data set*/&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;lt;and other code statments here?&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You did not provide either a complete data step, the name of your existing data set. So I created one with the example data you provided just to have something to work with.&lt;/P&gt;
&lt;P&gt;The FINDW function returns either the character position number (the example I used does this) or the word number in a string (check the documentation for the options), or 0 if the value is not found. By default commas are one of the delimiters for "words". So&amp;nbsp; abc,3,444 would have '3' as a word. The comparison I use returns 1 with the comparison is true (3 was found as a word) or 0 otherwise.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Aug 2020 21:48:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-parts-of-a-string/m-p/677357#M204309</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-08-17T21:48:17Z</dc:date>
    </item>
    <item>
      <title>Re: count parts of a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-parts-of-a-string/m-p/677358#M204310</link>
      <description>&lt;P&gt;My apologies. I thought that defining the variable choices in that way was necessary. But it is not, so your solution using FINDW works perfectly and efficiently!&lt;/P&gt;&lt;P&gt;thank you very much.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Aug 2020 21:54:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-parts-of-a-string/m-p/677358#M204310</guid>
      <dc:creator>emaneman</dc:creator>
      <dc:date>2020-08-17T21:54:41Z</dc:date>
    </item>
    <item>
      <title>Re: count parts of a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-parts-of-a-string/m-p/677382#M204318</link>
      <description>I would instead separate the data and flip it long and use a sing WHERE statement which is more dynamic. Or two loops would do it quite nicely. But you never mentioned this so the solution posted reflects what you state.</description>
      <pubDate>Tue, 18 Aug 2020 03:37:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-parts-of-a-string/m-p/677382#M204318</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-08-18T03:37:31Z</dc:date>
    </item>
    <item>
      <title>Re: count parts of a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-parts-of-a-string/m-p/677402#M204330</link>
      <description>&lt;P&gt;The way to go may be to use PROC FCMP to create a custom function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc fcmp outlib=work.funcs.string;
  function countw2(instr $,word $,delim $);
    start=1;
    len=length(word);
    do i=0 by 1;
      start=findw(instr,word,delim,start);
      if start=0 then
        leave;
      else 
        start=start+len;
      end;
    return(i);
    endsub;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The function returns the number of occurrences of WORD within INSTR, delimited by DELIM.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can then use the function like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options cmplib=work.funcs;

data want;
  set have;
  x=countw2(choices,'3',',');
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You may want to save the function in a permanent library, instead of WORK.&lt;/P&gt;</description>
      <pubDate>Tue, 18 Aug 2020 06:15:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-parts-of-a-string/m-p/677402#M204330</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-08-18T06:15:35Z</dc:date>
    </item>
    <item>
      <title>Re: count parts of a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-parts-of-a-string/m-p/677436#M204345</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thank you S_Lasssen, that works.&lt;/P&gt;</description>
      <pubDate>Tue, 18 Aug 2020 10:37:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-parts-of-a-string/m-p/677436#M204345</guid>
      <dc:creator>emaneman</dc:creator>
      <dc:date>2020-08-18T10:37:20Z</dc:date>
    </item>
  </channel>
</rss>

