<?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: Use IF to find a string THEN newvar = string in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Use-IF-to-find-a-string-THEN-newvar-string/m-p/312351#M67741</link>
    <description>&lt;P&gt;You may have a potential issue for compound words with Lieutenant-General or Major-General. If you are searching for Lieutenant or Major you will not want "-" to be included as a delimiter for a word search and do want it for the search for General.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since this is free form text you may have fun for your intended purpose if you only want General as a title or rank and not in context of "General orders" or similar.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also Sergeant-Major is a potential if searching for Major, and Lieutenant-Colonel. If there is Navy data then Lieutenant-Commander is another issue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And don't forget abbreviations such as CPT, LT, LTC, MAJ, GEN, COL, etc. since military runs on TLAs (three-letter acronyms).&lt;/P&gt;</description>
    <pubDate>Thu, 17 Nov 2016 15:28:06 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2016-11-17T15:28:06Z</dc:date>
    <item>
      <title>Use IF to find a string THEN newvar = string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-IF-to-find-a-string-THEN-newvar-string/m-p/312292#M67711</link>
      <description>&lt;P&gt;Guys, could i ask for some guidane again please?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have 122k lines of data where 1 of the variables is a user defined text entry. &amp;nbsp;Somewhere within the variable is the string Captain or General or Colonel, etc (i have to find 144 differerent strings in total).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to find each instance of Captain in the&amp;nbsp;text and then put Captain into a Variable called Type.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have been trying:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if Text contains 'Captain' then&amp;nbsp;Type = 'Captain' - this gives an arithmetic error. &amp;nbsp;I dont think i can use contains with IF!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do i find a string in any position in a text string and then put that string into &amp;nbsp;my newvar?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks guys,&lt;/P&gt;</description>
      <pubDate>Thu, 17 Nov 2016 12:03:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-IF-to-find-a-string-THEN-newvar-string/m-p/312292#M67711</guid>
      <dc:creator>pandhandj</dc:creator>
      <dc:date>2016-11-17T12:03:43Z</dc:date>
    </item>
    <item>
      <title>Re: Use IF to find a string THEN newvar = string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-IF-to-find-a-string-THEN-newvar-string/m-p/312295#M67713</link>
      <description>&lt;P&gt;Use the INDEX function like this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;

   if index(textvar, 'Captain') &amp;gt; 0 then type = 'Captain';
   else if index(textvar, 'General') &amp;gt; 0 then type = 'General';
   else if index(textvar, 'Colonel') &amp;gt; 0 then type = 'Colonel';
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 17 Nov 2016 12:14:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-IF-to-find-a-string-THEN-newvar-string/m-p/312295#M67713</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2016-11-17T12:14:04Z</dc:date>
    </item>
    <item>
      <title>Re: Use IF to find a string THEN newvar = string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-IF-to-find-a-string-THEN-newvar-string/m-p/312297#M67714</link>
      <description>&lt;P&gt;BTW I have assumed that no observation contains both the string 'Captain' and 'General' eg &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Nov 2016 12:12:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-IF-to-find-a-string-THEN-newvar-string/m-p/312297#M67714</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2016-11-17T12:12:44Z</dc:date>
    </item>
    <item>
      <title>Re: Use IF to find a string THEN newvar = string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-IF-to-find-a-string-THEN-newvar-string/m-p/312309#M67719</link>
      <description>&lt;P&gt;Just a thought ..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regardless of case insensitive characters , use &lt;STRONG&gt;find&lt;/STRONG&gt; function with &lt;STRONG&gt;i&lt;/STRONG&gt; option .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;if &lt;STRONG&gt;find&lt;/STRONG&gt;(textvar, 'CAPTAIN',&lt;STRONG&gt;"i"&lt;/STRONG&gt;) &amp;gt; 0 then type = 'Captain';&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input textvar $20.;&lt;BR /&gt;datalines;&lt;BR /&gt;Captain of Ship&lt;BR /&gt;captain of Ship&lt;BR /&gt;CAPTAIN india&lt;BR /&gt;The Captain&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;if find(textvar, 'CAPTAIN',"i") &amp;gt; 0 then type = 'Captain';&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Nov 2016 12:44:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-IF-to-find-a-string-THEN-newvar-string/m-p/312309#M67719</guid>
      <dc:creator>monikka1991</dc:creator>
      <dc:date>2016-11-17T12:44:45Z</dc:date>
    </item>
    <item>
      <title>Re: Use IF to find a string THEN newvar = string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-IF-to-find-a-string-THEN-newvar-string/m-p/312333#M67727</link>
      <description>&lt;P&gt;Regardless of which function you choose, you would be better off switching to the version that finds words rather than strings: &amp;nbsp;FINDW or INDEXW.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't want your logic to locate strings like these:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;generally&lt;/P&gt;
