- Author: admin
- Filed under: Auto-cars
- Date: Mar 12,2008
First, you must create the label formats with proc format using a value statement. Next, you attach the label format to the variable with a format statement. This format statement can be used in either proc or data steps. An example of the proc format step for creating the value formats, forgnf and $makef follows.
PROC FORMAT; VALUE forgnf 0=”domestic” 1=”foreign” ; VALUE $makef “AMC” =”American Motors” “Buick” =”Buick (GM)” “Cad.” =”Cadillac (GM)” “Chev.” =”Chevrolet (GM)” “Datsun” =”Datsun (Nissan)”;RUN;
You may include any number of value statements to create label formats as needed. Since make is a variable that contains character values, when you define the formats for it you have to precede the format name with a $ so the format name becomes $makef. Additionally, for character variables the values of the variables must be enclosed in quotes.
Now that the formats forgnf and $makef have been created, they must be linked to the variables, foreign and make. This is accomplished by including a format statement in either a proc or a data step. In the program below the format statement is used in a proc freq.
PROC FREQ DATA=auto2; FORMAT foreign forgnf. make $makef.; TABLES foreign make;RUN;
Notice that the formats forgnf. and $makef. are each followed by a period in the format statement. This is the way that SAS tells the difference between the name of a format and the name of a variable in a format statement.
The output of the frequencies procedure for foreign displays the newly defined labels instead of the values of the variable.
Where Car Was Made Cumulative Cumulative FOREIGN Frequency Percent Frequency Percent——————————————————domestic 19 73.1 19 73.1foreign 7 26.9 26 100.0
The output of the frequencies procedure for make displays the newly defined labels instead of the values of the variable. Values for which formats haven’t been defined (Audi and BMW) appear in the table without modification.
MAKE Frequency Percent Frequency Percent————————————————————-American Motors 3 11.5 3 11.5Audi 2 7.7 5 19.2BMW 1 3.8 6 23.1Buick (GM) 7 26.9 13 50.0Cadillac (GM) 3 11.5 16 61.5Chevrolet (GM) 6 23.1 22 84.6Datsun (Nissan) 4 15.4 26 100.0
If you link formats to variables in a data step where a permanent file is created, then every time you use that file SAS expects to find the formats. Thus you will have to supply the proc format code in each program that uses the file. Since this can make each of your programs much longer than you might like, I would like to provide a tip for accomplishing this task without repeating the code for the proc format in every program. Assuming that a small program containingonly the proc format is stored in a file called fmats.sas in a directory on your C: drive called myfiles, the following statement will bring that code into your current program:
%INCLUDE ‘C:\myfiles\fmats.sas’;
This should save time and make maintenance of your programs easier. The remainder of your program would follow this statement.
One Response for "Labeling values is a two step process"
None…
None…
Leave a comment
You must be logged in to post a comment.