SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT...

33
SAS ® Formats and the FORMAT Procedure Handout Developed by Christine Riddiough. Editing and production support provided by the Curriculum Development and Support Department. SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. indicates USA registration. Other brand and product names are registered trademarks or trademarks of their respective companies. Updated August 2015. Handout code HC066.

Transcript of SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT...

Page 1: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Handout

Developed by Christine Riddiough. Editing and production support provided by the Curriculum Development

and Support Department.

SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS

Institute Inc. in the USA and other countries. indicates USA registration. Other brand and product names are

registered trademarks or trademarks of their respective companies.

Updated August 2015. Handout code HC066.

Page 2: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.
Page 3: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 3

Table of Contents

Formatting Basics: Creating and Using Formats ............................................................................ 4

Format Utilities ............................................................................................................................... 4

Creating Permanent Formats.............................................. Error! Bookmark not defined.

Demonstration f2d1 ................................................................................................ 7

Demonstration f2d2 .............................................................................................. 11

The PICTURE and INVALUE Statements ....................................................................... 13

Demonstration f2d3 .............................................................................................. 15

Demonstration f2d4 .............................................................................................. 18

Format Uses .................................................................................................................................. 20

Grouping Data ................................................................................................................... 21

Demonstration f3d1 .................................................................................. 23

Table Lookups .................................................................................................................. 25

Demonstration f3d2 .............................................................................................. 27

Formats for Display .......................................................................................................... 28

Demonstration f3d3 .............................................................................................. 31

Page 4: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 4

Formatting Basics: Creating and Using Formats

Formats are typically used in SAS to display data values in a report. For example, while dates are stored as the

number of days before or after January 1, 1960, one usually wants to display them in a report in a way that human

beings will understand. To use a format one must add a format statement to a data step or proc step. For example:

/* the data set ia.delay has a flight date variable */

proc print data=ia.delay;

where date eq '01mar1995'd;

var date orig dest delay;

format date mmddyy10.;

/* the mmddyy10. Format displays dates in the form

month/day/year – 11/19/95 */

title 'Using a SAS Format for a Date';

run;

SAS has many formats that often satisfy those needs, but there are instances where a built-in format is not available.

In those instances the user can create their own formats. To do this one must first create the format using PROC

FORMAT and then must apply it using a format statement. One form of the format procedure uses the VALUE

statement to define the format. The general form of this statement is:

PROC FORMAT; VALUE format-name range1='label'

range2='label'

…;

RUN;

In addition to the VALUE statement, you can define formats for numbers using the PICTURE statement and

informats using the INVALUE statement.

These basic format ideas only scratch the surface of what you can do with formats. In the following sections we’ll

explore some of the tools available to build formats and some of the ways they can be used.

Format Utilities

Using the syntax above a format would be created in the work library. Like other items in that library it would be

deleted when the SAS session ended. Saving the code that created the format is one way to recreate the format when

it is next needed. However, one can also create a format in a permanent library so that is available whenever it is

needed. To create a format in a permanent library one must specify the library (and optionally the catalog) on the

PROC FORMAT statement.

For example International Airlines is a (fictitious) airline that carries passengers and cargo around the world. You

may be the user who maintains information about the airports that IA travels to. The data is stored as a three-letter

airport code, but in your reports you want to list the city where the airport is located. These reports may include

information about numbers of passengers traveling to these cities over the course of the year, amount of cargo

transported, crew schedules and so on. You are going to create and use a format to display the information as

required. Since it will be used many times you will save it in your IA library. To do this you will start with the

following code:

/* Create the format in the ia library */

proc format library=ia;

value cityfmt ‘ORD’=’Chicago’

‘DCA’=’Washington’

‘LAX’=’Los Angeles’;

run;

Page 5: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 5

This will save the format in the ia library in a catalog called formats. If you wanted to name the catalog something

different you could specify a catalog name on the PROC FORMAT statement:

proc format library=ia.formats2;

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 a libref, formats are stored permanently in

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

catalog.

