You are on page 1of 54

Business Analytics

ST521 – Base SAS I

Copyright © 2010, SAS Institute Inc. All rights reserved.


C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

Chapter 20: Creating and Maintaining Permanent


Formats

20.1 Creating Permanent Formats

2
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

Objectives
 Create formats from SAS data sets.
 Create permanent formats.
 Access permanent formats.
 Maintain formats.

3
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

Business Scenario
Management has requested that country names, instead
of country codes, be used in reports.

Country Population Country_ID

AU 20,000,000 160
CA . 260
DE 80,000,000 394
IL 5,000,000 475

Country Population Country_ID

Australia 20,000,000 160


Canada . 260
Germany 80,000,000 394
Israel 5,000,000 475

4
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

Creating a Format (Review)


Use PROC FORMAT to create the format.

proc format;
value $country 'AU' = 'Australia'
'CA' = 'Canada'
'DE' = 'Germany'
'IL' = 'Israel'
'TR' = 'Turkey'
'US' = 'United States'
'ZA' = 'South Africa';
run;

5
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

Using a Control Data Set to Create a Format


Instead of entering all of the values in PROC FORMAT,
you can create a format from a SAS data set that contains
the code and value information.
Partial Listing of
orion.country
Country Country_Name

AU Australia Control data set


CA Canada Partial Listing of
country
DE Germany
Start Label FmtName
IL Israel PROC FORMAT $country
AU Australia $country
TR Turkey
CA Canada $country

DE Germany $country

IL Israel $country

TR Turkey $country

6
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

Using a Control Data Set to Create a Format


Partial orion.country
Country_ Country_
Country Population Country_ID Continent_ID
Name FormerName
AU Australia 20,000,000 160 96
CA Canada . 260 91
East/West
DE Germany 80,000,000 394 93
Germany
IL Israel 5,000,000 475 95

DATA Step
Start
Start Label
Label FmtName
FmtName

data country;
keep Start Label FmtName;
retain FmtName '$country';
set orion.country(rename=(Country=Start
Country_Name=Label));
run;

7
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

Using a Control Data Set to Create a Format


Use the CNTLIN= option to read the data and create
the format.

proc format cntlin=country;


run;
CNTLIN=SAS-data-set
CNTLIN=SAS-data-set

The variables FmtName, Start, and Label are


required in order to create a format from a CNTLIN
data set.

8
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

Setup for the Poll


The following DATA steps both create a SAS data set
named country.

data country;
keep Start Label FmtName; Program 1
retain FmtName '$country';
set orion.country(rename=(Country=Start
Country_Name=Label));
run;

data country;
keep Start Label FmtName; Program 2
FmtName='$country';
set orion.country;
Start=Country;
Label=Country_Name;
run;

9
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

20.01 Multiple Choice Poll


Which program should be more efficient?

a. Program 1
b. Program 2
c. They should be equally efficient.

10
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

20.01 Multiple Choice Poll – Correct Answer


Which program should be more efficient?

a. Program 1
b. Program 2
c. They should be equally efficient.

 The RETAIN statement assigns an initial value at


compilation time.
 The RENAME= option renames the variables in the
new data set at compilation time.

11
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

Where Formats Are Stored


By default, formats are stored in the work.formats
catalog and exist only for the duration of the SAS session.

work.formats

PROC
PROCFORMAT;
FORMAT;

12
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

Using the LIBRARY= Option


Use the LIBRARY= option to control where the format is
stored.

proc format library=orion.MyFmts


cntlin=country;
run;
LIBRARY=libref.catalog
LIBRARY=libref.catalog

13
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

Using the LIBRARY= Option


If the LIBRARY= option specifies only a libref, formats
are stored permanently in a catalog named formats,
referenced by libref.formats.

proc format library=orion;


PROC
PROCFORMAT
FORMATLIBRARY=libref;
LIBRARY=libref;

orion.formats

14
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

Using the LIBRARY= Option


If the LIBRARY= option specifies libref.catalog, formats
are stored permanently in that catalog.

proc format library=orion.MyFmts;


PROC
PROCFORMAT
FORMATLIBRARY=libref.catalog;
LIBRARY=libref.catalog;

