3. For Your Information iii
Table of Contents
Chapter 1 Formatting Basics ................................................................................ 1-1
1.1 Creating and Using Formats ............................................................................................ 1-3
Chapter 2 Format Utilities ..................................................................................... 2-1
2.1 Introduction ...................................................................................................................... 2-3
2.2 Creating Permanent Formats ............................................................................................ 2-4
Demonstration: Creating Permanent Fonts ............................................................... 2-8
2.3 Using SAS Data Sets to Create Formats ........................................................................ 2-10
Demonstration: Creating Formats from Data Sets .................................................. 2-13
2.4 The PICTURE and INVALUE Statements .................................................................... 2-15
Demonstration: Using the PICTURE Statement ..................................................... 2-18
Demonstration: Using the INVALUE Statement .................................................... 2-23
Chapter 3 Format Uses .......................................................................................... 3-1
3.1 Introduction ...................................................................................................................... 3-3
3.2 Grouping Data.................................................................................................................. 3-4
Demonstration: Grouping Data ................................................................................. 3-6
3.3 Table Lookups................................................................................................................ 3-10
Demonstration: Table Lookups ............................................................................... 3-13
3.4 Formats for Display ....................................................................................................... 3-15
Demonstration: Using Formats for Display ............................................................ 3-20
Chapter 4 New in SAS® 9.3 .................................................................................... 4-1
4.1 Introduction ...................................................................................................................... 4-3
4.2 Viewing Formats and Using Functions ............................................................................ 4-4
4. iv For Your Information
Demonstration: New Features in SAS 9.3 ................................................................ 4-6
5. For Your Information v
To learn more…
For information about other courses in the curriculum, contact the SAS
Education Division at 1-800-333-7660, or send e-mail to training@sas.com.
You can also find this information on the Web at support.sas.com/training/
as well as in the Training Course Catalog.
For a list of other SAS books that relate to the topics covered in this
Course Notes, USA customers can contact our SAS Publishing Department
at 1-800-727-3228 or send e-mail to sasbook@sas.com. Customers outside
the USA, please contact your local SAS office.
Also, see the Publications Catalog on the Web at support.sas.com/pubs for
a complete list of books and a convenient order form.
9. 1.1 Creating and Using Formats 1-3
1.1 Creating and Using Formats
Objectives
Use the FORMAT statement.
Define the FORMAT procedure syntax.
4
4
Displaying Data Values
You can enhance reports by using SAS formats to format
data values.
SAS Format Report
Data
Set
Values in the SAS data set are not changed.
5
5
10. 1-4 Chapter 1 Formatting Basics
Formatting Data Values
To apply a format to a specific SAS variable, use the
FORMAT statement. General form of the FORMAT
statement:
FORMAT variable(s) format;
Example:
proc print data=ia.empdata;
format Salary dollar11.2;
run;
6
6
Creating User-Defined Formats
General form of a PROC FORMAT step:
PROC FORMAT;
VALUE format-name range1='label'
range2='label'
…;
RUN;
7
7
To create your own formats, use the FORMAT procedure.
In addition to the VALUE statement, you can define formats for numbers using the PICTURE statement
and informats using the INVALUE statement.
11. Chapter 2 Format Utilities
2.1 Introduction..................................................................................................................... 2-3
2.2 Creating Permanent Formats ........................................................................................ 2-4
Demonstration: Creating Permanent Fonts ........................................................................... 2-8
2.3 Using SAS Data Sets to Create Formats .................................................................... 2-10
Demonstration: Creating Formats from Data Sets ............................................................... 2-13
2.4 The PICTURE and INVALUE Statements .................................................................... 2-15
Demonstration: Using the PICTURE Statement .................................................................. 2-18
Demonstration: Using the INVALUE Statement ................................................................... 2-23
13. 2.1 Introduction 2-3
2.1 Introduction
Objectives
Create permanent formats.
Use data sets to create formats.
Define the PICTURE and INVALUE statements.
3
3
14. 2-4 Chapter 2 Format Utilities
2.2 Creating Permanent Formats
General Form of a PROC FORMAT Step
To create a permanent format, add the LIBRARY= option
to the PROC FORMAT statement:
PROC FORMAT LIBRARY=libref.catalog;
…
5
5
Without the LIBRARY= option, formats are stored in the work.formats catalog and exist for the duration
of the SAS session.
If the LIBRARY= option specifies only libref, formats are stored permanently in libref.formats.
If the LIBRARY= option specifies libref.catalog, formats are stored permanently in that catalog.
15. 2.2 Creating Permanent Formats 2-5
How Are Formats Stored?
Formats are stored as SAS catalogs entries.
A single SAS catalog can contain several catalog entries.
SAS Catalog ia.formats
revfmt.format dest.formatc routes.format
6
6
SAS catalogs are special SAS files that store many different types of information in smaller units named
entries.
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).
General form of the FMTSEARCH= system option:
OPTIONS FMTSEARCH=(item-1 item-2…item-n);
7
7
16. 2-6 Chapter 2 Format Utilities
Using the FMTSEARCH System Option
options fmtsearch=(ia ia.formats2);
SAS Supplied Formats
work.formats
ia.formats
ia. formats2
8
8 ...
Using the NOFMTERR Option
By default, the FMTERR system option is in effect. If you
use a format that SAS cannot load, SAS issues an error
message and stops processing the step.
To prevent the default action, change the system option
FMTERR to NOFMTERR.
OPTIONS FMTERR | NOFMTERR;
9
9
17. 2.2 Creating Permanent Formats 2-7
The CATALOG Procedure
The CATALOG procedure manages entries in
SAS catalogs.
Some functions of PROC CATALOG
create a listing of the contents of a catalog
copy a catalog or selected entries within a catalog
rename or delete entries within a catalog
modify the description of a catalog entry.
10
1
0
Documenting Formats
You can use the FMTLIB option in the PROC FORMAT
statement to document the format.
General form of the FMTLIB option:
PROC FORMAT LIBRARY=libref.catalog
FMTLIB;
<other statements>;
RUN;
11
1
1
18. 2-8 Chapter 2 Format Utilities
Creating Permanent Fonts
/* *************************************************** */
/* Program: f2d1.sas */
/* *************************************************** */
/* This program demonstrates how to create permanent */
/* formats and how to manage them. */
/* *************************************************** */
libname ia '.';
** Create formats in the ia.formats catalog.;
proc format library=ia;
value $emplev
'PRES' = 'Level A'
'VICEPR' = 'Level B'
'BAGCLK', 'CHKCLK', 'FACCLK', 'SALCLK', 'FACMNT',
'FINACT', 'FLSCHD', 'RECEPT', 'TELOP' = 'Level 1'
'FINCLK', 'FSVCLK', 'HRCLK', 'ITCLK', 'MKTCLK',
'RESCLK' = 'Level 2'
'BAGSUP', 'CHKSUP', 'ITPROG' = 'Level 3'
'GRCSUP', 'ITSUPT' = 'Level 4'
'FLTAT1'-'FLTAT3', 'GRCREW', 'MECH01'-'MECH03',
'PILOT1'-'PILOT3' = 'Level 5'
'FACMGR', 'FINMGR', 'FLSMGR', 'FSVMGR', 'HRMGR',
'ITMGR', 'MKTMGR', 'OFFMGR', 'RESMGR',
'SALMGR' = 'Level 6';
value $dest
'AKL','AMS','ARN','ATH','BKK','BRU','CBR','CCU',
'CDG','CPH','CPT','DEL','DXB','FBU','FCO',
'FRA','GLA','GVA','HEL','HKG','HND','JED',
'JNB','JRS','LHR','LIS','MAD','NBO','PEK',
'PRG','SIN','SYD','VIE','WLG' = 'International'
'ANC','BHM','BNA','BOS','DFW','HNL','IAD','IND',
'JFK','LAX','MCI','MIA','MSY','ORD','PWM',
'RDU','SEA','SFO' = 'Domestic'
other='*Unknown Airport*';
run;
** Create a format in the ia.fmt catalog.;
proc format library=ia.fmt;
value money
low - 30000 = 'Low'
30000 <- 55000 = 'Average'
55000 <- high = 'High';
run;
(Continued on the next page.)
19. 2.2 Creating Permanent Formats 2-9
** Use options to ensure the formats are found.;
options fmtsearch=(ia ia.fmt);
options nofmterr;
** Use proc catalog to manage format catalogs.;
filename fmt 'formats.txt';
proc catalog catalog=ia.fmt et=format;
contents file=fmt;
run;
copy out = ia.formats;
select money;
run;
delete money;
run;
quit;
** Use the fmtlib option to document formats.;
proc format library=ia fmtlib;
run;
Partial Output
20. 2-10 Chapter 2 Format Utilities
2.3 Using SAS Data Sets to Create Formats
Using a Control Data Set to Create a Format
You can create a format from a SAS data set that
contains value information (called a control data set).
Use the CNTLIN= option to read the data and create the
format.
General form of CNTLIN= option:
PROC FORMAT LIBRARY=libref.catalog
CNTLIN=SAS-data-set;
RUN;
14
1
4
21. 2.3 Using SAS Data Sets to Create Formats 2-11
Using a Control Data Set to Create a Format
To use an input data set to create a format, the data set
must contain the variables FMTNAME, START, and
LABEL.
15
1
5
Example:
data aports;
keep Start Label Fmtname;
retain fmtname '$airport';
set ia.cities (rename = (Code = Start City = Label));
run;
FMTNAME Character variable whose value is the format or informat name
START Character variable that gives the range's starting value
LABEL Character variable whose value is the informatted or formatted value or the name
of a standard SAS informat or format
Other variables are possible.
22. 2-12 Chapter 2 Format Utilities
Maintaining Permanent Formats
To maintain formats, you might need to output the formats
to a data set.
General form of PROC FORMAT with the CNTLOUT=
option:
PROC FORMAT LIBRARY=libref.catalog
CNTLOUT=SAS-data-set;
<other statements>;
RUN;
16
1
6
To maintain a format, you do the following:
• Use CNTLOUT= to create a data set
• Add, update, or delete observations in the data set
• Use CNTLIN= to re-create the format
23. 2.3 Using SAS Data Sets to Create Formats 2-13
Creating Formats from Data Sets
/* *************************************************** */
/* Program: f2d2.sas */
/* *************************************************** */
/* Create and maintain a format based on a data set. */
/* *************************************************** */
** Determine the structure of the original data set.;
title 'IA.CITIES';
proc print data=ia.cities;
run;
proc contents data=ia.cities;
run;
** Build the control data set.;
data citfmt(keep=start label fmtname);
set ia.cities(rename=(code=start city=label));
retain fmtname '$city';
run;
** Use the control data set to create the new format.;
proc format cntlin=citfmt;
run;
** Use the format as we would any other.;
proc print data=ia.delay;
where date eq '01mar1995'd;
var orig dest delay;
format orig dest $city.;
title 'Using the new format for ORIG and DEST';
run;
** Convert the format into a control data set.;
proc format cntlout=citfmt;
select $city;
run;
(Continued on the next page.)
24. 2-14 Chapter 2 Format Utilities
** Update the control data set with the new cities;
proc sql;
insert into citfmt
set fmtname='$city',start='LGA',end='LGA',label='New York, NY';
insert into citfmt
set fmtname='$city',start='YYZ',end='YYZ',label='Toronto';
insert into citfmt
set fmtname='$city',start='DCA',end='DCA',label='Washington, DC';
insert into citfmt
set fmtname='$city',start='PAR',end='PAR',label='Paris';
insert into citfmt
set fmtname='$city',start='LON',end='LON',label='London';
quit;
** Re-create the format with the new values;
proc format cntlin=citfmt;
run;
** Rerun the report.;
proc print data=ia.delay;
where date eq '01mar1995'd;
var orig dest delay;
format orig dest $city.;
title 'Using the updated format for ORIG and DEST';
run;
Partial Output
25. 2.4 The PICTURE and INVALUE Statements 2-15
2.4 The PICTURE and INVALUE Statements
The PICTURE Statement
The PICTURE statement in PROC FORMAT defines a
template for data values.
PROC FORMAT;
PICTURE name
value-range-set-1 <(picture-1-option(s))>;
RUN;
19
1
9
The PICTURE Statement
Options available with the PICTURE statement include
the following:
NOEDIT Specify that numbers are message
characters rather than digit selectors.
PREFIX= Specify a character prefix for the
formatted value.
FILL= Specify a character that completes
the formatted value.
MULT= Specify a number to multiply the variable's
value by before it is formatted.
20
2
0
26. 2-16 Chapter 2 Format Utilities
The DATATYPE Picture Option
The DATATYPE picture option specifies that this picture
is to apply to a SAS date, SAS time, or SAS datetime
value.
PROC FORMAT;
PICTURE name
value-range-set-1
(DATATYPE=DATE|TIME|DATETIME);
RUN;
21
2
1
27. 2.4 The PICTURE and INVALUE Statements 2-17
Directives on a PICTURE Statement
LOW and HIGH are keywords that signify the lowest
data value and the highest data value.
The % followed by a letter indicates a directive.
proc format;
picture myfmt
low-high = '%0d-%b-%Y'
(datatype = date);
run;
22
2
2
The 0 in the directive indicates whether the day of the month is one digit. Precede that digit with a zero.
Directives include the following:
%a Locale's abbreviated weekday name
%A Locale's full weekday name
%b Locale's abbreviated month name
%B Locale's full month name
%d Day of the month as a decimal number (1-31), with no leading zero
%m Month as a decimal number (1-12), with no leading zero
%Y Year with century as a decimal number
28. 2-18 Chapter 2 Format Utilities
Using the PICTURE Statement
/* *************************************************** */
/* Program: f2d3.sas */
/* *************************************************** */
/* This program demonstrates how to use the PICTURE */
/* statement in PROC FORMAT. */
/* *************************************************** */
** Use mult= option to convert data.;
proc format;
picture feet other='000000009' (mult=5280);
run;
data feet;
input miles @@;
format miles feet.;
cards;
1 1.5 2
;
run;
proc print data=feet;
title 'Converting Miles to Feet';
run;
** Use the mult and prefix options.;
proc format;
picture curr low-high='000,000' (mult=.001 prefix='$');
run;
proc print data=ia.crew(obs=20) label;
format salary curr.;
label salary='Salary in Thousands';
title 'Picture Format Multiplier';
run;
** Use the fill and prefix options.;
proc format;
picture salary low-high='00,000,000.00' (fill='*' prefix='$');
run;
proc print data=ia.crew(obs=20);
format salary salary.;
title 'Picture Format Fill Character';
run;
(Continued on the next page.)
29. 2.4 The PICTURE and INVALUE Statements 2-19
** Using placeholders and the noedit option.;
proc format;
picture delGrp
low-< 0 = 'Early Arrival'
0 = 'No Delay'
1 = '1 Minute Delay'
1 <- 10 = '09 Minutes Delay'
10<-high= ' Delay Greater than 10 Minutes' (noedit);
picture delGx
low-< 0 = 'Early Arrival'
0 = 'No Delay'
1 = '1 Minute Delay'
1 <- 10 = '99 Minutes Delay'
10<-high= ' Delay Greater than 10 Minutes' (noedit);
picture delGy
low-< 0 = 'Early Arrival'
0 = 'No Delay'
1 = '1 Minute Delay'
1 <- 10 = '09 Minutes Delay'
10<-high= ' Delay Greater than 10 Minutes';
run;
proc report data=ia.delay(obs=20) nowd;
column delay delay=del delay=dx;
define delay / display format=delgrp. 'NOEDIT with 09';
define del / format=delgx. 'NOEDIT with 99';
define dx / format=delgy. '09 without NOEDIT';
title 'Variations of Picture Formats';
run;
** Create a custom date format.;
proc format;
picture hirefmt low-high = '%0d-%b-%Y ' (datatype=date);
run;
proc print data=ia.crew(obs=20);
var firstname lastname hiredate;
format hiredate hirefmt.;
title 'Crew Hire Dates with Custom Date Format';
run;
** Use date directives with other text.;
proc format;
picture mrg
low-<'01apr1990'd = 'Pre-Merger'
'01apr1990'd-'31mar1993'd = '%B during Merger Process ' (datatype=date)
other = 'Post-Merger';
run;
proc print data=ia.crew(obs=20);
var firstname lastname hiredate;
format hiredate mrg.;
title 'Crew Hire Dates with Text/Date Format';
run;
30. 2-20 Chapter 2 Format Utilities
Partial Output
31. 2.4 The PICTURE and INVALUE Statements 2-21
PROC FORMAT INVALUE Statement
PROC FORMAT LIBRARY=libref.catalog;
INVALUE $charinfmt 'value1' = 'informatted-value-1'
'value2' = 'informatted-value-2'
'valuen' = 'informatted-value-n';
INVALUE numinfmt 'value1' = informatted-value-1
'value2' = informatted-value-2
'valuen' = informatted-value-n;
RUN;
24
2
4
Special Keywords
_SAME_ indicates that a value in the domain is to be
mapped into the same value in the range.
_ERROR_ indicates that a value or set of values
should be excluded from the domain.
25
2
5
32. 2-22 Chapter 2 Format Utilities
UPCASE Option
The UPCASE option automatically uppercases all
input values before they are compared to the informat
domain.
This option is used in an INVALUE statement when
creating an informat.
26
2
6
33. 2.4 The PICTURE and INVALUE Statements 2-23
Using the INVALUE Statement
/* *************************************************** */
/* Program: f2d4.sas */
/* *************************************************** */
/* This program demonstrates how to use the INVALUE */
/* statement in PROC FORMAT. */
/* *************************************************** */
proc format;
invalue eval
'Excellent'=4
'Good'=3
'Fair'=2
'Poor'=1
;
run;
data evals;
input EmployeeId $ @6 Evaluation eval. @6 String $9.;
datalines;
2355 Good
5889 2
3878 Excellent
4409 Poor
0740 Fair
2398 Excellent
4421 3
7385 Good
;
run;
title 'Data Read using a Custom Informat';
proc print data=evals;
run;
proc means data=evals;
run;
** Use _same_ and _error_ keywords;
proc format;
invalue tryit
1-10 = _same_
other = _error_
;
run;
(Continued on the next page.)
34. 2-24 Chapter 2 Format Utilities
data same;
infile datalines;
input @1 Original @1 Stored tryit.;
datalines;
5
15
;
run;
proc print data=same;
title 'Data Read using _SAME_ and _ERROR_';
run;
** Use the upcase option;
proc format;
invalue $Gender (upcase)
'M' = 'Male'
'F' = 'Female'
;
invalue $Genx
'M' = 'Male'
'F' = 'Female'
;
run;
data gendtest;
input @1 Original $
@1 WithUpcase: $gender.
@1 NoUpcase : $genx. ;
cards;
M
m
f
F
;
run;
proc print data=gendtest;
title 'Data Read using UPCASE option';
run;
39. 3.1 Introduction 3-3
3.1 Introduction
Objectives
Use formats to group data.
Use formats to look up data.
Use formats to display data.
3
3
40. 3-4 Chapter 3 Format Uses
3.2 Grouping Data
Grouping Data in Procedures
Procedures that group data can use the formatted value
to create reports:
PROC FREQ
PROC MEANS
PROC GCHART
5
5
Grouping Data in the DATA Step
You can use the GROUPFORMAT option in the DATA
step with some cases of BY-group processing to use the
formatted values, not the stored values.
General form of the GROUPFORMAT option:
BY GROUPFORMAT variable-name <NOTSORTED>;
6
6
41. 3.2 Grouping Data 3-5
Tips for Using the GROUPFORMAT Option
The GROUPFORMAT option
is available only in the DATA step
is useful when you define formats for grouped data
enables the DATA step to process the same groups
of data as a summary procedure or with PROC
REPORT.
7
7
Advantages of GROUPFORMAT
The GROUPFORMAT option
can be used to create ordered/grouped reports without
sorting the data
frequently eliminates the need for another step.
8
8
42. 3-6 Chapter 3 Format Uses
Grouping Data
/* *************************************************** */
/* Program: f3d1.sas */
/* *************************************************** */
/* This program demonstrates how to use formats to */
/* group data. */
/* *************************************************** */
/* Run proc freq twice: without a grouping format and */
/* with a grouping format. */;
proc freq data=ia.crew;
tables hiredate;
title 'FREQ without format';
run;
proc freq data=ia.crew;
tables hiredate;
format hiredate year4.;
title 'FREQ with format';
run;
/* Create a user-defined grouping format. */
proc format;
value $emp
'FLTAT1'-'FLTAT3'='Flight Attendant'
'PILOT1'-'PILOT3'='Pilot';
run;
/* Run proc means twice: without the grouping format and */
/* with the grouping format. */;
proc means data=ia.crew;
class jobcode;
var salary;
title 'MEANS without format';
run;
proc means data=ia.crew;
class jobcode;
format jobcode $emp.;
var salary;
title 'MEANS with format';
run;
(Continued on the next page.)
43. 3.2 Grouping Data 3-7
/* Run PROC TABULATE twice: without grouping formats and */
/* with grouping formats. */;
proc tabulate data=ia.crew format=9.;
class jobcode hiredate;
tables hiredate, jobcode;
title 'TABULATE Without Formats';
run;
proc tabulate data=ia.crew format=9.;
class jobcode hiredate;
tables hiredate, jobcode;
format jobcode $emp. hiredate year4.;
title 'TABULATE With Formats';
run;
/* Run proc gchart twice: without grouping formats and */
/* with grouping formats and DISCRETE option. */;
goptions reset=pattern;
proc gchart data=ia.crew;
vbar jobcode hiredate;
title 'GCHART without format';
run;
vbar jobcode hiredate / discrete;
format jobcode $emp. hiredate year4.;
title 'GCHART with format and DISCRETE';
run;
quit;
** Sort the data by date.;
proc sort data=ia.crew out=sortedbydate;
by hiredate;
run;
** Create a variable containing year -- extra pass of the data.;
data sortedbyyear;
set sortedbydate;
year=year(hiredate);
run;
data firsthired;
set sortedbyyear;
by year;
if first.year;
run;
proc print data=firsthired;
title 'First Employee Hired in Each Year';
run;
44. 3-8 Chapter 3 Format Uses
** Use the groupformat option to group by year.;
data firsthired;
set sortedbydate;
by groupformat hiredate;
format hiredate year4.;
if first.hiredate;
run;
proc print data=firsthired;
format hiredate date9.;
title 'First Employee Hired in Each Year';
run;
46. 3-10 Chapter 3 Format Uses
3.3 Table Lookups
Lookup Values
You can use formats for table lookups when you want to
lookup one value.
For example, you might have a model number stored with
your data, but you want to include the model type.
11
1
1
Using the FORMAT Procedure
You create the format for the lookup.
proc format library = ia;
value model
1001 = 'JetCruise LF5000'
1002 = 'JetCruise LF5100'
1003 = 'JetCruise LF5200'
...
1010 = 'JetCruise MF6000'
1011 = 'JetCruise SF1000'
1012 = 'JetCruise SF3000'
1013 = 'JetCruise SF7000';
run;
1
12 continued...
2
47. 3.3 Table Lookups 3-11
Using the FORMAT Procedure
Use the format in a PUT function to “look up” the values
that you need.
options fmtsearch = (ia);
data models;
set ia.aircraft;
ModelType = put(ModelNum,Model.);
run;
13
1
3
Using the FORMAT Procedure
Use the format in a FORMAT statement to “look up” the
values that you need:
options fmtsearch=(ia);
proc print data=ia.aircraft;
format ModelNum Model.;
run;
14
1
4
48. 3-12 Chapter 3 Format Uses
Using the FORMAT Procedure
Use PROC FORMAT in the following situations:
There is a low to a medium number of values.
The lookup values are not stable.
The values are used in many programs.
15
1
5
49. 3.3 Table Lookups 3-13
Table Lookups
/* *************************************************** */
/* Program: f3d2.sas */
/* *************************************************** */
/* Use a format to look up values. */
/* *************************************************** */
** Define a format to look up or validate data. ;
proc format;
value Model
1002='JetCruise LF8100'
1003='JetCruise LF5100'
1004='JetCruise LF5200'
1010='JetCruise MF2100'
1011='JetCruise MF4000'
1022='JetCruise SF1000'
other='**invalid**';
run;
** Use a format to look up values in a data step.;
data Models;
set ia.aircraft;
Model=put(Mnum,model.);
run;
proc print data=Models label;
title 'All Models';
run;
** Use a format to look up values in a proc step.;
proc print data=ia.aircraft label;
format Mnum model.;
where put(Mnum,model.) like '%MF%';
title 'MF Models';
run;
proc print data=ia.aircraft label;
where put(Mnum,model.) contains 'invalid';
title 'Invalid Models';
run;
51. 3.4 Formats for Display 3-15
3.4 Formats for Display
Formats for Display
Formats are most obviously used for display, but various
options can increase their usability.
FUZZ, NOTSORTED, MULTILABEL
Nesting formats
Used for colors or trafficlighting
18
1
8
Format Options
Options can be used in PROC FORMAT to modify the
format’s structure.
FUZZ
NOTSORTED
MULTILABEL
19
1
9
52. 3-16 Chapter 3 Format Uses
Format Options - Fuzzy Ranges
FUZZ option
Use it to map a value if it is “close enough.”
The default fuzz value is 1E(-12).
20
2
0
Format Options - NOTSORTED
NOTSORTED Option
This option stores the format in the order in the
VALUE statement.
Use the option to do the following:
– Save processing time
– Preserve the order in which you define ranges
Use the option with the PRELOADFMT option and
ORDER=DATA to maintain the order in certain
procedures.
21
2
1
Use NOTSORTED when the following conditions exist:
• You know the likelihood of certain ranges occurring, and you want your informat or format to search
those ranges first to save processing time.
• You want to preserve the order in which you define ranges when you print a description of the informat
or format using the FMTLIB option.
• You want to preserve the order in which you define ranges when you use the ORDER=DATA option
and the PRELOADFMT option to analyze class variables in PROC MEANS, PROC SUMMARY, or
PROC TABULATE.
53. 3.4 Formats for Display 3-17
Creating Overlapping Ranges
To create overlapping ranges, use the MULTILABEL
option in the VALUE statement in PROC FORMAT:
VALUE <$>fmtname <(MULTILABEL)> ...
Create a report with any procedure that supports the MLF
option.
PROC TABULATE
PROC MEANS
PROC SUMMARY
22
2
2
Nested Formats
Create a format that maps value ranges to existing
formats. Use this format in the following situations:
A modular approach is needed for ease of
maintenance.
One format is not desirable across the entire
range.
23
2
3
54. 3-18 Chapter 3 Format Uses
Using Formats to Apply Colors
You can use the FORMAT procedure to apply different
colors to each of the data values. After you create a
format, use the STYLE= option to apply that format to the
foreground or the background of the column.
PROC FORMAT;
VALUE $fmtname 'data-value-1'='color-1'
'data-value-2'='color-2'
...
'data-value-n'='color-n';
24
2
4
Trafficlighting with PROC TABULATE
You can use trafficlighting to highlight specific information
about a report.
To add cell trafficlighting, perform the following steps:
Create a user-defined format.
On the column you want to highlight, use the following:
*{S={BACKGROUND=format.}}
25
2
5
55. 3.4 Formats for Display 3-19
Trafficlighting with PROC REPORT
You can use trafficlighting to highlight specific information
about a report.
To trafficlight with PROC REPORT, use a COMPUTE
block for the variable on which you want to trafficlight.
26
2
6
56. 3-20 Chapter 3 Format Uses
Using Formats for Display
/* *************************************************** */
/* Program: f3d3.sas */
/* *************************************************** */
/* This program demonstrates how to use options to */
/* modify the behavior of formats. */
/* *************************************************** */
** Use the NOTSORTED option to control display order. ;
proc format;
value $emp (notsorted)
'PILOT1'-'PILOT3'='Pilot'
'MECH01'-'MECH03'='Mechanics'
'FLTAT1'-'FLTAT3'='Flight Attendant';
run;
** The preloadfmt option along with order=data ;
** results in the table being drawn in the order ;
** in which the format items are defined. ;
** The completetypes option forces all format ;
** items to appear in the output. ;
proc means data=ia.crew order=data completetypes;
class jobcode / preloadfmt;
var salary;
title 'MEANS with PRELOADFMT';
format jobcode $emp.;
run;
** Use the multilabel option to create a ;
** format with overlapping ranges. ;
proc format;
value hires (multilabel)
low -< '01jan1985'd = 'First Group'
'01jan1985'd -< '01jan1990'd = 'Second Group'
'01jan1990'd - high = 'Third Group'
'01jan1980'd -< '01jan1981'd = '1980'
'01jan1981'd -< '01jan1982'd = '1981'
'01jan1982'd -< '01jan1983'd = '1982'
'01jan1983'd -< '01jan1984'd = '1983'
'01jan1984'd -< '01jan1985'd = '1984'
'01jan1985'd -< '01jan1986'd = '1985'
'01jan1986'd -< '01jan1987'd = '1986'
'01jan1987'd -< '01jan1988'd = '1987'
'01jan1988'd -< '01jan1989'd = '1988'
'01jan1989'd -< '01jan1990'd = '1989'
'01jan1990'd -< '01jan1991'd = '1990'
'01jan1991'd -< '01jan1992'd = '1991'
'01jan1992'd -< '01jan1993'd = '1992'
'01jan1993'd -< '01jan1994'd = '1993'
'01jan1994'd -< '01jan1995'd = '1994';
run;
(Continued on the next page.)
57. 3.4 Formats for Display 3-21
** Add the MLF option in the CLASS statement to use the;
** multilabel format.;
proc means data=ia.crew;
var salary;
class hiredate / mlf;
format hiredate hires.;
title 'MEANS with MLF (multilabel format)';
run;
** Use nested formats to combine user-defined and SAS formats.;
proc format;
value benefit
low-'31dec89'd=[worddate20.]
'01jan90'd-high=' ** Not Eligible **'
;
run;
proc print data=ia.crew;
var firstname lastname hiredate;
format hiredate benefit.;
title 'Nested formats';
run;
** Use formats to do trafficlighting.;
proc format;
value fore
low-30000 = 'derp'
30000<-40000 = 'white'
40000<-42000 = 'white'
42000<-high = 'blue'
other = 'white';
value back
low-30000 = 'vpap'
30000<-40000 = 'vlib'
40000<-42000 = 'vipk'
42000<-high = 'vpab'
other = 'white';
run;
(Continued on the next page.)
58. 3-22 Chapter 3 Format Uses
ods listing close;
ods html file='test.html' style=statDoc;
proc tabulate data=ia.crew f=dollar8.;
title 'Traffic Lighting in PROC TABULATE';
where JobCode contains 'FLTAT';
var Salary;
class Location JobCode;
table Location*Salary*(min mean max)*
{s={background=back. foreground=fore.}}, JobCode
/ rts=20 box={label='Flight Attendants'};
keylabel mean='Average' max='Maximum' min='Minimum';
label JobCode='Code'
Location='Location'
Salary='Salary';
run;
proc report data=ia.crew nowd;
title 'Traffic Lighting in PROC REPORT';
columns Location Jobcode firstname lastname name salary ;
define location / order;
define jobcode / order order=formatted f=$emp.;
define firstname / noprint;
define lastname / noprint;
define name / computed 'Employee';
define salary / f=dollar8.
style(column)={background=back. foreground=fore.};
compute name / char length=20;
name=trim(firstname)||' '||lastname;
endcomp;
run;
ods html close;
ods listing;
61. ®
Chapter 4 New in SAS 9.3
4.1 Introduction..................................................................................................................... 4-3
4.2 Viewing Formats and Using Functions ........................................................................ 4-4
Demonstration: New Features in SAS 9.3 ............................................................................. 4-6
63. 4.1 Introduction 4-3
4.1 Introduction
Objectives
View formats in the Explorer window.
Use functions to create formats.
3
3
64. 4-4 Chapter 4 New in SAS® 9.3
4.2 Viewing Formats and Using Functions
Viewing Formats
In the current release of SAS it is possible to view formats
by clicking on the name of the format in the Explorer
window.
5
5
Viewing Formats
This action causes the format definition to be displayed in
the Results Viewer.
6
6
65. 4.2 Viewing Formats and Using Functions 4-5
Using Functions to Define Formats
It is now possible to use a function to define a format.
General form of the VALUE statement:
VALUE $format name OTHER=[function name()];
7
7
Using Functions to Define Formats
It is now possible to use a function to define a format.
Example:
proc format;
value $stn other=[stnamel()];
run;
8
8
66. 4-6 Chapter 4 New in SAS® 9.3
New Features in SAS 9.3
Program f4d1.sas
/* *************************************************** */
/* Program: f4d1.sas */
/* *************************************************** */
/* This program demonstrates the ability to use */
/* functions in formats - new in SAS9.3. */
/* *************************************************** */
proc format;
value $stn other=[stnamel()];
run;
proc report data=ia.summary nowd;
title 'Displaying State Names Using a Format Based on a Function';
column stcode stcode=name;
define stcode / "Original Value";
define name / "Value with Format" format=$stn30.;
run;
/* 1. Use the FCMP procedure to create the function.
2. Use the OPTIONS statement to make the function available to
SAS by specifying the location of the function in the CMPLIB= system
option.
3. Use the FORMAT procedure to create a new format.
4. Use the new format in your SAS program. */
/* 1. Create a function that creates the value Qx from a formatted
value. */
proc fcmp outlib=work.functions.smd;
function qfmt(date) $;
length qnum $4;
qnum=put(date,yyq4.);
if substr(qnum,3,1)='Q'
then return(substr(qnum,3,2));
else return(qnum);
endsub;
run;
/* 2. Make the function available to SAS. */
options cmplib=(work.functions);
(Continued on the next page.)
67. 4.2 Viewing Formats and Using Functions 4-7
/* 3. Create a format using the function created by the FCMP
procedure. */
proc format;
value qfmt other=[qfmt()];
run;
/* 4. Use the format in a SAS program. */
data djia2009;
input closeDate date7. close;
datalines;
01jan09 800.86
02feb09 7062.93
02mar09 7608.92
01apr09 8168.12
01may09 8500.33
01jun09 8447.00
01jul09 9171.61
03aug09 9496.28
01sep09 9712.28
01oct09 9712.73
02nov09 10344.84
02dec09 10428.05
run;
proc print data=djia2009;
title 'Displaying Quarters Using a Format Based on a User-Defined Function';
format closedate qfmt. close dollar9.;
run;