<?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 new variable in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/length-of-a-new-variable/m-p/29441#M6913</link>
    <description>Example 1: It shows default length of the char&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data name;&lt;BR /&gt;
input subject$;&lt;BR /&gt;
cards;&lt;BR /&gt;
Social&lt;BR /&gt;
Physics&lt;BR /&gt;
Chemistry&lt;BR /&gt;
nuclearphysics&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
OUTPUT:&lt;BR /&gt;
&lt;BR /&gt;
This is default output:&lt;BR /&gt;
&lt;BR /&gt;
Social&lt;BR /&gt;
Physics&lt;BR /&gt;
Chemistr&lt;BR /&gt;
nuclearp&lt;BR /&gt;
&lt;BR /&gt;
Example 2: Length of the first variables value&lt;BR /&gt;
data name1;&lt;BR /&gt;
set name;&lt;BR /&gt;
if subject ='Social' then Class ='H';&lt;BR /&gt;
else if subject ='Physics' then Class ='H plus';&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
output:&lt;BR /&gt;
&lt;BR /&gt;
Class&lt;BR /&gt;
H&lt;BR /&gt;
H &lt;BR /&gt;
&lt;BR /&gt;
 ( But the second row in the output should be H Plus instead of H because it takes the length of the first assigment variable)&lt;BR /&gt;
&lt;BR /&gt;
and as you know you can use length subject $ 15. ;&lt;BR /&gt;
for the final option.&lt;BR /&gt;
&lt;BR /&gt;
So, all three are possible hence D is correct.</description>
    <pubDate>Tue, 15 Mar 2011 16:00:49 GMT</pubDate>
    <dc:creator>SAS83</dc:creator>
    <dc:date>2011-03-15T16:00:49Z</dc:date>
    <item>
      <title>length of a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/length-of-a-new-variable/m-p/29440#M6912</link>
      <description>Experts:&lt;BR /&gt;
Got this question from my big SAS book but could not understand the answer.&lt;BR /&gt;
Which of the following can determine the length of a new variable?&lt;BR /&gt;
a. the length of the variable's first value&lt;BR /&gt;
b. the assignment statement&lt;BR /&gt;
c. the Length statement&lt;BR /&gt;
d. all of the above.&lt;BR /&gt;
The answer is D. I could not figure out why A is also correct. I thought length is determined during compilation stage so before SAS runs into the first value of any variable?&lt;BR /&gt;
Thank you!</description>
      <pubDate>Tue, 15 Mar 2011 00:45:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/length-of-a-new-variable/m-p/29440#M6912</guid>
      <dc:creator>mnew</dc:creator>
      <dc:date>2011-03-15T00:45:54Z</dc:date>
    </item>
    <item>
      <title>Re: length of a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/length-of-a-new-variable/m-p/29441#M6913</link>
      <description>Example 1: It shows default length of the char&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data name;&lt;BR /&gt;
input subject$;&lt;BR /&gt;
cards;&lt;BR /&gt;
Social&lt;BR /&gt;
Physics&lt;BR /&gt;
Chemistry&lt;BR /&gt;
nuclearphysics&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
OUTPUT:&lt;BR /&gt;
&lt;BR /&gt;
This is default output:&lt;BR /&gt;
&lt;BR /&gt;
Social&lt;BR /&gt;
Physics&lt;BR /&gt;
Chemistr&lt;BR /&gt;
nuclearp&lt;BR /&gt;
&lt;BR /&gt;
Example 2: Length of the first variables value&lt;BR /&gt;
data name1;&lt;BR /&gt;
set name;&lt;BR /&gt;
if subject ='Social' then Class ='H';&lt;BR /&gt;
else if subject ='Physics' then Class ='H plus';&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
output:&lt;BR /&gt;
&lt;BR /&gt;
Class&lt;BR /&gt;
H&lt;BR /&gt;
H &lt;BR /&gt;
&lt;BR /&gt;
 ( But the second row in the output should be H Plus instead of H because it takes the length of the first assigment variable)&lt;BR /&gt;
