BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
anming
Pyrite | Level 9

for a character variable, what is the difference between a length statement and a format statement, such as,

 

Length course $8;

Format course $8.;

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

In most cases, there is no difference.  Here are a couple of rare considerations.

 

A FORMAT statement could be applied to an existing variable.  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.  

 

Even more rare, some character values contain leading blanks.  Depending on the format used, the leading blanks might print or might not print.  The $8. format means the leading blanks will not print.  But the $CHAR8. format means that the leading blanks will print.

 

Procedures do not support a LENGTH statement, so this is a little beyond what you were asking.  A FORMAT statement in a procedure might change the outcome.  For example:

 

proc freq data=have;
   tables name;
   format name $1.;
run;

Normally, PROC FREQ reports each NAME on a separate line.  However, the FORMAT statement groups the results.  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.

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

Length of a character variable indicates how many characters can be in the value of the variable.


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".

 

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.

--
Paige Miller
Astounding
PROC Star

In most cases, there is no difference.  Here are a couple of rare considerations.

 

A FORMAT statement could be applied to an existing variable.  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.  

 

Even more rare, some character values contain leading blanks.  Depending on the format used, the leading blanks might print or might not print.  The $8. format means the leading blanks will not print.  But the $CHAR8. format means that the leading blanks will print.

 

Procedures do not support a LENGTH statement, so this is a little beyond what you were asking.  A FORMAT statement in a procedure might change the outcome.  For example:

 

proc freq data=have;
   tables name;
   format name $1.;
run;

Normally, PROC FREQ reports each NAME on a separate line.  However, the FORMAT statement groups the results.  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.

Cynthia_sas
SAS Super FREQ

Hi:

  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.

 

  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:

data big_string;
length string1 $40 string2 $20;
  infile datalines dlm=',';
  input strlen string1 $ string2 $;
 return;
datalines;
34,supercalifragilisticexpealidocious,abcdefghijklmnopqrstuvwxyzabcdefgh
;
run;

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.

 

  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:


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;

  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:

Cynthia_sas_0-1607994264838.png

 

  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.

 

  Hope this helps,

Cynthia

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 10226 views
  • 5 likes
  • 4 in conversation