Objectives
- Add value formats to a variable (PROC FORMAT)
- Create a new variable using the IF/THEN ; ELSE statement
Use: |
To create value labels that can later be attached to variables |
Syntax: |
PROC FORMAT;
VALUE format_name I ='Label'
2='Label';
|
Result: |
A new format is created |
Example:
The PROC FORMAT statement, with its required operands, creates value
labels that may later be attached to specific variables by using a FORMAT
statement in either the DATA step (permanent) or in the PROC step (temporary).
Where should you place the FORMAT procedure? The only rule is that the
format must be created before you try and use it. So if you are using
it in a DATA step, the PROC FORMAT must come before that DATA step. If
you are using the FORMAT statement as part of your PROC step, then the
PROC FORMAT must come before that. In order to avoid confusion and clutter,
it is recommended that you place your PROC FORMAT at the beginning of
your program, before the entire DATA step. The general form of the PROC
FORMAT statement is:
Example:
Notice that formats for character variables are preceded by a $
(dollar sign), whereas formats for numeric variables are not. Format names
can not exceed eight characters.
ToC
Use: |
To transform data or create new variables |
Syntax: |
IF expression THEN statement;
<ELSE statement>
|
Result: |
A new variable is created |
The IF / THEN statement can be used to transform data or create new
variables based upon criteria that are true or not true in your data.
The ELSE statement is optional. The general format of the lF/THEN; ELSE
statement is:
Example:
IF AGE < 20 THEN AGEGROUP = 1;
ELSE IF AGE > = 20 AND AGE < 30 THEN AGEGROUP=2;
ELSE AGEGROUP=3;
IF LANG='Spanish' or LANG='French' then
NEWLANG='NotEngl';
ELSE NEWLANG='English';
Note: < means less than, > = means
greater than or equal to, etc. LT and GE could also be used.
See the **Version 6 SAS Language Reference** for a list of all
valid operands.
ToC
Exercise 5.1
- Edit (Pico) your SAS program file, survey.sas.
- Based on the following example, add a PROC FORMAT statement before
the first line of your program. Type the following:
- Delete the lines containing the PROC UNIVARIATE, PROC MEANS and PROC
PLOT commands. Leave the PROC FREQ lines, as you will need them for
this exercise. (See example program that follows.)
- Add a FORMAT statement to the PROC FREQ that will format the variable
SEX with SEXFMT by typing:
- Add a PROC PRINT with a FORMAT statement that will format the variable
SEX with SEXFMT by typing:
Note: Don't forget the period after SEXFMT.
- Be sure to SAVE your work.
- Your SAS program should now look like this:
- Submit your SAS program file (survey.sas) at the Linux ($)
prompt. After it has finished (you will see the $ prompt again), check your output found in the survey.log and survey.lst
files.
Exercise 5.2
-
Edit (Pico) your SAS program, survey.sas. Add the IF / THEN
statements necessary to create a new variable AGEGROUP, which groups
the AGE variable into two categories, those younger than 20 years old
and those 20 years or older by typing:
- Change the TABLES statement of your PROC FREQ to request a crosstabulation
of the variable Q1 and the new variable, AGEGROUP by typing
- Delete the PROC PRINT statements.
- Be sure to save your work.
- At this point your program should look like this:
- Now, submit your SAS program file at the $ prompt.
When it has finished (and the $ prompt returns) check
the .log and .lst files for errors and warnings and for
the output from the PROC FREQ.
ToC
|