<?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: String Functions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/String-Functions/m-p/253945#M48367</link>
    <description>&lt;P&gt;Well, the syntax of the &lt;A href="http://support.sas.com/documentation/cdl/en/syntaxidx/68719/HTML/default/index.htm#/documentation/cdl/en/lefunctionsref/67960/HTML/default/n0n08xougp40i5n1xw7njpcy0a2b.htm" target="_blank"&gt;SUBSTR function&lt;/A&gt; specifies that the third argument "&lt;SPAN&gt;&lt;EM&gt;is the length of the substring to extract.&lt;/EM&gt;"&amp;nbsp;You want to extract the substring beginning at position 1 and ending either in the blank before "County" or in the character immediately before that blank (the result is effectively the same). The length of this substring is &lt;FONT face="courier new,courier"&gt;length(cty)-6&lt;/FONT&gt; or &lt;FONT face="courier new,courier"&gt;length(cty)-7&lt;/FONT&gt;, respectively. It is definitely not -6, because, obviously, the length of a (sub)string, i.e. a number of characters, cannot be negative.&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 02 Mar 2016 19:44:13 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2016-03-02T19:44:13Z</dc:date>
    <item>
      <title>String Functions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-Functions/m-p/253894#M48345</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to remove certain charecters from the variable.&lt;/P&gt;&lt;P&gt;From the below example I would like to remove the space and the County which comes after that.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Irving County&lt;/P&gt;&lt;P&gt;Collin County&lt;/P&gt;&lt;P&gt;Denton County&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Want:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Irving&lt;/P&gt;&lt;P&gt;Collin&lt;/P&gt;&lt;P&gt;Denton&lt;/P&gt;</description>
      <pubDate>Wed, 02 Mar 2016 17:31:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-Functions/m-p/253894#M48345</guid>
      <dc:creator>robertrao</dc:creator>
      <dc:date>2016-03-02T17:31:55Z</dc:date>
    </item>
    <item>
      <title>Re: String Functions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-Functions/m-p/253897#M48347</link>
      <description>&lt;P&gt;SCAN()&lt;/P&gt;</description>
      <pubDate>Wed, 02 Mar 2016 17:44:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-Functions/m-p/253897#M48347</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-03-02T17:44:28Z</dc:date>
    </item>
    <item>
      <title>Re: String Functions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-Functions/m-p/253900#M48349</link>
      <description>&lt;P&gt;SCAN works well for most cases:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;county = scan(county, 1);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But it takes the first word only, which could be a problem for multiple-word counties:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Van Buren County&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(Yes, I made that up.&amp;nbsp; But there must be multiple-word counties somewhere.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To check whether you need a more complex approach, consider:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Do you have any multiple-word county names?&lt;/P&gt;
&lt;P&gt;2. If so, is the last word always spelled correctly ("County")?&lt;/P&gt;</description>
      <pubDate>Wed, 02 Mar 2016 17:54:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-Functions/m-p/253900#M48349</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-03-02T17:54:45Z</dc:date>
    </item>
    <item>
      <title>Re: String Functions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-Functions/m-p/253903#M48351</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31685"&gt;@robertrao﻿&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How about this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input cty $30.;
cards;
Irving County
Collin County
Denton County
Countyard County
Lake of the Woods County
;

data want;
set have;
if propcase(scan(cty, -1, ' '))='County' then cty=substr(cty, 1, length(cty)-6);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(There is a Lake of the Woods County in MN, but I made up Countyard County.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As&amp;nbsp;Astounding mentioned already, if you're going to apply this to a large dataset, you should check for data issues and special cases such as 'Denver, City and County of' etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, in general you won't be able "to remove the space" (within the character variable), unless all names have the same length.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Mar 2016 18:11:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-Functions/m-p/253903#M48351</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-03-02T18:11:22Z</dc:date>
    </item>
    <item>
      <title>Re: String Functions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-Functions/m-p/253928#M48362</link>
      <description>&lt;P&gt;Thank you Everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It works fine for all conditions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again&lt;/P&gt;</description>
      <pubDate>Wed, 02 Mar 2016 19:23:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-Functions/m-p/253928#M48362</guid>
      <dc:creator>robertrao</dc:creator>
      <dc:date>2016-03-02T19:23:12Z</dc:date>
    </item>
    <item>
      <title>Re: String Functions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-Functions/m-p/253932#M48365</link>
      <description>&lt;P&gt;I have a question o how this is working:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;propcase&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;scan&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;cty&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;-1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; &lt;SPAN class="token string"&gt;' '&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'County'&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; cty&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;substr&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;cty&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; &lt;FONT color="#ff0000"&gt;&lt;SPAN class="token function"&gt;length&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;cty&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;-6&lt;/SPAN&gt;&lt;/FONT&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="token punctuation"&gt;cant i just use: &lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;propcase&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;scan&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;cty&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;-1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; &lt;SPAN class="token string"&gt;' '&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'County'&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; cty&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;FONT color="#ff0000"&gt;&lt;SPAN class="token function"&gt;substr&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;cty&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;/FONT&gt;&lt;SPAN class="token punctuation"&gt;&lt;FONT color="#ff0000"&gt;,-6&lt;/FONT&gt;);&amp;nbsp; why dont this work?????&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="token punctuation"&gt;&lt;SPAN class="token punctuation"&gt;Thanks&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Mar 2016 19:27:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-Functions/m-p/253932#M48365</guid>
      <dc:creator>robertrao</dc:creator>
      <dc:date>2016-03-02T19:27:27Z</dc:date>
    </item>
    <item>
      <title>Re: String Functions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-Functions/m-p/253945#M48367</link>
      <description>&lt;P&gt;Well, the syntax of the &lt;A href="http://support.sas.com/documentation/cdl/en/syntaxidx/68719/HTML/default/index.htm#/documentation/cdl/en/lefunctionsref/67960/HTML/default/n0n08xougp40i5n1xw7njpcy0a2b.htm" target="_blank"&gt;SUBSTR function&lt;/A&gt; specifies that the third argument "&lt;SPAN&gt;&lt;EM&gt;is the length of the substring to extract.&lt;/EM&gt;"&amp;nbsp;You want to extract the substring beginning at position 1 and ending either in the blank before "County" or in the character immediately before that blank (the result is effectively the same). The length of this substring is &lt;FONT face="courier new,courier"&gt;length(cty)-6&lt;/FONT&gt; or &lt;FONT face="courier new,courier"&gt;length(cty)-7&lt;/FONT&gt;, respectively. It is definitely not -6, because, obviously, the length of a (sub)string, i.e. a number of characters, cannot be negative.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Mar 2016 19:44:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-Functions/m-p/253945#M48367</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-03-02T19:44:13Z</dc:date>
    </item>
    <item>
      <title>Re: String Functions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-Functions/m-p/253960#M48371</link>
      <description>&lt;P&gt;Thanks again but I am a bit confused here.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When we are scanning from left to right we just say substr(county,1,6)&lt;/P&gt;&lt;P&gt;here we want it to scan from the right side&lt;/P&gt;&lt;P&gt;As you explained the length cannot be negative but we are still saying &lt;FONT color="#ff0000"&gt;&lt;EM&gt;&lt;FONT face="Courier New" size="2"&gt;length(County)-&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;6 which has -6 as negative&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/EM&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#ff0000"&gt;&lt;EM&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;Also i thought the 1 is the one which specifies to go from left to right or &lt;/FONT&gt;&lt;/STRONG&gt;&lt;/EM&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#ff0000"&gt;&lt;EM&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;right to left depending on the positive sign or negative sign preceding it&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/EM&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#ff0000"&gt;&lt;EM&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/EM&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;if propcase(scan(County, -&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;, &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;' '&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;))=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;'County'&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; then County=upcase(substr(County, &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;, length(County)-&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;6&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;));&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Mar 2016 20:34:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-Functions/m-p/253960#M48371</guid>
      <dc:creator>robertrao</dc:creator>
      <dc:date>2016-03-02T20:34:57Z</dc:date>
    </item>
    <item>
      <title>Re: String Functions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-Functions/m-p/253984#M48380</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31685"&gt;@robertrao&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;(...)&lt;/P&gt;
&lt;P&gt;here we want it to scan from the right side&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;We may want it to scan from the right side, but as it stands (SAS 9.4)&amp;nbsp;the syntax of the SUBSTR function does not offer this feature (please correct me if I'm wrong).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31685"&gt;@robertrao&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;As you explained the length cannot be negative but we are still saying &lt;FONT color="#ff0000"&gt;&lt;EM&gt;&lt;FONT face="Courier New" size="2"&gt;length(County)-&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;6 which has -6 as negative&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/EM&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Not &lt;FONT face="courier new,courier"&gt;-6&lt;/FONT&gt; is the third argument, but &lt;FONT face="courier new,courier"&gt;length(cty)-6&lt;/FONT&gt;, where &lt;FONT face="courier new,courier"&gt;length(cty)&lt;/FONT&gt;&amp;nbsp;evaluates to a number &amp;gt;=6 because of the IF condition: A string containing the word "County" must be at least as long as "County" itself, i.e. 6 characters. Hence the third argument is &amp;gt;=0, not negative.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31685"&gt;@robertrao&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#ff0000"&gt;&lt;EM&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;Also i thought the 1 is the one which specifies to go from left to right or &lt;/FONT&gt;&lt;/STRONG&gt;&lt;/EM&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#ff0000"&gt;&lt;EM&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;right to left depending on the positive sign or negative sign preceding it&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/EM&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The second argument of the SUBSTR function (1 in our case) specifies "&lt;SPAN&gt;the beginning character position."&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The second &lt;SPAN&gt;argument of the &lt;A href="http://support.sas.com/documentation/cdl/en/syntaxidx/68719/HTML/default/index.htm#/documentation/cdl//en/lefunctionsref/67960/HTML/default/p0jshdjy2z9zdzn1h7k90u99lyq6.htm" target="_blank"&gt;SCAN function&lt;/A&gt; (-1 in our case), however, specifies "the number of the word in the character string&lt;/SPAN&gt;&lt;SPAN&gt;" and if it "is negative, SCAN counts words from right to left in the character string."&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;SPAN&gt;So, bottom line is: Syntax details have to be accepted as they are. There is little room for interpretation or wishful thinking.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Mar 2016 21:35:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-Functions/m-p/253984#M48380</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-03-02T21:35:35Z</dc:date>
    </item>
  </channel>
</rss>

