Previous Page | Next Page

Working with Dates in the SAS System

Using SAS Date Functions


Finding the Day of the Week

SAS has various functions that produce calendar dates from SAS date values. SAS date functions enable you to do such things as derive partial date information or use the current date in calculations.

If the final payment for a tour is due 30 days before the tour leaves, then the final payment date can be calculated using subtraction; however, Tradewinds Travel is closed on Sundays. If the payment is due on a Sunday, then an additional day must be subtracted to make the payment due on Saturday. The WEEKDAY function, which returns the day of the week as a number from 1 through 7 (Sunday through Saturday) can be used to determine if the return day is a Sunday.

The following statements determine the final payment date by

DueDate = DepartureDate - 30;
if Weekday(DueDate) = 1 then DueDate = DueDate - 1;

Constructing a data set with these statements produces a list of payment due dates. The following program includes these statements and assigns the format WEEKDATE29. to the new variable DueDate:

options yearcutoff=1920 pagesize=60 linesize=80 pageno=1 nodate;
data pay;
   set mylib.tourdates;
   DueDate = DepartureDate - 30;
   if Weekday(DueDate) = 1 then DueDate = DueDate - 1;
   format DueDate weekdate29.;
run;

proc print data=pay;
   var Country DueDate;
   title 'Date and Day of Week Payment Is Due';
run;

Using the WEEKDAY Function

                      Date and Day of Week Payment Is Due                      1

              Obs    Country                              DueDate

                1    Japan               Thursday, April 13, 2000
                2    Greece            Friday, September 17, 1999
                3    New Zealand        Thursday, January 4, 2001
                4    Brazil              Monday, January 29, 2001
                5    Venezuela        Wednesday, October 11, 2000
                6    Italy                 Monday, March 26, 2001
                7    Russia                 Saturday, May 3, 1997
                8    Switzerland        Friday, December 15, 2000
                9    Australia       Thursday, September 24, 1998
               10    Ireland                Friday, July 28, 2000

Calculating a Date from Today

Tradewinds Travel occasionally gets the opportunity to do special advertising promotions. In general, tours that depart more than 90 days from today's date, but less than 180 days from today's date, are advertised. The following figure illustrates the time frame for advertising:

Optimum Interval for Advertising Tours Based on Today's Date

[Optimum Interval for Advertising Tours Based on Today's Date]

A program is needed that determines which tours leave between 90 and 180 days from the date the program is run, regardless of when you run the program.

The TODAY function produces a SAS date value that corresponds to the date when the program is run. The following statements determine which tours depart at least 90 days from today's date but not more than 180 days from now:

Now = today();
if Now + 90 <= DepartureDate <= Now + 180;

To print the value that is returned by the TODAY function, this example creates a variable that is equal to the value returned by the TODAY function. This step is not necessary but is used here to clarify the program. You can also use the function as part of the program statement.

if today() + 90 <= DepartureDate <= today() + 180;

The following program uses the TODAY function to determine which tours to advertise:

options yearcutoff=1920 pagesize=60 linesize=80 pageno=1 nodate;
data ads;
   set mylib.tourdates;
   Now = today();
   if Now + 90 <= DepartureDate <= Now + 180;
run;

proc print data=ads;
   title 'Tours Departing between 90 and 180 Days from Today';
   format DepartureDate Now date9.;
run;

The following output displays the results:

Using the Current Date as a SAS Date Value

               Tours Departing between 90 and 180 Days from Today              1

                                 Departure
               Obs    Country      Date       Nights          Now

                1      Japan     13MAY2000       8      23NOV1999

Note that the PROC PRINT step contains a FORMAT statement that temporarily assigns the format DATE9. to the variables DepartureDate and Now.

Previous Page | Next Page | Top of Page