<?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 or format of a character variable in a data step in SAS Studio</title>
    <link>https://communities.sas.com/t5/SAS-Studio/length-or-format-of-a-character-variable-in-a-data-step/m-p/705891#M9576</link>
    <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Please go back to the Programming1 course and review the lectures on LENGTH and on FORMAT. LENGTH changes the number of characters used to STORE a variable's value. You use LENGTH to control how many characters will be used to STORE the variable. You use FORMAT to control how that value is DISPLAYED -- for example, in PROC PRINT or PROC FREQ or other procedures.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Consider THIS code, which has a LENGTH to read in 2 variables, STRING1 and STRING2, both of which have a length or size of 34 characters:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data big_string;
length string1 $40 string2 $20;
  infile datalines dlm=',';
  input strlen string1 $ string2 $;
 return;
datalines;
34,supercalifragilisticexpealidocious,abcdefghijklmnopqrstuvwxyzabcdefgh
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Notice, also that I do NOT have a FORMAT statement in the code because all I want to do is illustrate how the LENGTH has an impact on the STORED value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Then, these 4 PROC PRINT steps are run, the first one without a FORMAT -- so using the default length of the variable and the next 3 steps each using a different FORMAT:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc print data=big_string
  style(data)={font_face='Courier New' font_size=11pt};
title '1) no format for display. Note that for String 2';
title2 'only 20 characters were read because of LENGTH.';
title3 'For String 1, the LENGTH value was big enough to read value';
var strlen string1 string2;
run;

proc print data=big_string
  style(data)={font_face='Courier New' font_size=11pt};
title '2) format $30. used for both and both are displayed using 30 characters';
title2 'but even with a bigger format, string2 still displays what was read';
var strlen string1 string2;
format string1 string2 $30.;
run;

proc print data=big_string
  style(data)={font_face='Courier New' font_size=11pt};
title '3) format $10. used for both, both values displayed using 10 characters';
var strlen string1 string2;
format string1 string2 $10.;
run;

proc print data=big_string
  style(data)={font_face='Courier New' font_size=11pt};
title '4) format length of $35 large enough for string1';
title2 'but string2 was read as length of 20, so there is nothing more to display';
title3 'and we only see the full 20 characters for string 2';
var strlen string1 string2;
format string1 $35. string2 $35.;
run;
title;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp; I used a STYLE override to change the font to a fixed pitch font so that the letter 'i' took up as much horizontal space as the letter 'w'. Here are the results:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_0-1607994264838.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/52613iE976E715BE1F61D6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_0-1607994264838.png" alt="Cynthia_sas_0-1607994264838.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; You can see in the raw data that the value for STRING2 is 34 characters, you can count them. However, because I had a length of $20 to read STRING2, only the first 20 characters were stored in the data set. I can use a FORMAT to make the stored value display with using fewer characters, but no matter how big I make the format for STRING2, I will only be able to see the 20 characters that were stored.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Hope this helps,&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;</description>
    <pubDate>Tue, 15 Dec 2020 01:07:21 GMT</pubDate>
    <dc:creator>Cynthia_sas</dc:creator>
    <dc:date>2020-12-15T01:07:21Z</dc:date>
    <item>
      <title>length or format of a character variable in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Studio/length-or-format-of-a-character-variable-in-a-data-step/m-p/705559#M9573</link>
      <description>&lt;P&gt;for a character variable, what is the difference between a length statement and a format statement, such as,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Length course $8;&lt;/P&gt;
