SAS Formats
SAS supports numeric variables and strings, with conversion options. There are many (i.e. hundreds) of formats on top of numerics that impact visualization, not storage.
Datetime
SAS prefers the DATE9 format--date constants are declared as "ddMMMyyyy"d. However there is a plethora of formats available.
Format |
Appears as... |
Note |
DATETIME20 |
dd-MMM-yyyy:hh:mm:ss |
MMM is as JAN, not 001 |
DATETIME18 |
ddMMMyyyy:hh:mm:ss |
MMM is as JAN, not 001 |
DATETIME16 |
ddMMMyy:hh:mm:ss |
MMM is as JAN, not 001 |
DATE11 |
dd-MMM-yyyy |
MMM is as JAN, not 001 |
DATE9 |
ddMMMyyyy |
MMM is as JAN, not 001 |
DATE7 |
ddMMMyy |
MMM is as JAN, not 001 |
TIME8 |
hh:mm:ss |
|
TIME5 |
hh:mm |
|
MMDDYY10 |
mm/dd/yyyy |
|
MMDDYY8 |
mm/dd/yy |
|
DDMMYY10 |
dd/mm/yyyy |
|
DDMMYY8 |
dd/mm/yy |
|
MONYY7 |
MMMyyyy |
MMM is as JAN, not 001 |
MONNAME |
M |
M is as January |
Computation
Dates and datetimes can be computed from scratch like:
today = today(); new_years_day = mdy(1,1,2020);
Dates and datetimes can be computed relative to another value like:
one_month_away = intnx('month', today(), 1); four_weeks_ago = intnx('week', today(), -4);
Valid date intervals are: YEAR, SEMIYEAR, QTR, MONTH, SEMIMONTH, TENDAY, WEEK, WEEKDAY, and DAY.
Valid datetime intervals are: DTYEAR, DTSEMIYEAR, DTQTR, DTMONTH, DTSEMIMONTH, DTTENDAY, DTWEEK, DTWEEKDAY, and DTDAY.
Data can be extracted from dates and datetimes using day(), month(), and year().
Numerics
Given 12345.67...
Format |
Using... |
Appears as... |
Note |
w. |
5. |
12346 |
Round to nearest integer |
w.d |
5.1 |
12345.7 |
Round to nearest decimal at lowest precision |
COMMAw.d |
COMMA6.1 |
12,345.7 |
|
COMMAw.d |
COMMA5.1 |
12345.7 |
Insufficient width for comma |
DOLLARw.d |
DOLLAR10.2 |
$12,345.67 |
|
EUROw.d |
DOLLAR10.2 |
€12.345,67 |
European-style decimal and commas |
Zw.d |
Z8. |
00012346 |
|
Strings
String variables are only formatted to a length. The format is specified as $N where N is the width.
Length Statement
Often, it is important to specify a string's length before creating it. Observe:
data _NULL_; VAR="FOO"; VAR="FOOBAR"; run; /* variable VAR is 3-wide and contains "FOO" */ data _NULL_; length VAR $60; VAR="FOO"; VAR="FOOBAR"; run; /* variable VAR is 60-wide and contains "FOOBAR" */
put Formats
The put function accepts additional formatting details:
-L left aligns the value
-C centers the value
-R right aligns the value
VAR2 = put(VAR1, $60. -R);