SAS formats have the form xxx.yyy, where xxx is the name and sometimes length of the format, and yyy is the number of digits after a decimal. If there are no digits after a decimal, the format name/length ends with the period. For example, the SAS format for dollar amounts in the millions might be DOLLAR12.2, which would allow 12 characters (including the decimal), with the last two being after a decimal point. ROMAN10. would output values in 10 'digits' as Roman Numerals. SSN11. outputs social security numbers, such as 123-45-6789.
SAS also allows the use of formats for character variables.
These formats are similar to numeric formats, except that the
format names always begin with the dollar sign ($).
SAS Language allows users to assign their own formats to values in their data. Usually, this involves users asking SAS to replace numbers in their data with some kind of labels. The most common example is the user request that SAS take '1's and '2's in a variable called SEX and format those values as the words 'female' and 'male' when they are displayed in output.
original data output using user-defined format
----------------------------------------------------
1 female
2 male
2 male
1 female
2 male
Both SAS formats and user-defined formats can be assigned to variables in PROCs (which makes them apply only for the output of that PROC) or in the SAS Data Set (which causes the format to be used every time the value is used). This is true of both SAS formats and user-defined formats. The use of SAS formats is automatic, as all the internal SAS formats are kept in a library that is always available in every SAS job or session. The use of user-defined formats is a little more tricky, as the formats first must be created, then they must be made available to the SAS job or session that uses them. Here is a brief scenario:
Temporary user-defined formats (that will exist for the life of the job or session only) are created with a simple PROC FORMAT, as in the following example.
proc format;
value sexfmt 1='female' 2='male';
This creates a user-defined format called SEXFMT. (note that a period is placed at the end of the format name in all situations except the original VALUE statement where it was created). The use of 'FMT' is not required. This format could have been called ABCXYZ. if desired.
The format then can be used in a DATA step
data readin; input id gender;
format gender sexfmt.;
cards;
001 2
002 2
003 1
004 2
005 1
;
The format also could be used instead in a PROC step
proc print data=readin;
format gender sexfmt.;
1 male
2 male
3 female
4 male
5 female
proc format;
value $gender 'F'='female' 'M'='male';
This creates a user-defined format called $GENDER. (note that a period is placed at the end of the format name in all situations except the original VALUE statement where it was created). The format then can be used in a DATA step
data chartest; input id gendchar $;
format gendchar $gender.;
cards;
001 M
002 M
003 F
004 M
005 F
;
proc print data=readin;
format gendchar $gender.;
SUGGESTION: Assign user-defined formats in SAS data steps only when you are familiar with the use of permanent format libraries, and you intend to have yours available in every session or job. Otherwise, assign formats in PROC steps only when necessary.
To store formats in a permanent library, include the LIBRARY= option in the PROC FORMAT statement. As mentioned above, this requires a prior LIBNAME statement, as shown in this example.
libname library '~/sasfmts'; /* example for UNIX */
libname library 'C:\sasfmts'; /* example for DOS/Win */
libname library 'Hard_Disk:SASformats'; /* example for Mac */
proc format library=library;
value sexfmt 1='female'
2='male';SAS creates (or updates) a file in ~/sasfmts (or C:\sasfmts, or Hard_Disk:SASfmts:) called formats.sct01, which is the formats catalog. This file should not be deleted or edited.
Then, in a future job in which the formats are to be used, include the LIBNAME statement again. You may then refer to the user-defined formats that are in your library without further ado.
libname library '~/sasfmts'; /* example for UNIX */
libname library 'C:\sasfmts'; /* example for DOS/Win */
libname library 'Hard_Disk:SASfmts'; /* example for Mac */
proc print data=readin;
format gender sexfmt.;
SAS will find the format in the format library, and the output will contain the formatted values for GENDER, in place of the original numeric values.