orion.MyFmts

 Store frequently used formats in permanent catalogs.

15
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

Viewing Formats
You can use the SAS Explorer window to view formats
stored in a catalog.

16
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

CATALOG Procedure
The CATALOG procedure manages entries in
SAS catalogs.

proc catalog cat=orion.MyFmts;


contents;
quit;
PROC
PROCCATALOG
CATALOGCATALOG=ilbref.catalog;
CATALOG=ilbref.catalog;
CONTENTS;
CONTENTS;
QUIT;
QUIT;
Contents of Catalog ORION.MYFMTS
Output
# Name Type Create Date Modified Date
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
1 DATES FORMAT 29Jan08:16:26:39 29Jan08:16:26:39
2 COUNTRY FORMATC 29Jan08:16:33:30 29Jan08:16:33:30
3 COUNTRY_NAME FORMATC 20Apr09:15:30:14 20Apr09:15:30:14
4 EXTRA FORMATC 20Apr09:15:30:14 20Apr09:15:30:14

p210d01
17
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

Documenting Formats
You can use the FMTLIB option in the PROC FORMAT
statement to document the format.
proc format library=orion.MyFmts fmtlib;
select $country;
run;

„ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ †
‚ FORMAT NAME: $COUNTRY LENGTH: 13 NUMBER OF VALUES: 7 ‚
‚ MIN LENGTH: 1 MAX LENGTH: 40 DEFAULT LENGTH 13 FUZZ: 0 ‚
‡ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ…ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ…ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ‰
‚START ‚END ‚LABEL (VER. V7|V8 05MAY2009:12:34:42)‚
‡ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ‰
‚AU ‚AU ‚Australia ‚
‚CA ‚CA ‚Canada ‚
‚DE ‚DE ‚Germany ‚
‚IL ‚IL ‚Israel ‚
‚TR ‚TR ‚Turkey ‚
‚US ‚US ‚United States ‚
‚ZA ‚ZA ‚South Africa ‚
Šƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ‹ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ‹ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒŒ

p210d01
18
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

Nesting Formats
In the VALUE statement, you can specify that the format
use a second format as the formatted value.
Enclose the format name in square brackets:

proc format library=orion.MyFmts;


value $extra ' '='Unknown'
other=[$country30.];
run;
value=[existing-format]
value=[existing-format]

p210d01
19
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

Business Scenario
Use the permanent format $country to specify country
names instead of country codes in the reports.
Country Population Country_ID

AU 20,000,000 160
CA . 260
DE 80,000,000 394
IL 5,000,000 475 orion.MyFmts

Country Population Country_ID

Australia 20,000,000 160


Canada . 260
Germany 80,000,000 394
Israel 5,000,000 475
20
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

Using Formats
By default, when a format is referenced, SAS does the
following:
 searches formats supplied by SAS
 searches work.formats
 writes an error to the log if the format is not found

1 formats supplied by SAS

2 work.formats

21
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

20.02 Short Answer Poll


Submit the program p210a01.
What error messages do you see in the SAS log?

data customers;
set orion.customer;
Country_Name=put(Country,$country.);
run;

proc freq data=orion.employee_addresses;


tables Country;
format Country $extra.;
run;

22
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

20.02 Short Answer Poll – Correct Answer


Submit the program p210a01.
What error messages do you see in the SAS log?
477 data customers;
478 set orion.customer;
479 Country_Name=put(Country,$country.);
---------
48
ERROR 48-59: The format $COUNTRY was not found or could not be loaded.

480 run;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.CUSTOMERS may be incomplete. When this step was stopped there were
0 observations and 13 variables.
WARNING: Data set WORK.CUSTOMERS was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

481
482 proc freq data=orion.employee_addresses;
483 tables Country;
484 format Country $extra.;
ERROR: The format $EXTRA was not found or could not be loaded.
485 run;

NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE FREQ used (Total process time):
real time 0.10 seconds
cpu time 0.00 seconds
23
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

Using the NOFMTERR System Option


The default for the FMTERR | NOFMTERR system option
depends on your SAS environment.

