<?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 the occurance of several words in a column in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-the-occurance-of-several-words-in-a-column/m-p/262019#M51075</link>
    <description>&lt;P&gt;Yes, the values are written correctly. I have copy pasted the values from the Excel sheet. I even tried to change&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;WHEN ('Fast Track 1 Heir') Heir +1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;to&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;WHEN ('%Heir%') Heir +1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Even then it didn't work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So you're suggestion would look like this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA atropos.dos_rap;
SET atropos.dos_opt;
BY dossier_manager;
RETAIN
	Heir
	Living_Expenses
	Balance
	Surety
	Manual
  	Notary
	Standard;

IF first.dossier_manager
THEN DO;
	Heir=0;
	Living_Expenses=0;
	Balance=0;
	Surety=0;
	Manual=0;
  	Notary=0;
	Standard=0;
END;

SELECT (distributionoption);

  	IF distributionoption = ('Fast Track 1 Heir') THEN Heir +1;

  	ELSE IF distributionoption = ('Fast Track Living Expenses') THEN Living_Expenses +1;

	ELSE IF distributionoption = ('Fast Track Balance') THEN Balance +1;
  	
  	ELSE IF distributionoption = ('Fast Track Surety") THEN Surety +1;

	ELSE IF distributionoption = 'Manual Distributionassignment Office') THEN Manual +1;

	ELSE IF distributionoption = ('Distribution Notery') THEN Notery +1;

	ELSE IF distributionoption = ('Distribution Standard') THEN Standard +1;
END;

IF last.dossier_manager THEN OUTPUT;
KEEP dossier_manager Heir Living_Expenses Balance Surety Manual Notary Standard;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 07 Apr 2016 09:13:16 GMT</pubDate>
    <dc:creator>Yves_Boonen</dc:creator>
    <dc:date>2016-04-07T09:13:16Z</dc:date>
    <item>
      <title>Count the occurance of several words in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-occurance-of-several-words-in-a-column/m-p/262016#M51073</link>
      <description>&lt;P&gt;I have an Excel sheet with a few dozen columns. One of these columns is called "distribution options". In this column you find general keywords to indicate how it will be distributed. Below you will find all the available "distribution options".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Fast Track Heir&lt;/P&gt;&lt;P&gt;Fast Track Living Expenses&lt;/P&gt;&lt;P&gt;Fast Track Balance&lt;/P&gt;&lt;P&gt;Fast Track Surety&lt;/P&gt;&lt;P&gt;Manual Distribution Office&lt;/P&gt;&lt;P&gt;Distribution Notery&lt;/P&gt;&lt;P&gt;Distribution Standard&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I wanted to read out the entire column and receive a table how many times each of these distribution options appear in the column.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The result in the end should look similar to this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;Distribution Options&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;Count&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Fast Track Heir&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;102&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Fast Track Living Expenses&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;230&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Fast Track Balance&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;17&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Fast Track Surety&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;43&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Manual Distribution Office&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Distribution Notery&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;18&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Distribution Standard&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;43&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I wrote a script, but for some reason it doesn't count the Fast Track Heir, Fast Track Surety and Manual Distribution Office. All the other values (the same as above) are correct.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I used this script, but perhaps there is an easier approach. I simply can't figure out why Heir, Surety and Manual return "0" values, whereas the rest returns the values shown above. There are no errors in the log either. This time I checked the data myself and used COUNTIF(range;"word") to count it in Excel. The values above are correct. The script somehow isn't.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SORT IN=atropos.dossiers OUT=atropos.dos_opt;
BY dossier_manager;
RUN;

DATA atropos.dos_rap;
SET atropos.dos_opt;
BY dossier_manager;
RETAIN
	Heir
	Living_Expenses
	Balance
	Surety
	Manual
  	Notary
	Standard;

IF first.dossier_manager
THEN DO;
	Heir=0;
	Living_Expenses=0;
	Balance=0;
	Surety=0;
	Manual=0;
  	Notary=0;
	Standard=0;
END;

