<?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: Specifying Length of Character Variable in Data Step in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Specifying-Length-of-Character-Variable-in-Data-Step/m-p/809087#M33729</link>
    <description>Thank you for taking the time to respond. I appreciate your insight.</description>
    <pubDate>Thu, 21 Apr 2022 14:27:05 GMT</pubDate>
    <dc:creator>GeorgeBonanza</dc:creator>
    <dc:date>2022-04-21T14:27:05Z</dc:date>
    <item>
      <title>Specifying Length of Character Variable in Data Step</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Specifying-Length-of-Character-Variable-in-Data-Step/m-p/809079#M33725</link>
      <description>&lt;P&gt;I am creating a new character variable in a Data Step.&amp;nbsp; The default length is not sufficient so I need to explicitly state the length.&lt;/P&gt;&lt;P&gt;It seems that both statements below will work. I was just curious if one was more correct than the other or if it's just a matter of preference.&amp;nbsp;&amp;nbsp;Any insight is appreciated.&amp;nbsp; Thanks in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;format DESCRIPTION $8.;&lt;/P&gt;&lt;P&gt;length DESCRIPTION $ 8;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data HAVE;
	input PRODUCT $ AMT;
	datalines;
001 100
002 300
003 1000
;
run;

data WANT1;
	set HAVE;
	format DESCRIPTION $8.;

	if PRODUCT = "001" then DESCRIPTION = "BIKE";
	else if PRODUCT = "002" then DESCRIPTION = "FRISBEE";
	else if PRODUCT = "003" then DESCRIPTION = "BASEBALL";
run;

data WANT2;
	set HAVE;
	length DESCRIPTION $ 8;

	if PRODUCT = "001" then DESCRIPTION = "BIKE";
	else if PRODUCT = "002" then DESCRIPTION = "FRISBEE";
	else if PRODUCT = "003" then DESCRIPTION = "BASEBALL";
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Apr 2022 14:03:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Specifying-Length-of-Character-Variable-in-Data-Step/m-p/809079#M33725</guid>
      <dc:creator>GeorgeBonanza</dc:creator>
      <dc:date>2022-04-21T14:03:03Z</dc:date>
    </item>
    <item>
      <title>Re: Specifying Length of Character Variable in Data Step</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Specifying-Length-of-Character-Variable-in-Data-Step/m-p/809080#M33726</link>
      <description>&lt;P&gt;As you observed both statements will create a variable with a length of $8.&lt;/P&gt;
&lt;P&gt;From a purist point of view if your intention is to define the length of a variable then using the LENGTH statement is the "right" way to do it. The FORMAT statement is an indirect way to achieve the same result - but people will need more "SAS insight" to understand your code and why you're doing what you're doing.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Apr 2022 14:12:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Specifying-Length-of-Character-Variable-in-Data-Step/m-p/809080#M33726</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-04-21T14:12:36Z</dc:date>
    </item>
    <item>
      <title>Re: Specifying Length of Character Variable in Data Step</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Specifying-Length-of-Character-Variable-in-Data-Step/m-p/809081#M33727</link>
      <description>&lt;P&gt;Length statement would be the preferred method of specifying a length for a variable:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One reason is just in case the variable already exists in the data set then the Length statement can trigger a warning about that similar to this:&lt;/P&gt;
&lt;PRE&gt;WARNING: Length of character variable XXXXXX 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;Attempting to use Format in this case will only affect display, not actual length and can lead to some odd behavior when comparisons using the stored values don't match the displayed values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Apr 2022 14:14:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Specifying-Length-of-Character-Variable-in-Data-Step/m-p/809081#M33727</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-04-21T14:14:57Z</dc:date>
    </item>
    <item>
      <title>Re: Specifying Length of Character Variable in Data Step</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Specifying-Length-of-Character-Variable-in-Data-Step/m-p/809082#M33728</link>
      <description>&lt;P&gt;Use the LENGTH statement to DEFINE the length required to STORE a variable.&lt;/P&gt;
&lt;P&gt;The FORMAT statement is for telling SAS what format to use to DISPLAY the value.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The FORMAT statement does NOT define the length anymore than referencing the variable in any other statement would.&amp;nbsp; It just gives SAS more information (the width of the display format you are attaching to the variable) on which to base its GUESS about what length you wanted the variable to have than many other statements that reference the variable would.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is no need to attach the $8. format specification to a character variable with a storage length of 8 bytes.&amp;nbsp; &lt;STRONG&gt;SAS already knows how to display character variables.&lt;/STRONG&gt;&amp;nbsp; There is also some down side to attaching formats like that to character variables.&amp;nbsp; For example what if you wanted to use that data as input to a data step that was going to increase the length of the variable so it could add something on the end.&amp;nbsp; If it still has the $8. format specification attached to it your printout might truncate the value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Apr 2022 16:23:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Specifying-Length-of-Character-Variable-in-Data-Step/m-p/809082#M33728</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-04-21T16:23:43Z</dc:date>
    </item>
    <item>
      <title>Re: Specifying Length of Character Variable in Data Step</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Specifying-Length-of-Character-Variable-in-Data-Step/m-p/809087#M33729</link>
      <description>Thank you for taking the time to respond. I appreciate your insight.</description>
      <pubDate>Thu, 21 Apr 2022 14:27:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Specifying-Length-of-Character-Variable-in-Data-Step/m-p/809087#M33729</guid>
      <dc:creator>GeorgeBonanza</dc:creator>
      <dc:date>2022-04-21T14:27:05Z</dc:date>
    </item>
    <item>
      <title>Re: Specifying Length of Character Variable in Data Step</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Specifying-Length-of-Character-Variable-in-Data-Step/m-p/809088#M33730</link>
      <description>Thank you for your insight, it is much appreciated.</description>
      <pubDate>Thu, 21 Apr 2022 14:28:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Specifying-Length-of-Character-Variable-in-Data-Step/m-p/809088#M33730</guid>
      <dc:creator>GeorgeBonanza</dc:creator>
      <dc:date>2022-04-21T14:28:23Z</dc:date>
    </item>
  </channel>
</rss>

