In SAS, how do I read and print long string variables properly?
SAS can handle a string variable up to 32,767 characters long. If you
try to read a long string using the $ (dollar
sign) qualifier in the INPUT statement, SAS will automatically
truncate the variable to the first eight characters.
Suppose you have an open-ended question with a maximum length of
10,000 characters and want to read it into a string variable of a SAS
data set. Raw data are saved in a comma-delimited ASCII text file
survey_result.csv. Use the INFORMAT
statement and tell SAS to read the string up to 10,000
characters. Then add the $ qualifier after
the variable name in the INPUT statement. Following is an example:
Above, the DATA step builds the SAS data set survey08 by
opening survey_result.csv and reading a numeric variable
id, a string variable name 20 characters
long, two numeric variables q1 and q2, a
string variable q2_open in a format specified by the
INFORMAT statement, and three numeric variables q3,
q4, and q5 consecutively. Note that
LRECL=32767 tells SAS to read a line up to 32,767
characters, and FIRSTOBS=2 indicates that data begin at
the second line (the first data line is ignored).
When you try to print this long string variable, PROC PRINT by default
truncates it to the first 98 characters. An easy solution is to use an
ODS HTML statement and print the output in HTML. See the
following example:
Note: NOOBS suppresses observation numbers in the
output, and the WHERE statement selects observations whose values of
q2_open are not null (missing).
For more about statistical and mathematical software, email the UITS Stat/Math Center, visit the center's web page, or phone 812-855-4724 (IUB) or 317-278-4740 (IUPUI). The center is located in Bloomington at 410 N. Park Avenue, and is open for consultation by appointment Monday-Friday 9am-5pm.
Last modified on October 06, 2009.