SAS catalogs are special SAS files that store many different kinds of information in smaller units called entries. The

formats are stored as SAS catalog entries. You can have multiple catalogs in multiple libraries. Those catalogs can

each store multiple catalog entries. Catalog entries have four-level names in the form

libref.formats.formatname.format(c). The last level will simply be format if the format aapplies

to numeric values and formatc if it applies to character values.

Once the format has been created it can be used on a format statement. However, for formats stored in permanent

libraries you have to tell SAS where to find those formats. To do that you can used the FMTSEARCH system

option. The general form of this option is:

19

Using the FMTSEARCH System OptionTo 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);OPTIONS FMTSEARCH =(item-1 item-2…item-n);

20

......

options fmtsearch=(ia ia.formats2);

Using the FMTSEARCH System Option

SAS Supplied FormatsSAS Supplied Formats

work.formatswork.formats

ia.formatsia.formats

ia. formats2ia. formats2

Page 6: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 6

21

Using the NOFMTERR OptionBy 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;OPTIONS FMTERR | NOFMTERR;

22

The CATALOG ProcedureThe 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.

24

Documenting FormatsYou can use the FMTLIB option in the PROC FORMAT

statement to document the format.

General form of the FMTLIB option:

PROC FORMAT LIBRARY = libref.CATALOGFMTLIB;

<other statements>;RUN;

PROC FORMAT LIBRARY = libref.CATALOGFMTLIB;

<other statements>;RUN;

Page 7: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 7

Demonstration f2d1

/* *************************************************** */

/* 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;

Page 8: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 8

** 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;

Page 9: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 9

Using Data Sets to Create Formats

26

Using a Control Data Set to Create a FormatYou 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;

PROC FORMAT LIBRARY=libref.CATALOG

CNTLIN=SAS-data-set;

RUN;

27

Using a Control Data Set to Create a FormatTo use an input data set to create a format, the data set

must contain the variables FMTNAME, START, and

LABEL.

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

It may be necessary to include other variables.

Page 10: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 10

28

Maintaining Permanent FormatsTo maintain formats, you may need to output it to a data

set.

General form of PROC FORMAT with the CNTLOUT=

option:

PROC FORMAT LIBRARY=libref.CATALOGCNTLOUT=SAS-data-set;

<other statements>;RUN;

PROC FORMAT LIBRARY=libref.CATALOGCNTLOUT=SAS-data-set;

<other statements>;RUN;

To maintain a format, you

use CNTLOUT= to create a data set

add, update or delete observations in the data set

use CNTLIN= to recreate the format.

Page 11: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 11

Demonstration f2d2

/* *************************************************** */

/* 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;

Page 12: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 12

** 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;

** Recreate 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;

Page 13: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 13

The PICTURE and INVALUE Statements

30

The PICTURE StatementThe PICTURE statement in PROC FORMAT defines a

template for data values.

PROC FORMAT;

PICTURE name

value-range-set-1 <(picture-1-option(s))>;

RUN;

PROC FORMAT;

PICTURE name

value-range-set-1 <(picture-1-option(s))>;

RUN;

Page 14: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 14

33

Directives on a PICTURE Statement LOW and HIGH are keywords signifying 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;

The 0 in the directive indicates if the day of the month is 1 digit; precede that digit with a 0.

Directives include

%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

Page 15: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 15

Demonstration f2d3

/* *************************************************** */

/* 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;

** 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

Page 16: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 16

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;

Page 17: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 17

35

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;

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;

36

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.

37

UPCASE Option The UPCASE option automatically uppercases all input

values before they are compared to the informat

domain.

It is used in an INVALUE statement when creating an

informat

Page 18: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 18

Demonstration f2d4

/* *************************************************** */

