Indiana University
University Information Technology Services
  
What are archived documents?
Login>>
Login

Login is for authorized groups (e.g., UITS, OVPIT, and TCC) that need access to specialized Knowledge Base documents. Otherwise, simply use the Knowledge Base without logging in.

Close

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:

DATA survey08; INFILE 'survey_result.csv' delimiter = ',' LRECL=32767 FIRSTOBS=2 ; INFORMAT name $20. ; INFORMAT q2_open $10000. ; INPUT id name $ q1 q2 q2_open $ q3 q4 q5; RUN;

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:

ODS HTML; PROC PRINT NOOBS; VAR q2_open; WHERE q2_open NE ""; RUN; ODS HTML CLOSE;

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.

This is document axeu in domain all.
Last modified on October 06, 2009.

Comments/Questions/Corrections

Use this form to offer suggestions, corrections, and additions to the Knowledge Base. We welcome your input!

If you are affiliated with Indiana University and would like assistance with a specific computing problem, please use the Ask a Consultant form, or contact your campus Support Center.

Contact Information

Note: We will reply to your comment at this address. If your message concerns a problem receiving email, please enter an alternate email address.