SAS Tutorial — Session 5

Contents
Introduction
Session 1
Session 2
Session 3
Session 4
Session 5
Session 6
Odds -n- Ends
 
 

Objectives

  1. Add value formats to a variable (PROC FORMAT)

  2. Create a new variable using the IF/THEN ; ELSE statement

Attaching formats to variable values

    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:

    PROC FORMAT;
    VALUE SEXFMT 1='Male' 2='Female';
    PROC FREQ;
    TABLES SEX;
    FORMAT SEX SEXFMT.;

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:

    PROC FORMAT;
    Additional procedure information statements;

Example:

    PROC FORMAT;
    VALUE SEXFMT 1='Male' 2 ='Female';
    VALUE $YNFMT 'Y'='Yes' 'N'='No';

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

IF/THEN; ELSE; statement

    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:

    IF expression THEN statement; <ELSE statement>;

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

Session 5 Exercises

Exercise 5.1

  1. Edit (Pico) your SAS program file, survey.sas.

  2. Based on the following example, add a PROC FORMAT statement before the first line of your program. Type the following:

      PROC FORMAT;
      VALUE SEXFMT 1 ='Male' 2 ='Female':

  3. 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.)

  4. Add a FORMAT statement to the PROC FREQ that will format the variable SEX with SEXFMT by typing:

      PROC FREQ;
      TABLES SEX;
      FORMAT SEX SEXFMT.;

  5. Add a PROC PRINT with a FORMAT statement that will format the variable SEX with SEXFMT by typing:

      PROC PRINT;
      VAR SEX;
      FORMAT SEX SEXFMT.;

    Note: Don't forget the period after SEXFMT.

  6. Be sure to SAVE your work.

  7. Your SAS program should now look like this:

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

  1. 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:

      IF AGE LT 20 THEN AGEGROUP=1;
      ELSE IF AGE GE 20 THEN AGEGROUP=2;

  2. Change the TABLES statement of your PROC FREQ to request a crosstabulation of the variable Q1 and the new variable, AGEGROUP by typing

      PROC FREQ;
      TABLES Q1*AGEGROUP;

  3. Delete the PROC PRINT statements.

  4. Be sure to save your work.

  5. At this point your program should look like this:

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

© University of New Mexico -- last updated -- comments to: docs@unm.edu