PROC FORMAT
The FORMAT procedure enables you to define your own informats and formats for variables.
First thing to learn about SAS DATA step is that it process the data row-wise. What I mean with that will become more apparent with each example, so without further ado, let's begin working on our examples.
With PROC FORMAT, you can do the following:
- • Print numeric values as character values (for ex, print 1 as MALE and 2 as FEMALE).
- • Print one character string as a different character string (for ex, print YES as OUI).
- • Print numeric values using a template (for ex, print 9458763450 as 945-876-3450).
Let's see an example (you can get the data from here).
PROC PRINT DATA = tutorial.format1;
RUN;
Obs | fname | lname | gender | married |
---|---|---|---|---|
1 | Alicia | Silverstone | F | 1 |
2 | Tom | Cruise | M | 0 |
3 | Uma | Thurman | F | 1 |
Setting up data like above is one thing but displaying it another. If we want to display the data in a more understandable way, we can use PROC FORMAT:
PROC FORMAT;
VALUE $ftgender 'F'='Female' 'M'='Male';
VALUE ftmarried 1='yes' 0='no';
RUN;
Notice how we use the VALUE statement. It is followed by name of the format; if the value that is to be converted (e.g. F and M) is a string, the name has to be preceded by a dollar sign. If it's numeric (e.g. 0 and 1) then no need for a dollar sign. To use the format we just created we use FORMAT statement in any of the PROCs. For example:
PROC PRINT DATA = tutorial.format1;
FORMAT gender $ftgender.;
FORMAT married ftmarried.;
RUN;
Obs | fname | lname | gender | married |
---|---|---|---|---|
1 | Alicia | Silverstone | Female | yes |
2 | Tom | Cruise | Male | no |
3 | Uma | Thurman | Female | yes |
Note the period we put at the end of format name; this tells SAS that this is a format. Don't forget it or you will get an error.