FORMAT Procedure

Example 5: Converting Raw Character Data to Numeric Values

Features:

INVALUE statement

Details

This example uses an INVALUE statement to create a numeric informat that converts numeric and character raw data to numeric data.

Program

libname proclib 'SAS-library-1';
libname library 'SAS-library-2';
proc format library=library;
   invalue evaluation 'O'=4
                      'S'=3
                      'E'=2
                      'C'=1
                      'N'=0;
run;
data proclib.points;
   input EmployeeId $ (Q1-Q4) (evaluation.,+1);
   TotalPoints=sum(of q1-q4);
   datalines;
2355 S O O S
5889 2 2 2 2
3878 C E E E
4409 0 1 1 1
3985 3 3 3 2
0740 S E E S
2398 E E C C
5162 C C C E
4421 3 2 2 2
7385 C C C N
;
proc print data=proclib.points noobs;
   title 'The PROCLIB.POINTS Data Set';
run;

Program Description

This program converts quarterly employee evaluation grades, which are alphabetic, into numeric values so that reports can be generated that sum the grades up as points.
Set up two SAS library references, one named PROCLIB and the other named LIBRARY.
libname proclib 'SAS-library-1';
libname library 'SAS-library-2';
Store the Evaluation. informat in the catalog LIBRARY.FORMATS.
proc format library=library;
Create the numeric informat Evaluation. The INVALUE statement converts the specified values. The letters O (Outstanding), S (Superior), E (Excellent), C (Commendable), and N (None) correspond to the numbers 4, 3, 2, 1, and 0, respectively.
   invalue evaluation 'O'=4
                      'S'=3
                      'E'=2
                      'C'=1
                      'N'=0;
run;
Create the PROCLIB.POINTS data set. The instream data, which immediately follows the DATALINES statement, contains a unique identification number (EmployeeId) and bonus evaluations for each employee for each quarter of the year (Q1–Q4). Some of the bonus evaluation values that are listed in the data lines are numbers; others are character values. Where character values are listed in the data lines, the Evaluation. informat converts the value O to 4, the value S to 3, and so on. The raw data values 0 through 4 are read as themselves because they are not referenced in the definition of the informat. Converting the letter values to numbers makes it possible to calculate the total number of bonus points for each employee for the year. TotalPoints is the total number of bonus points.
data proclib.points;
   input EmployeeId $ (Q1-Q4) (evaluation.,+1);
   TotalPoints=sum(of q1-q4);
   datalines;
2355 S O O S
5889 2 2 2 2
3878 C E E E
4409 0 1 1 1
3985 3 3 3 2
0740 S E E S
2398 E E C C
5162 C C C E
4421 3 2 2 2
7385 C C C N
;
Print the PROCLIB.POINTS data set. The NOOBS option suppresses the printing of observation numbers.
proc print data=proclib.points noobs;
Specify the title.
   title 'The PROCLIB.POINTS Data Set';
run;

Output

The PROCLIB.POINT Data Set
The PROCLIB.POINT Data Set