&lt;BR /&gt;
and as you know you can use length subject $ 15. ;&lt;BR /&gt;
for the final option.&lt;BR /&gt;
&lt;BR /&gt;
So, all three are possible hence D is correct.</description>
      <pubDate>Tue, 15 Mar 2011 16:00:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/length-of-a-new-variable/m-p/29441#M6913</guid>
      <dc:creator>SAS83</dc:creator>
      <dc:date>2011-03-15T16:00:49Z</dc:date>
    </item>
    <item>
      <title>Re: length of a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/length-of-a-new-variable/m-p/29442#M6914</link>
      <description>Thanks for the help. I'm still confused. The example one seems to reflect the default character var length is 8 rule. I'm not sure how the length is related to the first value.</description>
      <pubDate>Tue, 15 Mar 2011 20:47:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/length-of-a-new-variable/m-p/29442#M6914</guid>
      <dc:creator>mnew</dc:creator>
      <dc:date>2011-03-15T20:47:15Z</dc:date>
    </item>
    <item>
      <title>Re: length of a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/length-of-a-new-variable/m-p/29443#M6915</link>
      <description>The length can be related to the variable's first value when an assignment is made in the data step (with a constant). Your understanding of compilation and execution is correct and the data is not read until execution...&lt;BR /&gt;
&lt;BR /&gt;
The following is taken from the SAS 9.2 Language Reference manual &lt;A href="http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000695113.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000695113.htm&lt;/A&gt; &lt;BR /&gt;
In a DATA step, you can create a new variable and assign it a value by using it for the first time on the left side of an assignment statement. SAS determines the length of a variable from its first occurrence in the DATA step. The new variable gets the same type and length as the expression on the right side of the assignment statement.&lt;BR /&gt;
&lt;BR /&gt;
I think the wording of the response (a.) in your "big SAS book" is a bit vague as it implies data that you are reading in rather than through an assignment statement.&lt;BR /&gt;
&lt;BR /&gt;
Cheers,&lt;BR /&gt;
Michelle&lt;BR /&gt;
&lt;A href="http://twitter.com/homesatmetacoda" target="_blank"&gt;http://twitter.com/homesatmetacoda&lt;/A&gt;</description>
      <pubDate>Tue, 15 Mar 2011 23:13:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/length-of-a-new-variable/m-p/29443#M6915</guid>
      <dc:creator>MichelleHomes</dc:creator>
      <dc:date>2011-03-15T23:13:09Z</dc:date>
    </item>
    <item>
      <title>Re: length of a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/length-of-a-new-variable/m-p/29444#M6916</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi;&lt;/P&gt;&lt;P&gt;Just imagine, if you use the same variable 3 times conditionally say for eg in if-then-else construct.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if a&amp;gt;100 then length='Long';&lt;/P&gt;&lt;P&gt;else if 50&amp;lt;=a&amp;lt;=100 then length='Normal';&lt;/P&gt;&lt;P&gt;else if a&amp;lt;50 then length='short';&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;During compilation, the&lt;STRONG&gt; first instance&lt;/STRONG&gt; the variable 'length' above is coupled with the value 'Long' which is only 4 bytes long. This is the case which you are confused of.&lt;/P&gt;&lt;P&gt;In the second case, even if the same variable is assigned with value &lt;STRONG&gt;'Normal', which is 6 bytes long,&lt;/STRONG&gt; it cannot be accommodated, means its get truncated to 4 char length.&lt;/P&gt;&lt;P&gt;So, the &lt;STRONG&gt;first value&lt;/STRONG&gt;&amp;nbsp; which is 4 char is determined as the variable's length for later use!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;To avoid such truncations, SAS have LENGTH&amp;nbsp; statement which you should use well before you actually use the variables in your statements like a declaration. You need to find the space needed for the longest value of the variable&amp;nbsp; and then assign this number to that. So that there is no truncation happens like I said before. Hope this helps!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;LENGTH length $ 6;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;if a&amp;gt;100 then length='Long';&lt;/P&gt;&lt;P&gt;else if 50&amp;lt;=a&amp;lt;=100 then length='Normal';&lt;/P&gt;&lt;P&gt;else if a&amp;lt;50 then length='short'; /* Now its perfect*/&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Cheers!&lt;/P&gt;&lt;P&gt;Jojan.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 23 Mar 2014 20:29:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/length-of-a-new-variable/m-p/29444#M6916</guid>
      <dc:creator>JVarghese</dc:creator>
      <dc:date>2014-03-23T20:29:59Z</dc:date>
    </item>
  </channel>
</rss>

