<?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: Assigning vale to a variable- inconsistent results in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Assigning-vale-to-a-variable-inconsistent-results/m-p/464007#M118290</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A new character variable's length is determined by the first assignment (if it has not been determined explicitly, e.g. by a &lt;FONT face="courier new,courier"&gt;length&lt;/FONT&gt; statement.)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So in&amp;nbsp;the data step for&amp;nbsp;test5, &lt;FONT face="courier new,courier"&gt;x='1'&lt;/FONT&gt; sets the length of x to 1 character.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Try using a length statement before the assignment, e.g.:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test5;
   length x $ 8;
   x='1';
   x='11';
   x='111';
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Amir.&lt;/P&gt;</description>
    <pubDate>Tue, 22 May 2018 11:15:29 GMT</pubDate>
    <dc:creator>Amir</dc:creator>
    <dc:date>2018-05-22T11:15:29Z</dc:date>
    <item>
      <title>Assigning vale to a variable- inconsistent results</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-vale-to-a-variable-inconsistent-results/m-p/464001#M118287</link>
      <description>&lt;P&gt;The below code gives the result 3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test4;
x='1';
x='2';
x='3';
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But the below code gives the result 1, why is it not 111?&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test5;
x='1';
x='11';
x='111';
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;And the below code gives the result 1.&amp;nbsp;Why is it 1?&lt;/P&gt;&lt;PRE&gt;data t;&lt;BR /&gt;x='2';&lt;BR /&gt;x='3';&lt;BR /&gt;x='111';&lt;BR /&gt;run;&lt;/PRE&gt;&lt;P&gt;Why do we get such different results?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 May 2018 11:03:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-vale-to-a-variable-inconsistent-results/m-p/464001#M118287</guid>
      <dc:creator>riyaaora275</dc:creator>
      <dc:date>2018-05-22T11:03:52Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning vale to a variable- inconsistent results</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-vale-to-a-variable-inconsistent-results/m-p/464004#M118288</link>
      <description>&lt;P&gt;"&lt;SPAN&gt;Why do we get such different results? " - because you have not read the SAS Help documentation on these core concepts.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1) You get 3 and only three output as there is an implied output at the end of the loop, as none is specified.&amp;nbsp; So X gets set to 1, then to 2, then to 3 and the row is then output.&amp;nbsp; Fix:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; test4&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token operator"&gt;  x=&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'1'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;; output;&lt;/SPAN&gt;
  x&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'2'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;; output;&lt;/SPAN&gt;
  x&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'3'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;; output;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2) In this one, X is a new variable, and you have not set a length for the variable.&amp;nbsp; Therefore it is assigned implicitly based on the first data, which is character length 1.&amp;nbsp; So X gets set to 1, then to 1 again, as it only takes the first character, then to one again, as it only accepts 1 character.&amp;nbsp; Fix:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; test5&lt;SPAN class="token punctuation"&gt;;&lt;BR /&gt;&lt;/SPAN&gt;  length x $3;
  x&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'1'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  x&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'11'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  x&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'111'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;3) This question is exactly the same as 2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Goo idea to learn the basics on these types of things, and also a very good example of why not to let the computer try to guess what you want.&amp;nbsp; Always be explicit in your definitions and program, never rely on the computer to guess anything for you.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 May 2018 11:13:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-vale-to-a-variable-inconsistent-results/m-p/464004#M118288</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-05-22T11:13:25Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning vale to a variable- inconsistent results</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-vale-to-a-variable-inconsistent-results/m-p/464005#M118289</link>
      <description>&lt;P&gt;since you are defining x as a charaacter and there is no length statement.. so it takes the length of x as the first statement it encounters.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if your second program would be :&lt;/P&gt;&lt;P&gt;data test5;&lt;BR /&gt;x='1';&lt;BR /&gt;x='11';&lt;BR /&gt;x='311';&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;u would have got the result 3.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And if you write it as :&lt;/P&gt;&lt;P&gt;data test5;&lt;BR /&gt;length x $ 3.;&lt;BR /&gt;x='1';&lt;BR /&gt;x='11';&lt;BR /&gt;x='311';&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;you would get 311&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope I answered your query&lt;/P&gt;</description>
      <pubDate>Tue, 22 May 2018 11:14:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-vale-to-a-variable-inconsistent-results/m-p/464005#M118289</guid>
      <dc:creator>ruchi11dec</dc:creator>
      <dc:date>2018-05-22T11:14:54Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning vale to a variable- inconsistent results</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-vale-to-a-variable-inconsistent-results/m-p/464007#M118290</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A new character variable's length is determined by the first assignment (if it has not been determined explicitly, e.g. by a &lt;FONT face="courier new,courier"&gt;length&lt;/FONT&gt; statement.)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So in&amp;nbsp;the data step for&amp;nbsp;test5, &lt;FONT face="courier new,courier"&gt;x='1'&lt;/FONT&gt; sets the length of x to 1 character.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Try using a length statement before the assignment, e.g.:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test5;
   length x $ 8;
   x='1';
   x='11';
   x='111';
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Tue, 22 May 2018 11:15:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-vale-to-a-variable-inconsistent-results/m-p/464007#M118290</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2018-05-22T11:15:29Z</dc:date>
    </item>
  </channel>
</rss>

