<?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 substr in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/substr/m-p/472059#M120978</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;can you plz explain how come the length of variable sec is 25 ?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;first = 'ipswich, england';&lt;BR /&gt;sec = substr(first,1,7)!!','!!'England';&lt;BR /&gt;run;&lt;/P&gt;</description>
    <pubDate>Thu, 21 Jun 2018 12:27:46 GMT</pubDate>
    <dc:creator>sanyam13</dc:creator>
    <dc:date>2018-06-21T12:27:46Z</dc:date>
    <item>
      <title>substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr/m-p/472059#M120978</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;can you plz explain how come the length of variable sec is 25 ?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;first = 'ipswich, england';&lt;BR /&gt;sec = substr(first,1,7)!!','!!'England';&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jun 2018 12:27:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr/m-p/472059#M120978</guid>
      <dc:creator>sanyam13</dc:creator>
      <dc:date>2018-06-21T12:27:46Z</dc:date>
    </item>
    <item>
      <title>Re: substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr/m-p/472064#M120980</link>
      <description>&lt;P&gt;It is because you have not explicitly set any lengths SAS uses its defaults.&lt;/P&gt;
&lt;P&gt;So:&lt;BR /&gt;first is set to 16 as that is the length of the first string applied to it.&lt;/P&gt;
&lt;P&gt;Then the substr is called, and logically the maximum value that this can return is length of first, + 1 for the comma, + length of string England = 24, so sec is set to be length 24, so that it can fully cover anything the substr()+,+England can return.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Solution,&amp;nbsp;&lt;U&gt;&lt;STRONG&gt;always&lt;/STRONG&gt;&lt;/U&gt; set lengths yourself, don't let the software try to guess for you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jun 2018 12:34:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr/m-p/472064#M120980</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-06-21T12:34:30Z</dc:date>
    </item>
    <item>
      <title>Re: substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr/m-p/472069#M120981</link>
      <description>&lt;P&gt;The key to understanding why is to simplify the problem:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;sec = substr(first, 1, 7);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here, the length of SEC is 16, the same as the length of FIRST.&amp;nbsp; The reason:&amp;nbsp; the third parameter to SUBSTR does not have to be hard-coded.&amp;nbsp; It can be an expression, calculated based on variables within the incoming data.&amp;nbsp; So when SAS sees SUBSTR, it doesn't even look at the third parameter.&amp;nbsp; Rather, it notes that it is taking some portion of FIRST.&amp;nbsp; It might be taking the whole thing.&amp;nbsp; So to be safe, SAS assigns SEC the same length as FIRST.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you add ", England" you just have to add that many characters to the length of FIRST.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jun 2018 12:38:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr/m-p/472069#M120981</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-06-21T12:38:44Z</dc:date>
    </item>
    <item>
      <title>Re: substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr/m-p/472070#M120982</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/214904"&gt;@sanyam13&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;can you plz explain how come the length of variable sec is 25 ?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data test;&lt;BR /&gt;first = 'ipswich, england';&lt;BR /&gt;sec = substr(first,1,7)!!','!!'England';&lt;BR /&gt;run;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;SAS makes&amp;nbsp;&lt;EM&gt;guesses&lt;/EM&gt; about the required length for a new variable, depending on the length of variables and literals used. To prevent such guesses from happening (as they usually don't end up with the desired result), it is best practice to use the length statement.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jun 2018 12:39:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr/m-p/472070#M120982</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-06-21T12:39:30Z</dc:date>
    </item>
  </channel>
</rss>

