How to Create [Permanent] User Defined Formats in SAS

You have already learned how to create user defined formats in SAS using the proc format procedure. This article is an extended version of that comprehensive guide.

In this article you’ll learn how to create user defined formats using the proc format procedure in the permanent SAS library.

SAS formats created in the WORK library are temporary. It is only valid for that particular session.

But you can create SAS formats once and use it anywhere and whenever you want by creating formats and storing it in a permanent library.

Create Permanent SAS Formats

First, you need a permanent SAS library to store the formats in the library catalog. Let’s create a sas library that points to a permanent physical location.

To create a new SAS library with SAS code, you use the LIBNAME statement. The LIBNAME statement associates the name of the library, or libref, with the physical location of the library.

The following LIBNAME statement assigns the ST00 libref to your mentioned folder. The directory that you associate with the libref must already exist before you can assign it to the libref.

/* libname statement */
libname ST00 '/home/u61950255/Production/Staging Tables';

Now create two formats, one is for YEAR and one is for GRADE.

The YEAR format is defined with the following condition:

  • ‘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”

And GRADE format is defined with the following condition:

  • 70-79= “A”
  • 80-89= “A+”
  • 90-99= “A++”
  • 100= “Genius”;


IMPORTANT NOTE:
The YEAR format is defined on character values hence it can be applied on character columns whereas the GRADE format is defined on numeric values hence it can be applied on numeric columns only.

/* create permanent SAS formats and store it in the library catalog  */

/* libname statement */
libname ST00 '/home/u61950255/Production/Staging Tables';


proc format library=ST00;
	/* char format: User defined format on character variable */
	value $YEAR 
		'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";
		
	/* Numeric format: User defined format on numeric variable */
	value GRADE 
		70-79="A" 
		80-89="A+" 
		90-99="A++" 
		100="Genius";
run;
create permanent SAS formats

View Formats:

Now that you have created two formats and placed them in a permanent sas library. You can view them from the SAS library catalog using fmtlib with a proc format statement.

/* Viewing Formats in a Catalog */

proc format library = ST00 fmtlib;
run;
Viewing SAS Formats in a Catalog

Export Formats Data

The output from the above can be stored in a new data set. Basically you can export all the data from the SAS library catalog using cntlout. You can store output data in work or permanent SAS library.

/* export format catalog */

proc format library = ST00 cntlout = work.all_st00_formats;
run; 

proc print;
title 'export format catalog data: work.all_st00_formats';
export format catalog data

How to Use Permanent SAS Formats

Since you have already created two permanent user defined SAS formats, you can use it anywhere and wherever you want.

To demonstrate that you need to create a sample dataset and apply sas formats on it.

/* create a new sas dataset */
data work.grade;
	input Name $ 1-8 Gender $ 11 Status $13 Year $ 15-18 Section $ 20 Score 22-23 
		FinalGrade 25-26;
	datalines;
Dennison  M 1 1997 A 80 72
Edgar     F 1 1998 B 89 80
Faust     M 1 1981 B 78 73
Greeley   F 2 1988 A 82 91
Chris     F 2 2009 A 82 91
Harty     F 1 1999 B 84 84
Dan       M 2 1997 A 88 97
Bard      M 1 2011 A 92 97
Sandy     F 2 1998 B 89 91
Karlsen   F 1 1997 A 85 82
Bunny     M 2 2009 A 89 89
Josh      M 1 2022 A 91 93
Frode     F 2 2030 B 92 98
;
run;
proc print;
user defined formats in sas example

Now you can use those formats here independently. In the below example we tried to create two new columns with the data where we have applied YEAR and GRADE formats.

/* create a new dataset with two new columns  */
options fmtsearch = (ST00);
data fmt_Grade;
	set Grade;

	/* define two new variables */
	length fmtGrade $8 fmtYear $50;

	/* assign values with formats */
	fmtGrade=put(FinalGrade, Grade.);
	fmtYear=put(Year, $YEAR.);
run;

/* view data */
proc print data=fmt_grade;
run;
create user defined permanent formats in sas example

The SAS formats are really useful in the programming and data warehousing. It can save you from applying unnecessary joins on big datasets. The formats are kind of available everywhere and easily applied to extract and apply to map the values.

You can check this comprehensive guide on SAS PROC FORMAT where you’ll learn more details with examples about sas formats.

It would be worth checking out SAS Picture format. It can make your life even easier especially when you’re dealing with mobile number, currency, big amount, or sas dates.

FAQ

Is it possible to create permanent SAS formats?

Yes. In SAS you can create user-defined formats and store it in the permanent SAS library catalog. You can do so in the proc format procedure by mentioning in the lib= option.

 

Example:

/* libname statement */
libname ST00 '/home/u61950255/Production/Staging Tables';

proc format library=ST00;
/* User defined format on character variable */
value $YEAR 

'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;
What is difference between temporary and permanent SAS formats?

The formats are created using the proc format procedure in SAS.

 

The temporary SAS formats are created and stored it in the WORK library catalog. It is only available for the current session.

 

Whereas, the permanent SAS formats are created and stored in the permanent SAS library catalog. It is always available and can be used anytime, anywhere.