<?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 variable when using substr(scan()) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/length-of-variable-when-using-substr-scan/m-p/392341#M94384</link>
    <description>&lt;P&gt;The Length of the Result&lt;BR /&gt;In a DATA step, most variables have a fixed length. If the word returned by the SCAN function is assigned to a variable that has a fixed length greater than the length of the returned word, then the value of that variable will be padded with blanks. Macro variables have varying lengths and are not padded with blanks.&lt;BR /&gt;The maximum length of the word that is returned by the SCAN function depends on the environment from which it is called:&lt;BR /&gt;&lt;STRONG&gt;&lt;EM&gt;In a DATA step, if the SCAN function returns a value to a variable that has not yet been given a length, then that variable is given a length of 200 characters. If you need the SCAN function to assign to a variable a word that is longer than 200 characters, then you should explicitly specify the length of that variable.&lt;/EM&gt;&lt;/STRONG&gt;&lt;BR /&gt;If you use the SCAN function in an expression that contains operators or other functions, a word that is returned by the SCAN function can have a length of up to 32,767 characters, except in a WHERE clause. In that case, the maximum length is 200 characters.&lt;BR /&gt;In the SQL procedure, or in a WHERE clause in any procedure, the maximum length of a word that is returned by the SCAN function is 200 characters.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 31 Aug 2017 19:32:13 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2017-08-31T19:32:13Z</dc:date>
    <item>
      <title>length of variable when using substr(scan())</title>
      <link>https://communities.sas.com/t5/SAS-Programming/length-of-variable-when-using-substr-scan/m-p/392337#M94383</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is the program:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.test;
Author = 'Agatha Christie';
First = substr(scan(author,1,' ,'),1,1);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Can you tell me why the &lt;STRONG&gt;length&lt;/STRONG&gt; of &lt;STRONG&gt;first&lt;/STRONG&gt; is returned as &lt;STRONG&gt;200&amp;nbsp;&lt;/STRONG&gt;even when &lt;STRONG&gt;author&lt;/STRONG&gt; has&amp;nbsp;&lt;STRONG&gt;15....??&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Aug 2017 19:26:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/length-of-variable-when-using-substr-scan/m-p/392337#M94383</guid>
      <dc:creator>ab12nov</dc:creator>
      <dc:date>2017-08-31T19:26:36Z</dc:date>
    </item>
    <item>
      <title>Re: length of variable when using substr(scan())</title>
      <link>https://communities.sas.com/t5/SAS-Programming/length-of-variable-when-using-substr-scan/m-p/392341#M94384</link>
      <description>&lt;P&gt;The Length of the Result&lt;BR /&gt;In a DATA step, most variables have a fixed length. If the word returned by the SCAN function is assigned to a variable that has a fixed length greater than the length of the returned word, then the value of that variable will be padded with blanks. Macro variables have varying lengths and are not padded with blanks.&lt;BR /&gt;The maximum length of the word that is returned by the SCAN function depends on the environment from which it is called:&lt;BR /&gt;&lt;STRONG&gt;&lt;EM&gt;In a DATA step, if the SCAN function returns a value to a variable that has not yet been given a length, then that variable is given a length of 200 characters. If you need the SCAN function to assign to a variable a word that is longer than 200 characters, then you should explicitly specify the length of that variable.&lt;/EM&gt;&lt;/STRONG&gt;&lt;BR /&gt;If you use the SCAN function in an expression that contains operators or other functions, a word that is returned by the SCAN function can have a length of up to 32,767 characters, except in a WHERE clause. In that case, the maximum length is 200 characters.&lt;BR /&gt;In the SQL procedure, or in a WHERE clause in any procedure, the maximum length of a word that is returned by the SCAN function is 200 characters.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Aug 2017 19:32:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/length-of-variable-when-using-substr-scan/m-p/392341#M94384</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2017-08-31T19:32:13Z</dc:date>
    </item>
    <item>
      <title>Re: length of variable when using substr(scan())</title>
      <link>https://communities.sas.com/t5/SAS-Programming/length-of-variable-when-using-substr-scan/m-p/392363#M94394</link>
      <description>I would add that I think it's good practice to always assign a length to character variables with the LENGTH statement - you can save yourself a lot of pain by doing that. Numeric variables will always have a default length of 8 which is fine so most people won't bother declaring  them.</description>
      <pubDate>Thu, 31 Aug 2017 20:30:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/length-of-variable-when-using-substr-scan/m-p/392363#M94394</guid>
      <dc:creator>ChrisBrooks</dc:creator>
      <dc:date>2017-08-31T20:30:02Z</dc:date>
    </item>
    <item>
      <title>Re: length of variable when using substr(scan())</title>
      <link>https://communities.sas.com/t5/SAS-Programming/length-of-variable-when-using-substr-scan/m-p/392370#M94397</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/155414"&gt;@ab12nov&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below is the program:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.test;
Author = 'Agatha Christie';
First = substr(scan(author,1,' ,'),1,1);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Can you tell me why the &lt;STRONG&gt;length&lt;/STRONG&gt; of &lt;STRONG&gt;first&lt;/STRONG&gt; is returned as &lt;STRONG&gt;200&amp;nbsp;&lt;/STRONG&gt;even when &lt;STRONG&gt;author&lt;/STRONG&gt; has&amp;nbsp;&lt;STRONG&gt;15....??&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Because SAS cannot read your mind, although SAS 9.4 seems to know that 15 is the max that function will generate.&lt;/P&gt;
&lt;P&gt;Note that there are functions like FIRST() and CHAR() that SAS knows will&amp;nbsp;only return one character.&lt;/P&gt;
&lt;P&gt;But if you want FIRST to be one character long then define it that way. &amp;nbsp;And if you did define it that way then you don't really need SUBSTR() function anymore.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
    Author = 'Agatha Christie';
    First1 = substr(scan(author,1,' ,'),1,1);
    First2 = first(scan(author,1,' ,'));
    First3 = char(scan(author,1,' ,'),1);
    length first $1 ;
    first= scan(author,1,' ,') ;
run;
proc contents; run;
proc print; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 324px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/14770i227BE34AC35B9508/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Aug 2017 20:51:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/length-of-variable-when-using-substr-scan/m-p/392370#M94397</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-08-31T20:51:07Z</dc:date>
    </item>
  </channel>
</rss>