&lt;P&gt;captaincy&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In either case, though, you may encounter false positives such as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In general, these problems were handled.&lt;/P&gt;</description>
      <pubDate>Thu, 17 Nov 2016 13:39:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-IF-to-find-a-string-THEN-newvar-string/m-p/312333#M67727</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-11-17T13:39:10Z</dc:date>
    </item>
    <item>
      <title>Re: Use IF to find a string THEN newvar = string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-IF-to-find-a-string-THEN-newvar-string/m-p/312343#M67734</link>
      <description>&lt;P&gt;my thanks to draycut for the code and to astounding for the inedxw suggestion.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i'm cooking with gas now!&lt;/P&gt;</description>
      <pubDate>Thu, 17 Nov 2016 14:29:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-IF-to-find-a-string-THEN-newvar-string/m-p/312343#M67734</guid>
      <dc:creator>pandhandj</dc:creator>
      <dc:date>2016-11-17T14:29:33Z</dc:date>
    </item>
    <item>
      <title>Re: Use IF to find a string THEN newvar = string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-IF-to-find-a-string-THEN-newvar-string/m-p/312351#M67741</link>
      <description>&lt;P&gt;You may have a potential issue for compound words with Lieutenant-General or Major-General. If you are searching for Lieutenant or Major you will not want "-" to be included as a delimiter for a word search and do want it for the search for General.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since this is free form text you may have fun for your intended purpose if you only want General as a title or rank and not in context of "General orders" or similar.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also Sergeant-Major is a potential if searching for Major, and Lieutenant-Colonel. If there is Navy data then Lieutenant-Commander is another issue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And don't forget abbreviations such as CPT, LT, LTC, MAJ, GEN, COL, etc. since military runs on TLAs (three-letter acronyms).&lt;/P&gt;</description>
      <pubDate>Thu, 17 Nov 2016 15:28:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-IF-to-find-a-string-THEN-newvar-string/m-p/312351#M67741</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-11-17T15:28:06Z</dc:date>
    </item>
    <item>
      <title>Re: Use IF to find a string THEN newvar = string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-IF-to-find-a-string-THEN-newvar-string/m-p/315436#M68804</link>
      <description>Guys,&lt;BR /&gt;&lt;BR /&gt;i have come across an issue with my logic (not the code above).&lt;BR /&gt;&lt;BR /&gt;When i run the indexw IF statements above, it places "General", "Captain", etc in my TYPE variable, i can then count each instance of "General", "Captain", etc. However, i cant work out how to get a 0 count for any strings it does not find.&lt;BR /&gt;I tried this:&lt;BR /&gt;&lt;BR /&gt;if indexw(userdata, 'Captain') &amp;gt; 0 then do; type = 'Captain'; NotFound = 0;end;&lt;BR /&gt;else if indexw(userdata, 'Captain') = 0 then do; type = '.'; NotFound = 'Captain';end;&lt;BR /&gt;&lt;BR /&gt;but it fills the var NotFound with captain.&lt;BR /&gt;&lt;BR /&gt;Can anyone point me in the general direction? (see what i did there!)&lt;BR /&gt;&lt;BR /&gt;thanks.</description>
      <pubDate>Wed, 30 Nov 2016 08:33:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-IF-to-find-a-string-THEN-newvar-string/m-p/315436#M68804</guid>
      <dc:creator>pandhandj</dc:creator>
      <dc:date>2016-11-30T08:33:03Z</dc:date>
    </item>
    <item>
      <title>Re: Use IF to find a string THEN newvar = string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-IF-to-find-a-string-THEN-newvar-string/m-p/315466#M68822</link>
      <description>&lt;P&gt;I'm not sure that you're picturing the right end result. &amp;nbsp;Here's one possible change:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;type='Captain';&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;if indexw(userdata, 'Captain') &amp;gt; 0 then Found=1;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;else Found=0;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;If you're going to check the same data for "General", you will need to take extra steps. &amp;nbsp;Either output multiple observations (one for each TYPE value), or create multiple flags such as CaptainFound instead of Found.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Nov 2016 10:56:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-IF-to-find-a-string-THEN-newvar-string/m-p/315466#M68822</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-11-30T10:56:39Z</dc:date>
    </item>
    <item>
      <title>Re: Use IF to find a string THEN newvar = string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-IF-to-find-a-string-THEN-newvar-string/m-p/315477#M68824</link>
      <description>&lt;P&gt;"I'm not sure that you're picturing the right end result." - i think you might be right!&lt;BR /&gt;&lt;BR /&gt;i dont think my original idea will work for my new criteria.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I get the feeling that i'm going to have to go back to the start on this one...&lt;/P&gt;</description>
      <pubDate>Wed, 30 Nov 2016 11:26:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-IF-to-find-a-string-THEN-newvar-string/m-p/315477#M68824</guid>
      <dc:creator>pandhandj</dc:creator>
      <dc:date>2016-11-30T11:26:15Z</dc:date>
    </item>
    <item>
      <title>Re: Use IF to find a string THEN newvar = string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-IF-to-find-a-string-THEN-newvar-string/m-p/315488#M68831</link>
      <description>&lt;P&gt;trying to think round my issue...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i have 144 string i need to search for. &amp;nbsp;At present my results table gives me a count for the 75 strings that are present in my userdata variable. &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Will it be possible to code something that compares the 75 strings in my results table and compare it to my list of 144 searchable strings and then adds&amp;nbsp;the 69 strings that were not found to my results table with a count of 0?&lt;/P&gt;</description>
      <pubDate>Wed, 30 Nov 2016 12:02:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-IF-to-find-a-string-THEN-newvar-string/m-p/315488#M68831</guid>
      <dc:creator>pandhandj</dc:creator>
      <dc:date>2016-11-30T12:02:13Z</dc:date>
    </item>
    <item>
      <title>Re: Use IF to find a string THEN newvar = string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-IF-to-find-a-string-THEN-newvar-string/m-p/316167#M69055</link>
      <description>&lt;P&gt;Just for completeness... I managed to work this out.&lt;BR /&gt;&lt;BR /&gt;I made a new data set of my 144 search strings, compared it to the results from the indexw code, which gave me the search strings which the indexw code didnt find. I then just set the count for these strings to zero. I now have a complete list of all search strings and how many times the were found.&lt;BR /&gt;&lt;BR /&gt;thanks to all for their help with this one.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Dec 2016 09:00:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-IF-to-find-a-string-THEN-newvar-string/m-p/316167#M69055</guid>
      <dc:creator>pandhandj</dc:creator>
      <dc:date>2016-12-02T09:00:18Z</dc:date>
    </item>
  </channel>
</rss>

