8/19/2019 regain energy
1/27
Chapter Seven
Creating and Applying User-Defned
Formats
8/19/2019 regain energy
2/27
Objectives – Create user-defned ormats using the
FOR A! procedure" – Apply user-defned ormats to variables
in a list report" – #ermanently store the ormats you
create"
– Associate your ormats $ith variables "
2
8/19/2019 regain energy
3/27
Using %A% Formats
• &nhance the readability o reports byormatting the data values "
3
Salary Report
Emp Last First Job Annual Obs ID Name Name Code Salary
1 0031 OLDEN!ER DESIREE "ILO# $%0&''1()' ' 00*0 +ILLIA,S ARLENE ,( FL#A# $'3&)))(1' 3 00-1 "ERR. RO!ER# A( FL#A# $'1&/%-(-1 * 00 ' ,C +IER +A##S C2RIS#INA "ILO# $/)&3 -(3/ % 00/1 SCO## 2AR E. F( FL#A# $3'&'- (*0 ) 010) #2AC4ER DA ID S( FL#A# $'*&1)1(1* - 03%% !ELL #2O,AS !( "ILO# $%/& 03(1) 03)) LENN ,AR#2A S( "ILO# $1'0&'0'(3
8/19/2019 regain energy
4/27
Using User-defned Formats• Create custom ormats to recode data values in a report "
4
Salary Report in Cate5ories
Emp Last First Annual ID Name Name JobCode Salary
0031 OLDEN!ER DESIREE "ilot ,ore t6an %0&000 00*0 +ILLIA,S ARLENE ,( Fli56t Attendant Less t6an '%&000 00-1 "ERR. RO!ER# A( Fli56t Attendant Less t6an '%&000 00 ' ,C +IER +A##S C2RIS#INA "ilot ,ore t6an %0&000 00/1 SCO## 2AR E. F( Fli56t Attendant '%&000 to %0&000 010) #2AC4ER DA ID S( Fli56t Attendant Less t6an '%&000 03%% !ELL #2O,AS !( "ilot ,ore t6an %0&000 03)) LENN ,AR#2A S( "ilot ,ore t6an %0&000
8/19/2019 regain energy
5/27
8/19/2019 regain energy
6/27
Creating User-defnedFormats• 1eneral orm o a #ROC FOR A! step2
6
PROC FORMAT ; VALUE format-name range1= 'label ' range2= 'label '
. . . ;RUN;
Useful Options are
•LIBRARY = libref specify the libref for a SAS data library for apermanent catalog in which user-defined formats are stored.•FMTLIB prints the contents of a format catalog.
PROC FORMAT ; VALUE format-name range1= 'label ' range2= 'label '
. . . ;RUN;
Useful Options are
•LIBRARY = libref specify the libref for a SAS data library for apermanent catalog in which user-defined formats are stored.•FMTLIB prints the contents of a format catalog.
8/19/2019 regain energy
7/27
3o$ to create User-defnedFormats
Rules for Format-name – names the ormat you are creating – cannot be more than 4 characters – or character values, must have a dollar sign
( $ ) as the frst character, a letter or underscoreas the second character, and no more than 6 additional characters, numbers, andunderscores
– or numeric values, must have a letter orunderscore as the frst character and no morethan 5 additional characters, numbers, and
underscores – cannot end in a number – cannot be the name o a %A% ormat – does not end $ith a period in the (A*U&
statement"7
8/19/2019 regain energy
8/27
Creating User-defnedFormats
• Labels – can be up to 60,575 characters in length – are typically enclosed in 8uotes ,
although it is not re8uired"
8
Range(s)
can be single valuesranges of values.
8/19/2019 regain energy
9/27
Creating User-defnedFormats
•Assign labels to single numbers"
9
proc format; value gender 1 =' Female ' 2 =' Male' other =' Miscoded ';run;
Numericformat
name
ormattedvalue
!eywordNumeric datavalue
8/19/2019 regain energy
10/27
Creating User-defnedFormats• Assign labels to ranges o numbers"
10
proc format; value boardfmt low -49 =' elow'
!"-99 ='#verage' 1""- high ='#bove #verage';run;
Numeric data ranges
!eyword
8/19/2019 regain energy
11/27
Creating User-defnedFormats• Assign labels to character values and
ranges o character values"
11
proc format; value $gradefm '#'='%ood' ' '-'&' ='Fair' 'F'=' oor' '(')'*' ='+ee (nstructor' other ='Miscoded';
run;
"haracterformat name
#iscretecharactervalues
"haractervalue range !eyword
8/19/2019 regain energy
12/27
reat ng User- e neFormats
12
$codefmt
Step 1: "reate the format.
Step 2: Apply the format .
proc format; value $codefmt 'F, # '='Flight #ttendant' ' (,. '=' ilot';run;
proc printdata=m/lib0empdata; format ob ode $codefmt 0 ;run;
8/19/2019 regain energy
13/27
Creating User-defnedFormats
13
money
Step 1: "reate the format.
Step 2: Apply the format.
proc format; value mone/ low-32!""" =',ess than 2!)"""' 2!"""-!""""='2!)""" to !")"""' !""""3-high='More than !")"""';
run;
proc printdata=m/lib0empdata; format +alar/ mone/0 ;run;
8/19/2019 regain energy
14/27
Creating User-defnedFormats
14
%ou can use multiple &A U( statements in a single )*+"+*,A step.
proc format; value $codefmt 'F, # '='Flight #ttendant' ' (,. '=' ilot'; value mone/ low-32!""" =',ess than 2!)"""' 2!"""-!""""='2!)""" to !")"""' !""""3-high='More than !")"""';run;
8/19/2019 regain energy
15/27
8/19/2019 regain energy
16/27
Display the list o yourormats
A ter you create your o$n user-defned ormats andsaved in your permanent library, named 9 library :,you can as; %A% to list the ormats you havecreated2
#ROC FOR A! lib < library fmtlib =Run=
Fmtlib is an option in the #ROC FOR A! to list yourormats in the *.+RAR> named 2 library" ormats
catalog"O!&2 user-defned ormat is saved as a FormatCatalog" !he fle name is LIBREF.formats
16
8/19/2019 regain energy
17/27
A %A% #rogram e?ample2#ermanent User-defned Format
libname library @C2 ath77B mt older@=proc ormat lib = library fmtlib =value job mt @#.*O!@
8/19/2019 regain energy
18/27
Output rom fmtlib option in #ROCFOR A!
18
---------------------------------------------------------------------------------------------------- FOR A! A &2 %A*F ! LEN !"# U +&R OF
(A*U&%2 6. *& 1!32 / AE *& 1!32 B %EF&'L! LEN !"
FUGG2 %!D
--------------------------------------------------------------------------------------------------------- %!AR! & D *A+&* '(&R" (5H(407%B/B20/2B72B6)I ---------------------------------------------------------------------------------------------------------- *OJ 6BBBB *o$
6BBB/ 7BBBB oderate7BBBB 3.13 3igh
---------------------------------------------------------------------------------------------------------
N%meric &ariab#e: 'a#ar(
NOTE: The de"a%#t !ength i )!*)+' 8 "or n%meric variab#e
8/19/2019 regain energy
19/27
------------------------------------------------------------------------------------------------------
FOR A! A &2 KO+F ! LEN !"# U +&R OF (A*U&%2 0. *& 1!32 / AE *& 1!32 B %EF&'L! LEN !" FUGG2 B
------------------------------------------------------------------------------------------------------%!AR! & D *A+&* '(&R" (5H(4 07%B/B20/2B72B6)
------------------------------------------------------------------------------------------------------F*!A! F*!A! Flight Attendant#.*O! #.*O! #ilot
-------------------------------------------------------------------------------------------------------
19
, er -e"ined .ormat "or /haracter &ariab#e: obcode
NOTE: The de"a%#t !ength i the #ength o" the #arge t "ormat va#%e$.or thi e amp#e there are 16 character "or va#%e: .#ight )ttendant
8/19/2019 regain energy
20/27
3o$ does %A% handle theFormat behind the sceneM
• Once user-defned ormat is created, these ormatsmust be stored some$here" !here ore, the userneeds to defne the location to store the ormatcatalog in %A% library" !he library can be eithertemporary or permanent"
• !emporary library is the JORN library"• #ermanent library to store Format Catalog is defned
using the *ibname statement"• !he recommended permanent *ibrary Re erence
ame is 9*.+RAR>:"• . the user decides to use *.+R&F di erent rom
*.+RAR>, then, one needs to be a$are that %A%system needs to be told $here to loo; or the specifc
*.+R&F using a system O#!.O % statement" 20
! i F
8/19/2019 regain energy
21/27
!emporary storing FormatCatalog
.n #ROC FOR A! procedure, i there is no *.+RAR>defned or storing the ormat catalog, %A%automatically creates one in the JORN library as atemporary ormat catalog" !he name isJORN"Formats
!he synta? is
#ROC FOR A!=(A*U& ormat-name
range/ < 9label/: range0
8/19/2019 regain energy
22/27
#ermanent *ibrary or FormatCatalog. you $ant to create a permanent library or your
ormat catalog, the synta? is2*.+ A & library 9physical location: =#ROC FOR A! *.+RAR> < library =
(A*U& ormat-name
range/ < 9label/: range0
8/19/2019 regain energy
23/27
3o$ does %A% search or the user-defned ormats to be applied in
your %A% programM
23
/" +y de ault, %A% al$ays search or ormats in theJORN library, $hich is named as JORN"FOR A!% , then, search or a permanent ormat library$ith the libre named *.+RAR> " !he permanent
ormat catalog is named *.+RAR>"FOR A!% " !hen,loo; or other *.+R&F, $hich is O! automaticallydoneQ
0" For the reason in '/), the library name orstoring permanent ormat must be named2*.+RAR>, Other$ise, you must tell %A% system toloo; or your specifc *.+R&F" !his is accomplishedby providing the ormat search se8uence in %A%
system O#!.O % statement, $hich $ill be
8/19/2019 regain energy
24/27
Defne ormat catalog searchse8uence or the libre di ers rom
*.+RAR>As mentioned, %A% search ormat catalog based on theollo$ing order2
'/) JORN library, '0) the library named LIBR&R*
For this reason+ the LIBN&,E statement for spe-ifyinthe stora e lo-ation of formats m/st /se LIBR&R* asthe libref.
3o$ever, users can provide the ormat search se8uence in the%A% program to instruct %A% to locate the library $ith name
di erent rom *.+RAR> using the ollo$ing %A% %ystemO#!.O % statement2
01!I0NS F,!SE&RC" = (libref libref2 33)4%A% $ill search the ormat catalog in the order the libre :s are
listed" 24
&? l i i di t
8/19/2019 regain energy
25/27
&?ample or assigning di erent*ibre or ormat catalog
libname mtemp0 @C2 ath77B mt0@=options mtsearch < ' mtemp0)=proc ormat lib < mtemp0 mtlib=value j mt @#.*O!@
8/19/2019 regain energy
26/27
&?ercise /Open the program c5/"sas" !he data setreported in this program is the &mpdata data2
26
Emp JobObs ID LastName FirstName Code Salary
1 0031 GOLDENBERG DESIREE PILO !0""1#$"" 00%0 &ILLI'(S 'RLENE (# FL ' "3$$$#1"3 00)1 PERR* ROBER '# FL ' "1+!)#)1% 00," (CG&IER-&' S C.RIS IN' PILO +$3,)#3+! 00+1 SCO .'R/E* F# FL ' 3""),#%0$ 010$ .'C ER D'/ID S# FL ' "%1$1#1%) 03!! BELL .O('S B# PILO !+,03#1$, 03$$ GLENN ('R .' S# PILO 1"0"0"#3,
8/19/2019 regain energy
27/27
#rogram /2 Jrite a %A% program to read this data rom your >*.+ libraryand per orm the ollo$ing tas;s2/"Defne the user-defned ormat as a !emporary ormat catalog orvariables" And ma;e sure to list the ormats you created using F !*.+option in your #ROC FOR A! procedure"
Kobcode2 #.*O! as 9#ilot: , F*!A! as 9Flight Attendant:%alary2 lo$ 6BBBB as 9*o$:, 6BBB/-7BBBB as 9 oderate:,7BBBB 3igh as 93igh:
0" Use the ormat as a local ormat statement in the #ROC #rint procedure"
#rogram 02/"Revise the program / above to create a #ermanent ormat catalog using*.+RAR> as libre , $hich is stored in c2 ath77B mt/ older"0"Use the permanent ormant statement in your data step, and print thedata
#rogram 62 Revise your program 0 to do the ollo$ing tas;s or defninguser-defne ormat2/"Defne a *.+R&F, perm mt, that connect to the path c2 ath77B mt00"Jrite the O#!.O % statement to in orm %A% the ormat search se8uence
O#!.O % mtsearch < 'perm mt)=
27