/* 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;

data same;

infile datalines;

input @1 Original @1 Stored tryit.;

datalines;

5

15

;

run;

Page 19: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 19

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;

Page 20: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 20

Format Uses

40

Objectives Use formats to group data.

Use formats to look up data.

Use formats to display data.

Page 21: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 21

Grouping Data

41

Grouping Data in ProceduresProcedures that group data may use the formatted

value to create reports:

PROC FREQ

PROC MEANS

PROC GCHART

42

Grouping Data in the DATA StepYou 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>;BY GROUPFORMAT variable-name <NOTSORTED>;

Page 22: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 22

43

Tips for Using the GROUPFORMAT OptionThe 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 PROC REPORT.

44

Advantages of GROUPFORMATThe GROUPFORMAT option

can be used to create ordered/grouped reports without

sorting the data

frequently eliminates the need for another step.

Page 23: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 23

Demonstration f3d1

/* *************************************************** */

/* 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;

/* 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;

Page 24: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 24

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;

** 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;

Page 25: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 25

Table Lookups

46

Lookup ValuesYou can use formats for table lookups when you want to

lookup one value.

For example, you may have a model number stored with

your data, but you want to include the model type.

...

47

Using the FORMAT ProcedureYou would 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;

continued...

48

Using the FORMAT ProcedureUse the format in a PUT function to “look up” the values

you need:

options fmtsearch = (ia);

data models;

set ia.aircraft;

ModelType = put(ModelNum,Model.);

run;

Page 26: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 26

49

Using the FORMAT ProcedureUse the format in a FORMAT statement to “look up” the

values you need:

options fmtsearch=(ia);

proc print data=ia.aircraft;

format ModelNum Model.;

run;

Page 27: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 27

Demonstration f3d2

/* *************************************************** */

/* 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;

Page 28: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 28

Formats for Display

52

Formats for DisplayFormats are most obviously used for display, but various

options can increase their useability:

FUZZ, NOTSORTED, MULTILABEL

nesting formats

use for colors/traffic lighting

53

Format OptionsOptions can be used in PROC FORMAT to modify the

format’s structure:

FUZZ

NOTSORTED

MULTILABEL.

55

Format Options - NOTSORTEDNOTSORTED Option

Stores the format in the order in the VALUE statement.

Use it to:

– save processing time

– preserve the order in which you define ranges.

Use with PRELOADFMT option and ORDER=DATA to

maintain the order in certain procedures.

Page 29: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 29

Use NOTSORTED when

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 that you define ranges when you print a description of the informat or

format using the FMTLIB option.

you want to preserve the order that 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.

56

Creating Overlapping RangesTo create overlapping ranges, use the MULTILABEL

option in the VALUE statement in PROC FORMAT:

Create a report with any procedure that supports the MLF

option:

PROC TABULATE

PROC MEANS

PROC SUMMARY.

VALUE <$>fmtname <(MULTILABEL)> ...VALUE <$>fmtname <(MULTILABEL)> ...

57

Nested FormatsCreate a format that maps value ranges to existing

formats. Use when

a modular approach is needed for ease of

maintenance

one format is not desirable across the entire

range.

Page 30: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 30

58

Using Formats to Apply ColorsYou 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';

PROC FORMAT;

VALUE $fmtname 'data-value-1'='color-1'

'data-value-2'='color-2'

...

'data-value-n'='color-n';

59

Traffic Lighting with PROC TABULATEYou can use traffic lighting to highlight specific information

about a report.

To add cell traffic lighting,

create a user-defined format

on the column you want to highlight, use

*{S={BACKGROUND=format.}}*{S={BACKGROUND=format.}}

60

Traffic Lighting with PROC REPORTYou can use traffic lighting to highlight specific information

about a report.

To traffic light with PROC REPORT, use a compute block

for the variable on which you want to traffic light.

Page 31: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 31

Demonstration f3d3

/* *************************************************** */

/* 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;

Page 32: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 32

** Add the mlf option on 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 traffic lighting.;

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;

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;

Page 33: SAS Formats and the FORMAT Procedure - Lex Jansen · 2015-09-23 · SAS® Formats and the FORMAT Procedure Page 5 This will save the format in the ia library in a catalog called formats.

SAS® Formats and the FORMAT Procedure

Page 33

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;