OPTIONS
OPTIONSFMTERR
FMTERR| |NOFMTERR;
NOFMTERR;

OPTION DESCRIPTION DEFAULT


FMTERR Specifies that when SAS cannot find a SAS
specified variable format, it generates Windowing
an error message.
Replaces missing formats with the w. or Enterprise Guide
NOFMTERR $w. default format, issues a note, and SAS Studio
continues.

24
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

20.03 Short Answer Poll


Replace the current OPTIONS statement with the
following statement and resubmit p210a01. What is the
result?

options nofmterr;

25
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

20.03 Short Answer Poll – Correct Answer


Replace the current OPTIONS statement with the
following statement and resubmit p210a01. What is the
result?

options nofmterr;

All of the procedure steps were executed with no


warnings or errors in the SAS log. The user-defined
formats were not applied.

26
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

Using the FMTSEARCH= System Option


To use permanent formats or to search multiple catalogs,
use the FMTSEARCH= system option to identify the
catalog(s) to be searched for the format(s).

options fmtsearch=(orion orion.MyFmts);


OPTIONS
OPTIONSFMTSEARCH=(item-1
FMTSEARCH=(item-1item-2…item-n);
item-2…item-n);

27
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

Using the FMTSEARCH= System Option


options fmtsearch=(orion orion.MyFmts);

1 formats supplied by SAS

2 work.formats

3 library.formats

4 orion.formats

5 orion.MyFmts
28
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

20.04 Short Answer Poll


Replace the current OPTIONS statement with the
following statement and resubmit p210a01. What is the
result?

options fmterr fmtsearch=(orion orion.MyFmts);

29
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

20.04 Short Answer Poll – Correct Answer


Replace the current OPTIONS statement with the
following statement and resubmit p210a01. What is the
result?

options fmterr fmtsearch=(orion orion.MyFmts);

All of the procedure steps were executed with no


warnings or errors in the SAS log. The user-defined
formats were applied.

30
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

Maintaining Formats
To maintain formats, perform one of the following tasks:
 Edit the PROC FORMAT code that created the original
format.
 Create a SAS data set from the format, edit the data
set, and use the CNTLIN= option to re-create the
format.

31
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

Maintaining Permanent Formats


Step 1 Create a data set.
proc format library=libref.catalog
cntlout=SAS-data-set;
select format-name;
Permanent run;
Formats
Catalog

Step 2 Edit the


SAS Data Set
values.
Step 3 Re-create the format.
proc format library=libref.catalog
cntlin=SAS-data-set;
run;
32
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

33
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

1. By default, user-defined formats are stored in a


temporary catalog named work.formats and are
deleted at the end of your SAS session.
 True
 False

34
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

1. By default, user-defined formats are stored in a


temporary catalog named work.formats and are
deleted at the end of your SAS session.
 True
 False

35
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

2. Which PROC FORMAT statement option is used to


create a permanent format?
a. CNTLIN=
b. FMTLIB=
c. LIBRARY=
d. CNTLOUT=

36
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

2. Which PROC FORMAT statement option is used to


create a permanent format?
a. CNTLIN=
b. FMTLIB=
c. LIBRARY=
d. CNTLOUT=

The LIBRARY= option enables you to specify a


SAS library where the formats that you are
creating in the PROC FORMAT step are stored.

37
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

3. The variables FmtName, Start, and Label are


required in order to create a format from a CNTLIN
data set.
 True
 False

38
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

3. The variables FmtName, Start, and Label are


required in order to create a format from a CNTLIN
data set.
 True
 False

39
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

4. What is the search order that is used to locate the


permanent format given the following OPTIONS
statement?

options fmtsearch=(orion.MyFmts);

a. orion.Myfmts only
b. work.formats  orion.MyFmts  library.formats 
formats supplied by SAS
c. formats supplied by SAS  work.formats 
library.formats  orion.MyFmts
d. orion.MyFmts  Work.formats  library.formats 
formats supplied by SAS

40
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

4. What is the search order that is used to locate the


permanent format given the following OPTIONS
statement?

options fmtsearch=(orion.MyFmts);

