SAS Helpful Hints
Formats and Informats
User Defined
Formats are wonderful tools for converting data as it is read in
and/or displayed. The following examples show how to use character,
numeric, date and time formats.
Library=library creates a file of formats that may be used
by other programs as long as they have a corresponding Libname
library statement.
Fmtlib prints out or displays the entire
format library.
Libname library 'C:\';
Proc Format Library=library Fmtlib;
*Character format;
Value $gender
'M'='Male' 'F'='Female';
*Numeric formats;
Value yesno 0='No'
1='Yes';
Value age 1-12='Pre-teen'
13-15='Teen' 16-18='Student Driver'
19,20='No
Alcohol' 21-30='21-30 Years Old';
*Character informat;
Invalue $color
'R'='Red' 'O'='Orange' 'Y'='Yellow' 'G'='Green'
'B'='Blue'
'I'='Indigo' 'V'='Violet';
*Numeric informats;
Invalue colorn
'Red'=1 'Orange'=2 'Yellow'=3 'Green'=4
'Blue'=5
'Indigo'=6 'Violet'=7;
Invalue inyesno
'No'=0 'Yes'=1;
Run;
Data work.A;
Length color $6.;
Input id sex $ age @6 ageb honor_roll @11
color $color1. @13 colorb $
@13 colorb_number
colorn.;
*Create a character from a numeric. Very useful
shortcut. Try it with date formats!;
hr1=compress(input(put(honor_roll,yesno.),$3.));
*Create a numeric from a character.;
hr2=input(put(hr1,$3.),inyesno.);
Cards;
24 F 8 1 O Red
32 M 14 1 B Orange
38 F 17 0 G Yellow
46 M 20 0 Y Green
52 F 24 1 I Blue
;
Run;
Proc Print;
ID id;
Var sex age ageb honor_roll hr1 hr2 color
colorb colorb_number;
Format sex $gender.
ageb age. honor_roll yesno.;
Run;
NOTE that the values of sex, ageb and honor_roll are changed as
they are displayed. NOTE that the values of color and colorb_number
are altered as they are read in.
Results
| id |
sex |
age |
ageb |
honor_
roll |
hr1 |
hr2 |
color |
colorb |
colorb_
number |
| 24 |
Female |
8 |
Pre-teen |
Yes |
Yes |
1 |
Orange |
Red |
1 |
| 32 |
Male |
14 |
Teen |
Yes |
Yes |
1 |
Blue |
Orange |
2 |
| 38 |
Female |
17 |
Student Driver |
No |
No |
0 |
Green |
Yellow |
3 |
| 46 |
Male |
20 |
No Alcohol |
No |
No |
0 |
Yelow |
Green |
4 |
| 52 |
Female |
24 |
21-30 Years Old |
Yes |
Yes |
1 |
Indigo |
Blue |
5 |

Here are a couple of my favorite numeric formats.
Data work.B;
Input x Comma10.2;
x_comma=x;
Input y Percent7.2;
y_percent=y;
Format x_comma Comma10.2
y_percent Percent7.2;
Cards;
1,234.56
83.24%
24,321.12
103.82%
;
Run;
Results
| x |
x_comma |
y |
y_percent |
| 1234.56 |
1,234.56 |
0.8324 |
83.2% |
| 24321.12 |
24,321.12 |
1.0382 |
104% |

Here is an example of my favorite date formats.
Data work.C;
Input d1 Date9.;
d1date=d1;
d1weekdate=d1;
d1worddate=d1;
weekday=d1;
Input d2 Datetime18.;
d2datetime=d2;
Format d1date Date9.
d1weekdate Weekdate. d1worddate Worddate.
weekday Weekday.
d2datetime Datetime18.;
Cards;
12OCT2001
12OCT2001:16:34:04
4APR1994
14MAR1999:3:15:24
;
Run;
Results
| d1 |
d1date |
d1weekdate |
d1worddate |
weekday |
d2 |
d2datetime |
| 15260 |
12OCT2001 |
Friday, October 12, 2001 |
October 12, 2001 |
6 |
1318523644 |
12OCT01:16:34:04 |
| 12512 |
04APR1994 |
Monday, April 4, 1994 |
April 4, 1994 |
2 |
1237000524 |
14MAR99:03:15:24 |

More date formats.
Data work.D;
Input d3 Ddmmyy10.;
d3ddmmyy=d3;
Input d4 Mmddyy10.;
d4mmddyy=d4;
Format d3ddmmyy Ddmmyy10.
d4mmddyy Mmddyy10.;
Cards;
22/10/2001
10/12/2001
19/7/2000
2/5/2001
;
Run;
Results
| d3 |
d3ddmmyy |
d4 |
d4mmddyy |
| 15270 |
22/10/2001 |
15260 |
10/12/2001 |
| 14810 |
19/07/2000 |
15011 |
02/05/2001 |

This is an example of a couple time formats.
Data work.E;
Input t Time8.;
t2=t;
t3=t;
Format t2 Time8. t3
Timeampm11. ;
Cards;
16:34:04
5:3:38
;
Run;
Results
| t |
t2 |
t3 |
| 59644 |
16:34:04 |
4:34:04 PM
|
| 18218 |
5:03:38 |
5:03:38 AM
|

If you have a dataset with formated variables, but do not have the
formats, SAS does not allow you to use the dataset without the formats.
Procedures and data steps refuse to open the dataset. To remove or
ignore the formats associated with variables, add the following statement:
Format _all_;
This allows you to use the saveset without the formats. This works
in the datastep and all SAS procedures.
Please send your comments and suggestions about this web page to A. Watts
(watts@bst.rochester.edu)
|