The formats are used in the SAS (statistical analysis system) to determine how variable values to be displayed or written as output. There are plenty of built-in formats available in SAS such as date9., w10., comma10.2, MMDDYYw., etc.
It is also allowed to create your own user-defined formats in SAS. You can set your own rules and data mapping patterns while creating user-defined formats.
Typically there are two types of formats, character formats, and numeric formats.
- Character formats are always created on character variables or char values
- Numeric formats are always created on numeric variables or numeric values
Temporary vs Permanent SAS Data Sets
Before you start learning permanent formats in SAS, let me make you familiar with the temporary and permanent data sets. The same logic is applied when creating temporary and permanent sas formats.
The temporary data set in sas also known as WORK data set as the data gets stored in the WORK library. This data set is only available within the session.
The following code creates a temporary data set called RESULT (or WORK.RESULT).
/* temporary data set */
data result;
... ;
run;
Similarly, the permanent data set can be created but by specifying the permanent library reference. The library reference should point to a physical location accessible in the SAS environment.
The following code creates a permanent data set called libref.RESULT and data gets stored in the physical location which is referenced as “libref” through SAS library statement.
/* library statement*/
libname libref "/<physical_location>/";
/* permanent data set */
data libref.result;
... ;
run;
Temporary SAS Formats
SAS formats created using PROC FORMAT procedure. By default the proc format procedure uses library=WORK option hence all the formats created gets stored in the WORK library catalog unless and until you specify permanent library reference.
/* temporary sas format */
proc format;
value $<_format_name_>
...;
run;
/*--- OR ----*/
proc format library=libref;
value $<_format_name_>
...;
run;
Permanent SAS Formats
The permanent SAS formats are created using proc format procedure with library=libref option. The library=libref option tells SAS to store format data values in the specified SAS library catalog.
You can verify this by visiting the physical location of a SAS library to see the catalog file. The permanent formats are not bound to the current or any specific sas session. It’s available throughout the SAS environment at any given time.
The only thing you need to do is to tell SAS where to look for the formats using the FMTSEARCH option in SAS.
OPTIONS FMTSEARCH=(libref-1 libref-2 ..);
Now again you should have specified SAS libname statement in order to use libref. So when you want to use SAS formats, add below two lines.
/*tell sas where to look for formats*/
libname libref "/<physical_location>/";
options fmtsearch=(libref);
It looks like a tedious process, right?
We do have a solution for this one. Consider adding all the SAS library statements and format options in the config file (autoexec.exe).
When you initialise your SAS session, all the required things get set automatically. It works fine with the client applications such as BASE SAS, SAS EG, DI Studio, etc. It also works with the batch jobs.
Examples can be added in autoexec.exe file:
libname ST00 '/home/u61950255/Production/Staging Tables';
libname PL00 '/home/u61950255/Production/Primary Tables';
options fmtsearch=(ST00 PL00);
Create Permanent Formats in SAS
Here you’ll create two permanent formats using proc format procedure. One will be built up on character values/variable and one will be on numeric values/variable.
SAS User-defined Formats on Char Values/Variable
In this example let’s assume you have Years data in the character variable and the values you have are; 1980, 1994, 2002, 1997, 2014, 2030, and so on.
You want to create permanent user-defined formats to display it as in decades (text). The following mapping data values can be used:
'1980'-'1989' ⇾ "80's decade"
'1990'-'1999' ⇾ "90's decade"
'2000'-'2009' ⇾ "2000's decade"
'2010'-'2019' ⇾ "2010's decade"
'2020'-'2099' ⇾ "2020's or more"
The following code creates the permanent SAS format called “YEAR_FMT” using the above data mapping criteria.
/* libname statement */
libname ST00 '/home/u61950255/Production/Staging Tables';
proc format library=ST00;
/* char format: User defined format on character values/variable */
value $YEAR_FMT
'1980'-'1989'="80's decade"
'1990'-'1999'="90's decade"
'2000'-'2009'="2000's decade"
'2010'-'2019'="2010's decade"
'2020'-'2099'="2020's or more";
run;
Curious to know more?
If you’re curious to know what’s happening in the background then go to the physical library location and look for formats.sas file which is created or updated if it was already there.
SAS User-defined Formats on Numeric Values/Variable
Now it’s time to play with the numeric data values. Formats can be created on numeric variable/values using the same method described above.
Everything is the same except the little difference when it is being used. You’ll get to know the exact difference further in the article.
In this example let’s assume you have grades in numeric values such as 70, 83, 58, 96, etc. Now you want to create a format that displays Grades in the form of A, A+, A++, etc.
Here is the criteria you can use to map the data.
00-49 ⇾ "FAIL"
50-69 ⇾ "B"
70-79 ⇾ "A"
80-89 ⇾ "A+"
90-99 ⇾ "A++"
100 ⇾ "Genius"
The following code creates a permanent format called GRADE_FMT which is built up on numeric grades.
/* libname statement */
libname ST00 '/home/u61950255/Production/Staging Tables';
proc format library=ST00;
/* Numeric format: User defined format on numeric variable */
value GRADE_FMT
00-49="FAIL"
50-69="B"
70-79="A"
80-89="A+"
90-99="A++"
100="Genius";
run;
This time also you can verify if formats.sas file is updated or not. Go to the physical location of that SAS library and look for this file.
Get SAS Format Details
You can create as many formats as you require by using methods mentioned above but how to see the details about the formats created under any library?
It’s fairly easy to get the SAS format details using the proc format with FMTLIB option. You just mention libref with FMTLIB option and it prints all the details about formats available under that specific library catalogues.
By now you have created two formats, YEAR_FMT and GRADE_FMT, under ST00 library. The following code prints all the details about formats present under ST00 library:
/* view SAS format details */
proc format library=ST00 fmtlib;
run;
Export SAS Permanent Format Catalogues
There is a possibility to export entire SAS format catalogues into a SAS work data set or permanent data set. It can be achieved through the proc format procedure with cntlout= option.
The following code exports the entire SAS format catalogues into a SAS WORK data set called work.all_st00_formats.
/* export entire format catalogues */
proc format library=ST00 cntlout=work.all_st00_formats;
run;
proc print;
How To Use Permanent Format In SAS ?
Now the question is how to use permanent SAS formats which you have already created above. As stated earlier, formats created for character and numeric values are used in slightly different ways.
The formats created on char values/variable should be referred to as $<FormatName>.(dot) (dollar sign followed by format name)
for example: $YEAR_FMT
Whereas formats created on numeric values or variable must be referred to as <FormatName>.(dot) (format name ends with dot without dollar sign)
for example: GRADE_FMT.
EXAMPLE:
The WORK.RESULT data set has the following attributes: Name (char), Gender(char), Status (char), Year(char), Section(char), Score(num), FinalGrade(num).
/* create a new sas data set */
data work.result;
input Name $ 1-8 Gender $ 11 Status $13 Year $ 15-18 Section $ 20 Score 22-23
FinalGrade 25-26;
datalines;
Mark Z. F 2 1987 A 90 97
Elon Musk M 1 1998 A 92 97
Bill G. M 2 1993 B 81 71
Jeff B. M 1 1997 A 80 72
Warren B. M 1 1998 B 89 80
Kim K F 1 1981 B 78 73
Neil A. F 2 1988 A 82 91
Blekk J. F 1 2001 B 84 80
Harty F 1 1999 B 84 84
Bard M 1 2011 A 92 97
Siri F 2 1998 B 89 91
Bunny M 2 2009 A 89 89
;
run;
proc print;
Now you’ve SAS data set and formats ready. If you look at the above data set, YEAR_FMT can be applied on YEAR attribute to display years in the form of decades text.
And GRADE_FMT can be applied on FinalGrade attribute to display grades in other formats like A, A++, etc.
In the following code you apply formats on YEAR and FinalGrade attributes to print the data set.
/* tell SAS where to look for formats */
options fmtsearch=(ST00);
/* apply formats on year and finalgrade */
proc print data=work.result;
format year $YEAR_FMT.;
format FinalGrade GRADE_FMT.;
run;
The above example demonstrates how you can use SAS formats to display data. The following example shows how you can use format to create a new data set with the formatted values.
The following code creates a new SAS data set called “work.formatted_result” from the existing data set work.result but with the two new variables.
- The data on formattedGrade, a new variable, is populated from the existing variable FinalGrade with the user-defined permanent format GRADE_FMT. using the put() function.
- The data on formattedYear, a new variable, is populated from the existing variable YEAR with the user-defined permanent format $YEAR. using the put() function.
/* tell SAS where to look for formats */
options fmtsearch=(ST00);
data formatted_result;
set result;
/* define two new variables */
length formattedGrade $8 formattedYear $50;
/* assign values with formats */
formattedGrade=put(FinalGrade, GRADE_FMT.);
formattedYear=put(Year, $YEAR_FMT.);
run;
/* view data */
proc print data=formatted_result;
title "Data set: work.formatted_result";
run;
FAQ
Formats determine how variable values are displayed or written as output. With formats, you can do the following:
- Print numeric values as character values (for example, print 1 as
A
and 2 asB
). - Print one character string as a different character string (for example, print
YES
asOK
). - Print numeric values using a template (for example, print 9876543210 as
987-654-3210
).
The SAS format are created using library reference libname=libref , hence the data stored in the format catalog file formats.sas located on physical directory of the specified SAS library.
The formats are assigned using FORMAT keyword followed by variable name and then specify actual format name.
For example: Year is the attribute name. YEAR_FMT is the user-defined format.
format year $YEAR_FMT.;