Formats, Informats, and Dates
Prerequisites: Variables, types, and missing values
- A format controls how a value is displayed:
dollar9.2,date9.,comma8.. - An informat controls how raw text is read:
date9.reads15MAR2024,comma8.reads52,000.
Both are names ending in a period, often with a width and decimals:
dollar9.2 means "9 characters wide, 2 decimals". Character formats
start with $.
Dates are numbers
A SAS date is the number of days since 1 January 1960. Write a date
constant as a quoted ddMONyyyy followed by d:
data _null_;
d = '15MAR2024'd;
put d=;
put d= date9.;
put d= mmddyy10.;
put d= yymmdd10.;
put d= ddmmyy10.;
days = '15MAR2024'd - '01JAN2024'd;
put days=;
run;
Log (abridged):
d=23450
d=15MAR2024
d=03/15/2024
d=2024-03-15
d=15/03/2024
days=74
Because dates are numbers, subtracting two dates gives the days between
them, and adding 7 moves a week forward. Always attach a date format
when you display them, or you'll see numbers like 23450.
Handy date functions:
data _null_;
d = mdy(3, 15, 2024);
y = year(d);
m = month(d);
wd = weekday(d);
put y= m= wd=;
run;
y=2024 m=3 wd=6
WEEKDAY returns 1 for Sunday through 7 for Saturday — 15 March 2024 was
a Friday. TODAY() returns the current date.
Reading and displaying with formats
data hires;
input name $ hired :date9. salary :comma8.;
format hired date9. salary dollar9.;
datalines;
Ada 15MAR2024 52,000
Alan 01SEP2023 61,500
;
run;
proc print data=hires;
run;
Obs name hired salary
1 Ada 15MAR2024 $52,000
2 Alan 01SEP2023 $61,500
- The colon in
:date9.means "read this informat but still stop at the next blank" — use it with list input. FORMATin a DATA step stores the format with the variable, so every procedure displays it that way. Inside a PROC step, it applies to that step only.- The stored values are unchanged:
salaryis still52000.
Common formats
| Format | Value | Displays |
|---|---|---|
8.2 |
3.14159 | 3.14 |
comma10. |
1234567 | 1,234,567 |
dollar9.2 |
1234.5 | $1,234.50 |
percent8.1 |
0.256 | 25.6% |
date9. |
23450 | 15MAR2024 |
weekdate. |
23450 | Friday, March 15, 2024 |
Level 2 shows how to create your own formats with PROC FORMAT.