Lesson 3: variable formats

If informats instruct SAS how to read in variables, think of formats as instructions on how to display them. To tell SAS how to display a variable, use the FORMAT statement, followed by the variable name, and finally the format. There’s many different formats available, but one of the most commonly used ones is the $ sign for character variables. The general syntax for it is a dollar sign followed by the number of characters you wish to display, and a period to finish it off.
Given the code below, what would the output look like?

DATA myTable;
INPUT Names $ Age;
FORMAT Names $2.;
DATALINES;
Jess 23
;
RUN;

a) . 23
b) Jess 23
c) Je 23

Answer: c
Remember, the format statement only tells SAS how to display the values. In this case, we used the format “$2.” so SAS only returned the first 2 characters, but Jess’ full name, with all 4 characters, is actually what’s saved in the table. Try re-running the code with the format “$4.” to see what it does.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s