<?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 Length of variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Length-of-variable/m-p/324266#M71974</link>
    <description>&lt;P class="reader-word-layer reader-word-s7-14"&gt;The&amp;nbsp;following&amp;nbsp;SAS&amp;nbsp;program&amp;nbsp;is&amp;nbsp;submitted:&amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s7-40"&gt;&amp;nbsp;data&amp;nbsp;names;&amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-7"&gt;title='EDU';&amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-11"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;title='EDU'&amp;nbsp;then&amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-7"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Division='Education';&amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-11"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&amp;nbsp;if&amp;nbsp;title='HR'&amp;nbsp;then&amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-12"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Division='Human&amp;nbsp;Resources';&amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-7"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&amp;nbsp;Division='Unknown';&amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-13"&gt;run;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-13"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-15"&gt;Which&amp;nbsp;one&amp;nbsp;of&amp;nbsp;the&amp;nbsp;following&amp;nbsp;represents&amp;nbsp;the&amp;nbsp;value&amp;nbsp;of&amp;nbsp;the&amp;nbsp;variable&amp;nbsp;Division in&amp;nbsp;the&amp;nbsp;output&amp;nbsp;data&amp;nbsp;set?&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-24"&gt;a.Educatio&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-15"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-24"&gt;b.Education&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-15"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-23 reader-word-s8-26"&gt;c.Human&amp;nbsp;Re&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-27"&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-24"&gt;d.Human&amp;nbsp;Resources&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-15"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-15"&gt;This is one of the SAS Base Programming practice questions from SAS. The correct answer is b. The explanation said &amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-15"&gt;"The&amp;nbsp;length&amp;nbsp;of&amp;nbsp;the&amp;nbsp;variable&amp;nbsp;Division is set&amp;nbsp;to&amp;nbsp;9&amp;nbsp;when&amp;nbsp;the&amp;nbsp;DATA&amp;nbsp;step&amp;nbsp;compiles". I cannot see why is it set to 9 bytes when the DATA step compiles. There is no explicit&amp;nbsp;LENGTH statement and the default length for a character variable is 8. Anyone can help? Thank you!&lt;/P&gt;</description>
    <pubDate>Thu, 12 Jan 2017 15:08:27 GMT</pubDate>
    <dc:creator>cookiejar</dc:creator>
    <dc:date>2017-01-12T15:08:27Z</dc:date>
    <item>
      <title>Length of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Length-of-variable/m-p/324266#M71974</link>
      <description>&lt;P class="reader-word-layer reader-word-s7-14"&gt;The&amp;nbsp;following&amp;nbsp;SAS&amp;nbsp;program&amp;nbsp;is&amp;nbsp;submitted:&amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s7-40"&gt;&amp;nbsp;data&amp;nbsp;names;&amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-7"&gt;title='EDU';&amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-11"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;title='EDU'&amp;nbsp;then&amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-7"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Division='Education';&amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-11"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&amp;nbsp;if&amp;nbsp;title='HR'&amp;nbsp;then&amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-12"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Division='Human&amp;nbsp;Resources';&amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-7"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&amp;nbsp;Division='Unknown';&amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-13"&gt;run;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-13"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-15"&gt;Which&amp;nbsp;one&amp;nbsp;of&amp;nbsp;the&amp;nbsp;following&amp;nbsp;represents&amp;nbsp;the&amp;nbsp;value&amp;nbsp;of&amp;nbsp;the&amp;nbsp;variable&amp;nbsp;Division in&amp;nbsp;the&amp;nbsp;output&amp;nbsp;data&amp;nbsp;set?&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-24"&gt;a.Educatio&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-15"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-24"&gt;b.Education&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-15"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-23 reader-word-s8-26"&gt;c.Human&amp;nbsp;Re&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-27"&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-24"&gt;d.Human&amp;nbsp;Resources&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-15"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-15"&gt;This is one of the SAS Base Programming practice questions from SAS. The correct answer is b. The explanation said &amp;nbsp;&lt;/P&gt;&lt;P class="reader-word-layer reader-word-s8-15"&gt;"The&amp;nbsp;length&amp;nbsp;of&amp;nbsp;the&amp;nbsp;variable&amp;nbsp;Division is set&amp;nbsp;to&amp;nbsp;9&amp;nbsp;when&amp;nbsp;the&amp;nbsp;DATA&amp;nbsp;step&amp;nbsp;compiles". I cannot see why is it set to 9 bytes when the DATA step compiles. There is no explicit&amp;nbsp;LENGTH statement and the default length for a character variable is 8. Anyone can help? Thank you!&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jan 2017 15:08:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Length-of-variable/m-p/324266#M71974</guid>
      <dc:creator>cookiejar</dc:creator>
      <dc:date>2017-01-12T15:08:27Z</dc:date>
    </item>
    <item>
      <title>Re: Length of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Length-of-variable/m-p/324270#M71975</link>
      <description>&lt;P&gt;For character variables, the SAS DATA step compiler assigns lengths in this order:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;an explicit LENGTH statement&lt;/P&gt;
