notes7 - Creating User-defined Formats Notes 7: Assigning...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Creating User-defined Formats Notes 7: Assigning and Changing Variable Attributes SAS provides the FORMAT procedure, which enables you to define custom formats. To create and use your own formats, 1. use the FORMAT procedure to create the format 2. apply the format to specific variable(s) by using a FORMAT statement. 1 2 Creating User-defined Formats General form of a PROC FORMAT step: PROC FORMAT; PROC FORMAT; VALUE format-name range1='label ' ' VALUE format-name range1='label range2='label ' ' range2='label ......;; RUN; RUN; By default, the new format will be stored in the WORK library and is temporary. To create permanent formats, use the LIBRARY=libref option in the PROC FORMAT statement, then use the FMTSEARCH system option as in the following statement: options fmtsearch=(st); 3 Creating User-defined Formats Format-name names the format you are creating cannot be more than 32 characters in SAS System 9 for character values, must have a dollar sign ($) as the first character, a letter or underscore as the second character for numeric values, must have a letter or underscore as the first character cannot end in a number cannot be the name of an existing SAS format does not end with a period in the VALUE statement. 4 Creating User-defined Formats Labels can be up to 32,767 characters in length are typically enclosed in quotes, although it is not required. Range(s) can be single values ranges of values. Creating User-defined Formats Assign labels to single numbers. proc format; value gender 1='Female' 2='Male' other='Miscoded'; run; Numeric format name Numeric data value Formatted value Keyword 5 6 1 Creating User-defined Formats Assign labels to ranges of numbers. Keyword proc format; value boardfmt low-49='Below' 50-99='Average' 100-high='Above Average'; run; Numeric data ranges Creating User-defined Formats Step 1: Create the format. proc format; value money low-<25000 ='Less than 25,000' 25000-50000='25,000 to 50,000' 50000<-high='More than 50,000'; run; Step 2: Apply the format. proc print data=st.empdata; format Salary money.; run; 7 money 8 Creating User-defined Formats Assign labels to character values and ranges of character values. Character format name proc format; value $grade 'A'='Good' 'B'-'D'='Fair' 'F'='Poor' 'I','U'='See Instructor' other='Miscoded'; run; Character value range Discrete character values Keyword 9 Creating User-defined Formats Step 1: Create the format. proc format; value $codefmt 'FLTAT'='Flight Attendant' 'PILOT'='Pilot'; run; $codefmt Step 2: Apply the format. proc print data=st.empdata; format JobCode $codefmt.; run; 10 Creating User-defined Formats You can use multiple VALUE statements in a single PROC FORMAT step. proc format; value $codefmt 'FLTAT'='Flight Attendant' 'PILOT'='Pilot'; value money low-<25000 ='Less than 25,000' 25000-50000='25,000 to 50,000' 50000<-high='More than 50,000'; run; Applying User-defined Formats proc print data=st.empdata split=' ' noobs; label LastName='Last Name' FirstName='First Name' Salary='Annual Salary'; format Jobcode $codefmt. Salary money.; title1 'Salary Report in Categories'; run; Salary Report in Categories Emp ID 0031 0040 0071 0082 0091 0106 0355 0366 Last Name GOLDENBERG WILLIAMS PERRY MCGWIERMCGWIER-WATTS SCOTT THACKER BELL GLENN First Name DESIREE ARLENE M. ROBERT A. CHRISTINA HARVEY F. DAVID S. THOMAS B. MARTHA S. Annual Salary 50,000 25,000 25,000 50,000 50,000 25,000 50,000 50,000 12 JobCode Pilot Flight Flight Pilot Flight Flight Pilot Pilot 11 More than Attendant Less than Attendant Less than More than Attendant 25,000 to Attendant Less than More than More than 2 Default Variable Attributes When a variable is created in a DATA step, the name, type, and length of the variable are automatically assigned remaining attributes such as label and format are not automatically assigned. When the variable is used in a later step, the name is displayed for identification purposes its value is displayed using a systemdetermined format. 13 Default Variable Attributes Create the st.dfwlax data set. libname st 'D:\Stat 440'; data st.dfwlax; infile 'D:\Stat 440\dfwlax.dat'; input @1 Flight $3. @4 Date mmddyy8. @12 Dest $3. @15 FirstClass 3. @18 Economy 3.; run; 14 Examine the descriptor portion of the st.dfwlax data set. proc contents data=st.dfwlax; run; Partial Output Alphabetic List of Variables and Attributes # 2 3 5 4 1 Variable Date Dest Economy FirstClass Flight Type Num Char Num Num Char Len 8 3 8 8 3 Default Variable Attributes Specifying Variable Attributes Use LABEL and FORMAT statements in the PROC step to temporarily assign the attributes (for the duration of the step only) DATA step to permanently assign the attributes (stored in the data set descriptor portion). 15 16 Temporary Variable Attributes Use LABEL and FORMAT statements in a PROC step to temporarily assign attributes. proc print data=st.dfwlax label; format Date mmddyy10.; label Dest='Destination' FirstClass='First Class Passengers' Economy='Economy Passengers'; run; Temporary Variable Attributes The SAS System First Class Passengers 20 20 15 5 14 15 17 7 . 14 Obs 1 2 3 4 5 6 7 8 9 10 Flight 439 921 114 982 439 982 431 982 114 982 Date 12/11/2000 12/11/2000 12/12/2000 12/12/2000 12/13/2000 12/13/2000 12/14/2000 12/14/2000 12/15/2000 12/15/2000 Destination LAX DFW LAX dfw LAX DFW LaX DFW LAX DFW Economy Passengers 137 131 170 85 196 116 166 88 187 31 17 18 3 Permanent Variable Attributes Assign labels and formats in the DATA step. libname st 'D:\Stat 440'; data st.dfwlax; infile 'raw-data-file'; input @1 Flight $3. @4 Date mmddyy8. @12 Dest $3. @15 FirstClass 3. @18 Economy 3.; format Date mmddyy10.; label Dest='Destination' FirstClass='First Class Passengers' Economy='Economy Passengers'; run; 19 Permanent Variable Attributes Examine the descriptor portion of the st.dfwlax data set. proc contents data=st.dfwlax; run; Partial Output Alphabetic List of Variables and Attributes # 2 3 5 4 1 Variable Date Dest Economy FirstClass Flight Type Num Char Num Num Char Len 8 3 8 8 3 Format MMDDYY10. Destination Economy Passengers Passengers Passengers First Class Passengers Label 20 Permanent Variable Attributes proc print data=st.dfwlax label; run; The SAS System First Class Passengers 20 20 15 5 14 15 17 7 . 14 Override Permanent Attributes Use a FORMAT statement in a PROC step to temporarily override the format stored in the data set descriptor. Obs 1 2 3 4 5 6 7 8 9 10 Flight 439 921 114 982 439 982 431 982 114 982 Date 12/11/2000 12/11/2000 12/12/2000 12/12/2000 12/13/2000 12/13/2000 12/14/2000 12/14/2000 12/15/2000 12/15/2000 Destination LAX DFW LAX dfw LAX DFW LaX DFW LAX DFW Economy Passengers 137 131 170 85 196 116 166 88 187 31 21 proc print data=st.dfwlax label; format Date date9.; run; 22 Override Permanent Attributes The SAS System First Class Passengers 20 20 15 5 14 15 17 7 . 14 Obs 1 2 3 4 5 6 7 8 9 10 Flight 439 921 114 982 439 982 431 982 114 982 Date 11DEC2000 11DEC2000 12DEC2000 12DEC2000 13DEC2000 13DEC2000 14DEC2000 14DEC2000 15DEC2000 15DEC2000 Destination LAX DFW LAX dfw LAX DFW LaX DFW LAX DFW Economy Passengers 137 131 170 85 196 116 166 88 187 31 The DATASETS Procedure You can use the DATASETS procedure to modify a variable's name label format informat. 23 24 4 The DATASETS Procedure General form of PROC DATASETS for changing variable attributes: PROC DATASETS LIBRARY=libref ;; PROC DATASETS LIBRARY=libref MODIFY SAS-data-set ;; MODIFY SAS-data-set RENAME old-name-1=new-name-1 RENAME old-name-1=new-name-1 <. ....old-name-n=new-name-n>; <. old-name-n=new-name-n>; LABEL variable-1='label-1' LABEL variable-1='label-1' <. ....variable-n='label-n'>; <. variable-n='label-n'>; FORMAT variable-list-1 format-1 FORMAT variable-list-1 format-1 <. ....variable-list-n format-n>; <. variable-list-n format-n>; INFORMAT variable-list-1 informat-1 INFORMAT variable-list-1 informat-1 <. ....variable-list-n informat-n>; <. variable-list-n informat-n>; RUN; RUN; 25 Data Set Contents Use the DATASETS procedure to change the name of the variable Dest to Destination. Look at the attributes of the variables in the st.dfwlax data set. proc contents data=st.dfwlax; run; Alphabetic List of Variables and Attributes # 2 3 5 4 1 Variable Date Dest Economy FirstClass Flight Type Num Char Num Num Char Len 8 3 8 8 3 26 The DATASETS Procedure Rename the variable Dest to Destination. proc datasets library=st; modify dfwlax; rename Dest=Destination; run; Data Set Contents Look at the attributes of the variables in the st.dfwlax data set after running PROC DATASETS. proc contents data=st.dfwlax; run; Alphabetic List of Variables and Attributes # 2 3 5 4 1 Variable Date Destination Economy FirstClass Flight Type Num Char Num Num Char Len 8 3 8 8 3 27 28 Lab Exercises 6 Creating and Applying User-defined Formats Assigning Variable Attributes 29 5 ...
View Full Document

Ask a homework question - tutors are online