Changing Formats Temporarily
If you are preparing a report that requires the date in a different format, then you can
override the permanent format by using a FORMAT statement in a PROC step. For
example, to display the value for DepartureDate in the data set MYLIB.TOURDATES in
the form of month-name dd, yyyy, you can issue a FORMAT statement in a PROC
PRINT step. The following program specifies the WORDDATE18. format for the
variable DepartureDate:
proc print data=mylib.tourdates;
title 'Tour Departure Dates';
format DepartureDate worddate18.;
The following output displays the results:
Figure 15.9 Overriding a Previously Specified Format
The format DATE9. is still permanently assigned to DepartureDate. Calendar dates in
the remaining examples are in the form ddMMMyyyy unless a FORMAT statement is
included in the PROC PRINT step.
Using Dates in Calculations
Sorting Dates
Because SAS date values are numeric variables, you can sort them and use them in
calculations. The following example uses the data set MYLIB.TOURDATES to extract
other information about the Tradewinds Travel data.
Using Dates in Calculations 237
To help determine how frequently tours are scheduled, you can print a report with the
tours listed in chronological order. The first step is to specify the following BY statement
in a PROC SORT step to tell SAS to arrange the observations in ascending order of the
date variable DepartureDate:
by DepartureDate;
By using a VAR statement in the following PROC PRINT step, you can list the departure
date as the first column in the report:
proc sort data=mylib.fmttourdate out=sortdate;
by DepartureDate;
proc print data=sortdate;
var DepartureDate Country Nights;
title 'Departure Dates Listed in Chronological Order';
The following output displays the results:
Figure 15.10 Sorting by SAS Date Values
The observations in the data set SORTDATE are now arranged in chronological order.
Note that there are no FORMAT statements in this example, so the dates are displayed in
the DATE9. format that you assigned to DepartureDate when you created the data set
Creating New Date Variables
Because you know the departure date and the number of nights spent on each tour, you
can calculate the return date for each tour. To start, create a new variable by adding the
number of nights to the departure date, as follows:
Return = DepartureDate + Nights;
238 Chapter 15 Working with Dates in the SAS System

Get Step-by-Step Programming with Base SAS 9.4, Second Edition, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.