&lt;P&gt;the first character string it encounters&lt;/P&gt;
&lt;P&gt;the default of 8.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use this search to get some useful articles on the topic&lt;/P&gt;
&lt;P&gt;sas data step compiler.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Doc Muhlbaier&lt;/P&gt;
&lt;P&gt;Duke&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jan 2017 15:16:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Length-of-variable/m-p/324270#M71975</guid>
      <dc:creator>Doc_Duke</dc:creator>
      <dc:date>2017-01-12T15:16:57Z</dc:date>
    </item>
    <item>
      <title>Re: Length of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Length-of-variable/m-p/324271#M71976</link>
      <description>&lt;P&gt;To my knowledge a SAS character variable only defaults to a length of 8 when you define it in an input statement without a specific length definition. Something like: &lt;EM&gt;INPUT myvar $;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With SAS you don't have to explicitely declare variables in a data step. The SAS compiler simply processes the run group (the data step) once from top to bottom and creates all the variables for the PDV. It uses the first variable definition it finds.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your case the first time variable &lt;EM&gt;Division&lt;/EM&gt; gets mentioned, is in the statement&amp;nbsp;&lt;EM&gt;Division='Education'; &lt;/EM&gt;Here the assigned string &lt;EM&gt;Education&lt;/EM&gt;&amp;nbsp;consists of 9 characters and that's why SAS creates the character variable &lt;EM&gt;Division&lt;/EM&gt;&amp;nbsp;with a length of 9.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So yes, it's great that SAS does all these things implicitely for us (unlike in other languages) but it also leads sometimes to a lot of confusion. I still remember mine many many year ago where I couldn't understand why some strings got truncated (=consecutive strings were longer but as the variable had already been defined with a specific length these strings got truncated).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This top-to-bottom, first comes first approach is even true when you're explicitely using a declarative&amp;nbsp;&lt;EM&gt;Length&lt;/EM&gt; statement. At least in such a case SAS throws a warning.&lt;/P&gt;
&lt;PRE&gt;15         data test;
16           a='abc';
17           length a $10;
WARNING: Length of character variable a has already been set. 
         Use the LENGTH statement as the very first statement in the DATA STEP to declare the length of a character variable.&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jan 2017 15:31:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Length-of-variable/m-p/324271#M71976</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-01-12T15:31:24Z</dc:date>
    </item>
    <item>
      <title>Re: Length of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Length-of-variable/m-p/324279#M71977</link>
      <description>&lt;P&gt;For added fun check the length of Division in this case.&lt;/P&gt;
&lt;PRE&gt;data names; 
   title='EDU'; 
   if title='EDU' then 
      Division=cats('Education',''); 
   else if title='HR' then 
      Division='Human Resources'; 
   else Division='Unknown'; 
run;
&lt;/PRE&gt;
&lt;P&gt;I really wouldn't consider this a solution. More pointing out&amp;nbsp;that&amp;nbsp;how you create the variable can also lead to unexpected lengths, in this case 200, for a&amp;nbsp;variable. &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jan 2017 17:39:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Length-of-variable/m-p/324279#M71977</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-01-12T17:39:58Z</dc:date>
    </item>
    <item>
      <title>Re: Length of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Length-of-variable/m-p/324310#M71989</link>
      <description>&lt;P&gt;Thank you so much for your detailed answer. The solution gives me more insight.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jan 2017 16:23:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Length-of-variable/m-p/324310#M71989</guid>
      <dc:creator>cookiejar</dc:creator>
      <dc:date>2017-01-12T16:23:20Z</dc:date>
    </item>
  </channel>
</rss>