&lt;P&gt;Format course $8.;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 13 Dec 2020 23:13:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/length-or-format-of-a-character-variable-in-a-data-step/m-p/705559#M9573</guid>
      <dc:creator>anming</dc:creator>
      <dc:date>2020-12-13T23:13:31Z</dc:date>
    </item>
    <item>
      <title>Re: length or format of a character variable in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Studio/length-or-format-of-a-character-variable-in-a-data-step/m-p/705561#M9574</link>
      <description>&lt;P&gt;Length of a character variable indicates how many characters can be in the value of the variable.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;So COURSE can take on eight characters, such as "Syracuse", that's eight letters without the double quotes. COURSE cannot take on the value "Rochester", that's 9 characters and it would be truncated to "Rocheste".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Formats for all variables (numeric and characters) change the appearance of the variable. A format of $8. indicates that the changed appearance of the variable is at most eight characters.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Dec 2020 00:06:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/length-or-format-of-a-character-variable-in-a-data-step/m-p/705561#M9574</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-12-14T00:06:08Z</dc:date>
    </item>
    <item>
      <title>Re: length or format of a character variable in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Studio/length-or-format-of-a-character-variable-in-a-data-step/m-p/705566#M9575</link>
      <description>&lt;P&gt;In most cases, there is no difference.&amp;nbsp; Here are a couple of rare considerations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A FORMAT statement could be applied to an existing variable.&amp;nbsp; If the variable is already defined as being 10 characters long, the FORMAT statement is still legal and would indicate that only the first 8 characters should be printed.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Even more rare, some character values contain leading blanks.&amp;nbsp; Depending on the format used, the leading blanks might print or might not print.&amp;nbsp; The $8. format means the leading blanks will not print.&amp;nbsp; But the $CHAR8. format means that the leading blanks will print.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Procedures do not support a LENGTH statement, so this is a little beyond what you were asking.&amp;nbsp; A FORMAT statement in a procedure might change the outcome.&amp;nbsp; For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=have;
   tables name;
   format name $1.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Normally, PROC FREQ reports each NAME on a separate line.&amp;nbsp; However, the FORMAT statement groups the results.&amp;nbsp; So for each first letter, there will be one line on the report with the total count for all NAMEs that begin with that letter.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Dec 2020 00:54:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/length-or-format-of-a-character-variable-in-a-data-step/m-p/705566#M9575</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-12-14T00:54:14Z</dc:date>
    </item>
    <item>
      <title>Re: length or format of a character variable in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Studio/length-or-format-of-a-character-variable-in-a-data-step/m-p/705891#M9576</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Please go back to the Programming1 course and review the lectures on LENGTH and on FORMAT. LENGTH changes the number of characters used to STORE a variable's value. You use LENGTH to control how many characters will be used to STORE the variable. You use FORMAT to control how that value is DISPLAYED -- for example, in PROC PRINT or PROC FREQ or other procedures.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Consider THIS code, which has a LENGTH to read in 2 variables, STRING1 and STRING2, both of which have a length or size of 34 characters:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data big_string;
length string1 $40 string2 $20;
  infile datalines dlm=',';
  input strlen string1 $ string2 $;
 return;
datalines;
34,supercalifragilisticexpealidocious,abcdefghijklmnopqrstuvwxyzabcdefgh
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Notice, also that I do NOT have a FORMAT statement in the code because all I want to do is illustrate how the LENGTH has an impact on the STORED value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Then, these 4 PROC PRINT steps are run, the first one without a FORMAT -- so using the default length of the variable and the next 3 steps each using a different FORMAT:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc print data=big_string
  style(data)={font_face='Courier New' font_size=11pt};
title '1) no format for display. Note that for String 2';
title2 'only 20 characters were read because of LENGTH.';
title3 'For String 1, the LENGTH value was big enough to read value';
var strlen string1 string2;
run;

proc print data=big_string
  style(data)={font_face='Courier New' font_size=11pt};
title '2) format $30. used for both and both are displayed using 30 characters';
title2 'but even with a bigger format, string2 still displays what was read';
var strlen string1 string2;
format string1 string2 $30.;
run;

proc print data=big_string
  style(data)={font_face='Courier New' font_size=11pt};
title '3) format $10. used for both, both values displayed using 10 characters';
var strlen string1 string2;
format string1 string2 $10.;
run;

proc print data=big_string
  style(data)={font_face='Courier New' font_size=11pt};
title '4) format length of $35 large enough for string1';
title2 'but string2 was read as length of 20, so there is nothing more to display';
title3 'and we only see the full 20 characters for string 2';
var strlen string1 string2;
format string1 $35. string2 $35.;
run;
title;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp; I used a STYLE override to change the font to a fixed pitch font so that the letter 'i' took up as much horizontal space as the letter 'w'. Here are the results:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_0-1607994264838.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/52613iE976E715BE1F61D6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_0-1607994264838.png" alt="Cynthia_sas_0-1607994264838.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; You can see in the raw data that the value for STRING2 is 34 characters, you can count them. However, because I had a length of $20 to read STRING2, only the first 20 characters were stored in the data set. I can use a FORMAT to make the stored value display with using fewer characters, but no matter how big I make the format for STRING2, I will only be able to see the 20 characters that were stored.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Hope this helps,&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;</description>
      <pubDate>Tue, 15 Dec 2020 01:07:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/length-or-format-of-a-character-variable-in-a-data-step/m-p/705891#M9576</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2020-12-15T01:07:21Z</dc:date>
    </item>
  </channel>
</rss>