SELECT (distributionoption);

  	WHEN ('Fast Track 1 Heir') Heir +1;

  	WHEN ('Fast Track Living Expenses') Living_Expenses +1;

	WHEN ('Fast Track Balance') Balance +1;
  	
  	WHEN ('Fast Track Surety") Surety +1;

	WHEN ('Manual Distributionassignment Office') Manual +1;

	WHEN ('Distribution Notery') Notery +1;

	WHEN ('Distribution Standard') Standard +1;

  	OTHERWISE;
END;

IF last.dossier_manager THEN OUTPUT;
KEEP dossier_manager Heir Living_Expenses Balance Surety Manual Notary Standard;
RUN;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Apr 2016 08:52:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-occurance-of-several-words-in-a-column/m-p/262016#M51073</guid>
      <dc:creator>Yves_Boonen</dc:creator>
      <dc:date>2016-04-07T08:52:59Z</dc:date>
    </item>
    <item>
      <title>Re: Count the occurance of several words in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-occurance-of-several-words-in-a-column/m-p/262017#M51074</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Are you sure the values are written the way you have added in the select clause ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;By the way select clause is useful when:&lt;/P&gt;&lt;P&gt;- you have a long series of mutually exclusive numeric conditions&lt;BR /&gt;- data values are uniformly distributed&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In your case I believe an aprroach using&lt;STRONG&gt; if then else&lt;/STRONG&gt; condition would be more appropriate.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2016 08:59:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-occurance-of-several-words-in-a-column/m-p/262017#M51074</guid>
      <dc:creator>Loko</dc:creator>
      <dc:date>2016-04-07T08:59:08Z</dc:date>
    </item>
    <item>
      <title>Re: Count the occurance of several words in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-occurance-of-several-words-in-a-column/m-p/262019#M51075</link>
      <description>&lt;P&gt;Yes, the values are written correctly. I have copy pasted the values from the Excel sheet. I even tried to change&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;WHEN ('Fast Track 1 Heir') Heir +1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;to&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;WHEN ('%Heir%') Heir +1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Even then it didn't work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So you're suggestion would look like this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA atropos.dos_rap;
SET atropos.dos_opt;
BY dossier_manager;
RETAIN
	Heir
	Living_Expenses
	Balance
	Surety
	Manual
  	Notary
	Standard;

IF first.dossier_manager
THEN DO;
	Heir=0;
	Living_Expenses=0;
	Balance=0;
	Surety=0;
	Manual=0;
  	Notary=0;
	Standard=0;
END;

SELECT (distributionoption);

  	IF distributionoption = ('Fast Track 1 Heir') THEN Heir +1;

  	ELSE IF distributionoption = ('Fast Track Living Expenses') THEN Living_Expenses +1;

	ELSE IF distributionoption = ('Fast Track Balance') THEN Balance +1;
  	
  	ELSE IF distributionoption = ('Fast Track Surety") THEN Surety +1;

	ELSE IF distributionoption = 'Manual Distributionassignment Office') THEN Manual +1;

	ELSE IF distributionoption = ('Distribution Notery') THEN Notery +1;

	ELSE IF distributionoption = ('Distribution Standard') THEN Standard +1;
END;

IF last.dossier_manager THEN OUTPUT;
KEEP dossier_manager Heir Living_Expenses Balance Surety Manual Notary Standard;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Apr 2016 09:13:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-occurance-of-several-words-in-a-column/m-p/262019#M51075</guid>
      <dc:creator>Yves_Boonen</dc:creator>
      <dc:date>2016-04-07T09:13:16Z</dc:date>
    </item>
    <item>
      <title>Re: Count the occurance of several words in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-occurance-of-several-words-in-a-column/m-p/262020#M51076</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Firstly, please code in consistent casing/indentaiton, makes it much easier to read, and posting example test data in the form of a datastep will help you get tested code. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now your problem. &amp;nbsp;I thinkk you are looking at it the wrong way round, you have list of what values you want out, and then trying to count from that. &amp;nbsp;The other way of looking at it is to proc means the data you have, and then filtering the results for those you want to keep. &amp;nbsp;This has the benefit of being able to see what counted data is present. &amp;nbsp;For example (untested);&lt;/P&gt;
&lt;PRE&gt;proc means data=have;
  by distribution_options;
  output out=results;
run;
/* Filter out the ones you want to keep */
proc sql;
  create table WANT as
  select  *
  from    DISTRIBUTION_OPTIONS in (select OPTIONS from LIST_OF_VALUES);
quit;&lt;/PRE&gt;
&lt;P&gt;Nice and simple way, though to be honest I don't think you need the second step, just drop records you don't want in your further processing, then its just one step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just to add, on your second post, a select clause can look like:&lt;/P&gt;
&lt;P&gt;select(&amp;lt;test&amp;gt;);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; when(&amp;lt;result&amp;gt;) &amp;lt;code&amp;gt;;&lt;/P&gt;
&lt;P&gt;Or ir can look like:&lt;/P&gt;
&lt;P&gt;select;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; when (index(strip(upcase(&amp;lt;text&amp;gt;)),"HIKE")&amp;gt;0) &amp;lt;code&amp;gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2016 09:24:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-occurance-of-several-words-in-a-column/m-p/262020#M51076</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-04-07T09:24:37Z</dc:date>
    </item>
    <item>
      <title>Re: Count the occurance of several words in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-occurance-of-several-words-in-a-column/m-p/262029#M51080</link>
      <description>&lt;P&gt;I tried a different approach.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I imported the file. Then I took the imported data and only selected the distribution options, while storing this new selection as a new table. I noticed that some of the distribution options were incomplete and thus the query didn't work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example: "Fast Track 1 Heir" became "Fast Track 1 He" as well as with the other two.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I did copy and paste the exact values from the Excel file, but that wasn't the table&amp;nbsp;my query tried to work with. In fact it was an import in my&amp;nbsp;SAS library. Instead of checking the raw data, I should also check my imported data more. Consider this a lesson learned, because after I adjusted the names in the first script, everything ran smoothly!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for helping me get there.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2016 09:54:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-occurance-of-several-words-in-a-column/m-p/262029#M51080</guid>
      <dc:creator>Yves_Boonen</dc:creator>
      <dc:date>2016-04-07T09:54:00Z</dc:date>
    </item>
    <item>
      <title>Re: Count the occurance of several words in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-occurance-of-several-words-in-a-column/m-p/262030#M51081</link>
      <description>&lt;P&gt;If this is really EXACTLY your code, look at the log, because you have non-matching quotes in&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;WHEN ('Fast Track Surety") Surety +1;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Apr 2016 09:54:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-occurance-of-several-words-in-a-column/m-p/262030#M51081</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-04-07T09:54:52Z</dc:date>
    </item>
    <item>
      <title>Re: Count the occurance of several words in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-occurance-of-several-words-in-a-column/m-p/262031#M51082</link>
      <description>&lt;P&gt;No it isn't.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The original code (in my SAS client) has variable names in Dutch. To make it more understandable, I manually changed all the names in here. Kinda screwed me over, but rest assured: no syntax errors. &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;Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2016 09:57:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-occurance-of-several-words-in-a-column/m-p/262031#M51082</guid>
      <dc:creator>Yves_Boonen</dc:creator>
      <dc:date>2016-04-07T09:57:48Z</dc:date>
    </item>
    <item>
      <title>Re: Count the occurance of several words in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-occurance-of-several-words-in-a-column/m-p/262046#M51085</link>
      <description>&lt;P&gt;Not sure I under the purpose of this. Seems to me as a hard coded recode of column values, count, and then a transpose...?&lt;/P&gt;
&lt;P&gt;Similar to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9﻿&lt;/a&gt;, I think there are smarter ways of accomplish this.&lt;/P&gt;
&lt;P&gt;A format/lookup table can recode/rename your distribution options.&lt;/P&gt;
&lt;P&gt;PROC TABULATE does cross tabulation out of the box - no need to transpose your data set.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2016 12:07:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-occurance-of-several-words-in-a-column/m-p/262046#M51085</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-04-07T12:07:12Z</dc:date>
    </item>
    <item>
      <title>Re: Count the occurance of several words in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-occurance-of-several-words-in-a-column/m-p/262061#M51086</link>
      <description>&lt;P&gt;Another way to do this is using proc sql:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input distribution_options $40.;&lt;BR /&gt;datalines;&lt;BR /&gt;Fast Track Heir&lt;BR /&gt;Fast Track Living Expenses&lt;BR /&gt;Fast Track Balance&lt;BR /&gt;Fast Track Surety&lt;BR /&gt;Manual Distribution Office&lt;BR /&gt;Distribution Notery&lt;BR /&gt;Distribution Standard&lt;BR /&gt;Fast Track Heir&lt;BR /&gt;Fast Track Living Expenses&lt;BR /&gt;Fast Track Balance&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt;select trim(distribution_options) as distribution_options , count(*) as Count&lt;BR /&gt;from have&lt;BR /&gt;group by trim(distribution_options);&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2016 13:05:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-occurance-of-several-words-in-a-column/m-p/262061#M51086</guid>
      <dc:creator>stat_sas</dc:creator>
      <dc:date>2016-04-07T13:05:14Z</dc:date>
    </item>
    <item>
      <title>Re: Count the occurance of several words in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-occurance-of-several-words-in-a-column/m-p/262075#M51088</link>
      <description>&lt;P&gt;Maybe I am missing something, but Proc Freq seems to be designed for this kind of job.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2016 13:51:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-occurance-of-several-words-in-a-column/m-p/262075#M51088</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2016-04-07T13:51:35Z</dc:date>
    </item>
  </channel>
</rss>

