Previous Page | Next Page

Working with Dates in the SAS System

Comparing Durations and SAS Date Values

You can use SAS date values to find the units of time between dates. Tradewinds Travel was founded on February 8, 1982. On November 23, 1999, you decide to find out how old Tradewinds Travel is, and you write the following program:

options yearcutoff=1920 pagesize=60 linesize=80 pageno=1 nodate;
   /* Calculating a duration in days */
data ttage;
   Start = '08feb82'd;
   RightNow = today();
   Age = RightNow - Start;
   format Start RightNow date9.;
run;

proc print data=ttage;
   title 'Age of Tradewinds Travel';
run;

Calculating a Duration in Days

                            Age of Tradewinds Travel                           1

                     Obs        Start     RightNow     Age

                      1     08FEB1982    23NOV1999    6497

The value of Age is 6497, a number that looks like an unformatted SAS date value. However, Age is actually the difference between February 8, 1982, and November 23, 1999, and represents a duration in days, not a SAS date value. To make the value of Age more understandable, divide the number of days by 365 (more precisely, 365.25) to produce a duration in years. The following DATA step calculates the age of Tradewinds Travel in years:

options yearcutoff=1920 pagesize=60 linesize=80 pageno=1 nodate;
   /* Calculating a duration in years */
data ttage2;
   Start = '08feb82'd;
   RightNow = today();
   AgeInDays = RightNow - Start;
   AgeInYears = AgeInDays / 365.25;
   format AgeInYears 4.1 Start RightNow date9.;
run;

proc print data=ttage2;
   title 'Age in Years of Tradewinds Travel';
run;

The following output displays the results:

Calculating a Duration in Years

                       Age in Years of Tradewinds Travel                       1

                                                  Age      Age
                                                   In      In
                Obs        Start     RightNow     Days    Years

                 1     08FEB1982    23NOV1999     6497    17.8 

To show a portion of a year, the value for AgeInYears is assigned a numeric format of 4.1 in the FORMAT statement of the DATA step. The 4 tells SAS that the number contains up to four characters. The 1 tells SAS that the number includes one digit after the decimal point.

Previous Page | Next Page | Top of Page