<?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: Length of a character variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Length-of-a-character-variable/m-p/56137#M11977</link>
    <description>HI Patrick&lt;BR /&gt;
&lt;BR /&gt;
If I go by your explanation then why is 'a' not equal to 16?</description>
    <pubDate>Wed, 29 Dec 2010 16:34:14 GMT</pubDate>
    <dc:creator>JatinRai</dc:creator>
    <dc:date>2010-12-29T16:34:14Z</dc:date>
    <item>
      <title>Length of a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Length-of-a-character-variable/m-p/56134#M11974</link>
      <description>Hi&lt;BR /&gt;
&lt;BR /&gt;
I submitted the following code:&lt;BR /&gt;
&lt;BR /&gt;
data _null_;&lt;BR /&gt;
      first= 'ipswich, england';&lt;BR /&gt;
      city=substr(first,1,7)||', '||'England';&lt;BR /&gt;
      l=lengthc(city);&lt;BR /&gt;
      a=lengthc(substr(first,1,7));&lt;BR /&gt;
      put l= a=;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
O/P is l=25 a=7.&lt;BR /&gt;
&lt;BR /&gt;
Can someone explain why is l=25? What I understand is that it equals 16 bytes of first + 2 bytes for space &amp;amp; comma + 7 bytes for england. But why does this happen?&lt;BR /&gt;
&lt;BR /&gt;
And second question is that why is a not equal to 16?</description>
      <pubDate>Wed, 29 Dec 2010 05:52:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Length-of-a-character-variable/m-p/56134#M11974</guid>
      <dc:creator>JatinRai</dc:creator>
      <dc:date>2010-12-29T05:52:40Z</dc:date>
    </item>
    <item>
      <title>Re: Length of a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Length-of-a-character-variable/m-p/56135#M11975</link>
      <description>Hi &lt;BR /&gt;
&lt;BR /&gt;
It seems that SAS uses the length of variable "first" and not of the substring to create the new variable "city" (length: 16 +2 + 7 = 25).&lt;BR /&gt;
&lt;BR /&gt;
HTH&lt;BR /&gt;
Patrick</description>
      <pubDate>Wed, 29 Dec 2010 06:59:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Length-of-a-character-variable/m-p/56135#M11975</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2010-12-29T06:59:57Z</dc:date>
    </item>
    <item>
      <title>Re: Length of a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Length-of-a-character-variable/m-p/56136#M11976</link>
      <description>Hi.&lt;BR /&gt;
All make me some sense is 'substr(first,1,7)'  is only in PDV ( not write into disk) so SAS will treat it has the same storage length with variable 'First' . and once you write it into dataset (such as ' a=substr(first,1,7) '),it will has  7 length of storage.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Wed, 29 Dec 2010 07:24:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Length-of-a-character-variable/m-p/56136#M11976</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2010-12-29T07:24:25Z</dc:date>
    </item>
    <item>
      <title>Re: Length of a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Length-of-a-character-variable/m-p/56137#M11977</link>
      <description>HI Patrick&lt;BR /&gt;
&lt;BR /&gt;
If I go by your explanation then why is 'a' not equal to 16?</description>
      <pubDate>Wed, 29 Dec 2010 16:34:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Length-of-a-character-variable/m-p/56137#M11977</guid>
      <dc:creator>JatinRai</dc:creator>
      <dc:date>2010-12-29T16:34:14Z</dc:date>
    </item>
    <item>
      <title>Re: Length of a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Length-of-a-character-variable/m-p/56138#M11978</link>
      <description>Jatin Rai wrote:&lt;BR /&gt;&lt;BR /&gt;
&amp;gt; If I go by your explanation then why is 'a' not equal to 16?&lt;BR /&gt;&lt;BR /&gt;
&lt;BR /&gt;&lt;BR /&gt;
Good question! Who knows. SUBSTR() and LENGTHC() may work differently. SAS, as any languages, isn't completely regular, and has its share of quarks.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;
It is a good practice to declare the length of a character variable before you use it and not to let sas guess.</description>
      <pubDate>Wed, 29 Dec 2010 17:06:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Length-of-a-character-variable/m-p/56138#M11978</guid>
      <dc:creator>chang_y_chung_hotmail_com</dc:creator>
      <dc:date>2010-12-29T17:06:53Z</dc:date>
    </item>
    <item>
      <title>Re: Length of a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Length-of-a-character-variable/m-p/56139#M11979</link>
      <description>SAS does have expected behavior when dealing with CHARACTER type variable assignments - pretty well documented and mostly consistent.&lt;BR /&gt;
&lt;BR /&gt;
Suggest if you want to ensure an expected SAS CHARACTER variable length to avoid truncation, use an ATTRIB or LENGTH statement (and not a FORMAT statement, by the way).  And you will want to code the statement ahead of the first reference to a given SAS CHARACTER variable, that being either with an assignment stmt, a SET, or a RETAIN, to cover most instances.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Wed, 29 Dec 2010 17:22:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Length-of-a-character-variable/m-p/56139#M11979</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-12-29T17:22:46Z</dc:date>
    </item>
    <item>
      <title>Re: Length of a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Length-of-a-character-variable/m-p/56140#M11980</link>
      <description>Jatin,&lt;BR /&gt;
&lt;BR /&gt;
I don't know if the details regarding assigning variable length in such a condition are documented but, if you want the calculation to equal 16, just use a function that doesn't include trailing blanks.  I.e., use length rather than lengthn.&lt;BR /&gt;
&lt;BR /&gt;
HTH,&lt;BR /&gt;
Art</description>
      <pubDate>Wed, 29 Dec 2010 22:46:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Length-of-a-character-variable/m-p/56140#M11980</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2010-12-29T22:46:22Z</dc:date>
    </item>
    <item>
      <title>Re: Length of a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Length-of-a-character-variable/m-p/56141#M11981</link>
      <description>Hi Jatin&lt;BR /&gt;
&lt;BR /&gt;
We SAS users are just too spoiled by SAS doing so much for us implicit.&lt;BR /&gt;
&lt;BR /&gt;
So may be SAS R&amp;amp;D decided here to take the safe way and assign a variable length which is always long enough - instead of trying to cater for all the possibilities of how people might create a concatenated string.&lt;BR /&gt;
&lt;BR /&gt;
I.e: using cat() for concatenation you even end up with the default length for character variables ($200 in my environment).&lt;BR /&gt;
&lt;BR /&gt;
As others already said: If you want to be sure what you get define it explicit using a length statement.&lt;BR /&gt;
&lt;BR /&gt;
HTH&lt;BR /&gt;
Patrick</description>
      <pubDate>Thu, 30 Dec 2010 01:46:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Length-of-a-character-variable/m-p/56141#M11981</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2010-12-30T01:46:10Z</dc:date>
    </item>
  </channel>
</rss>