a. orion.Myfmts only
b. work.formats  orion.MyFmts  library.formats 
formats supplied by SAS
c. formats supplied by SAS  work.formats 
library.formats  orion.MyFmts
d. orion.MyFmts  Work.formats  library.formats 
formats supplied by SAS

41
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

5. Which statement best describes the purpose of the


output control data set?
a. The output control data set enables you to output one
value for one observation that contains the
information about the format.
b. The output control data set enables you to output
formats without writing VALUE statement in the
PROC FORMAT step.
c. The output control data set contains information that
describes formats. It is the data set that is created
with the CNTLIN= option in the PROC FORMAT
statement.
d. The output control data set contains information that
describes formats. It is the data set that is created
with the CNTLOUT= option in the PROC FORMAT
statement.
42
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

5. Which statement best describes the purpose of the


output control data set?
a. The output control data set enables you to output one
value for one observation that contains the
information about the format.
b. The output control data set enables you to output
formats without writing VALUE statement in the
PROC FORMAT step.
c. The output control data set contains information that
describes formats. It is the data set that is created
with the CNTLIN= option in the PROC FORMAT
statement.
d. The output control data set contains information that
describes formats. It is the data set that is created
with the CNTLOUT= option in the PROC FORMAT
statement.
43
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

6. By default, the NOFMTERR system option is in effect.


If you use a format that SAS cannot load, SAS issues
an error message and stops processing the step.
 True
 False

44
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

6. By default, the NOFMTERR system option is in effect.


If you use a format that SAS cannot load, SAS issues
an error message and stops processing the step.
 True
 False

By default, the FTMTERR system option is in


effect. If you want to avoid error messages and
continue processing the step when SAS cannot
load a format, use the NOFMTERR system option.

45
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

7. Which statement is true concerning the FMTLIB option


in PROC FORMAT?
a. The FMTLIB option in PROC FORMAT prints
information about all permanent formats in your
SAS session.
b. The FMTLIB option in PROC FORMAT prints
information about all of the formats in the
work.formats catalog.
c. The FMTLIB option in PROC FORMAT prints
information about all of the formats in the
LIBRARY.FORMATS option.
d. The FMTLIB option in PROC FORMAT prints
information about all formats in the catalog that is
specified in the LIBRARY= option.

46
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

7. Which statement is true concerning the FMTLIB option


in PROC FORMAT?
a. The FMTLIB option in PROC FORMAT prints
information about all permanent formats in your
SAS session.
b. The FMTLIB option in PROC FORMAT prints
information about all of the formats in the
work.formats catalog.
c. The FMTLIB option in PROC FORMAT prints
information about all of the formats in the
LIBRARY.FORMATS option.
d. The FMTLIB option in PROC FORMAT prints
information about all formats in the catalog that is
specified in the LIBRARY= option.

47
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

8. Which procedure enables management of a


SAS catalog?
a. PROC COPY
b. PROC CATMOD
c. PROC CATALOG
d. PROC CONTENTS

48
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

8. Which procedure enables management of a


SAS catalog?
a. PROC COPY
b. PROC CATMOD
c. PROC CATALOG
d. PROC CONTENTS

49
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

9. In which location can you not use a format?


a. PUT statement
b. TITLE statement
c. FORMAT= option
d. FORMAT statement

50
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

9. In which location can you not use a format?


a. PUT statement
b. TITLE statement
c. FORMAT= option
d. FORMAT statement

51
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

10. In the VALUE statement in PROC FORMAT, you


cannot nest formats by specifying the format that you
are creating. Use a second format as the formatted
value.
 True
 False

52
C o py r i g h t © 2 0 1 4, S A S In s ti t u te I n c . A l l r i g ht s r e s e r v e d .

10. In the VALUE statement in PROC FORMAT, you


cannot nest formats by specifying the format that you
are creating. Use a second format as the formatted
value.
 True
 False

Format nesting is allowed. Simply enclose the


nested format name in square brackets:

proc format library=orion.MyFmts;


value $extra ' '='Unknown'
other=[$country30.];
run;

53
Business Analytics
ST521 – Base SAS I

Copyright © 2010, SAS Institute Inc. All rights reserved.

You might also like