Previous Page | Next Page

Working with Dates in the SAS System

Entering Dates


Understanding Informats for Date Values

In order for SAS to read a value as a SAS date value, you must give it a set of directions called an informat. By default, SAS reads numeric variables with a standard numeric informat that does not include letters or special characters. When a field that contains data does not match the standard patterns, you specify the appropriate informat in the INPUT statement.

SAS provides many informats. Four informats that are commonly used to read date values are:

MMDDYY8.

reads dates written as mm/dd/yy.

MMDDYY10.

reads dates written as mm/dd/yyyy.

DATE7.

reads dates in the form ddMMMyy.

DATE9.

reads dates in the form ddMMMyyyy.

Note that each informat name ends with a period and contains a width specification that tells SAS how many columns to read.

Reading a Date Value

To create a SAS data set for the Tradewinds Travel data, the DATE9. informat is used in the INPUT statement to read the variable DepartureDate.

input Country $ 1-11 @13 DepartureDate date9. Nights;

Using an informat in the INPUT statement is called formatted input. The formatted input in this example contains the following items:

The following DATA step creates MYLIB.TOURDATES using the DATE9. informat to create SAS date values:

options yearcutoff=1920 pagesize=60 linesize=80 pageno=1 nodate;
libname mylib 'permanent-data-library';

data mylib.tourdates;
   infile 'input-file';
   input Country $ 1-11 @13 DepartureDate date9. Nights;
run;

proc print data=mylib.tourdates;
   title 'Tour Departure Dates as SAS Date Values';
run;

The following output displays the results:

Creating SAS Date Values from Calendar Dates

                    Tour Departure Dates as SAS Date Values                    1

                                         Departure
                   Obs    Country           Date      Nights

                     1    Japan            14743         8  
                     2    Greece           14534        12  
                     3    New Zealand      15009        16  
                     4    Brazil           15034         8  
                     5    Venezuela        14924         9  
                     6    Italy            15090         8  
                     7    Russia           13668        14  
                     8    Switzerland      14989         9  
                     9    Australia        14176        12  
                    10    Ireland          14849         7  

Compare the SAS values of the variable DepartureDate with the values of the raw data shown in the previous section. The data set MYLIB.TOURDATES shows that SAS read the departure dates and created SAS date values. Now you need a way to display the dates in a recognizable form.


Using Good Programming Practices to Read Dates

When reading dates, it is good programming practice to always use the DATE9. or MMDDYY10. informats to be sure that the data is read correctly. If you use the DATE7. or MMDDYY8. informat, then SAS reads only the first two digits of the year. If the data contains four-digit years, then SAS reads the century and not the year.

Consider the Tradewinds Travel external file with both two-digit years and four-digit years:

Japan       13may2000  8
Greece      17oct99   12
New Zealand 03feb2001 16
Brazil      28feb2001  8
Venezuela   10nov00    9
Italy       25apr2001  8
USSR        03jun1997 14
Switzerland 14jan2001  9
Australia   24oct98   12
Ireland     27aug2000  7

The following DATA step creates a SAS data set MYLIB.TOURDATES7 by using the DATE7. informat:

options yearcutoff=1920 pagesize=60 linesize=80 pageno=1 nodate;

data mylib.tourdates7;
   infile 'input-file';
   input Country $ 1-11 @13 DepartureDate date7. Nights;
run;

proc print data=mylib.tourdates7;
   title 'Tour Departure Dates Using the DATE7. Informat';
   title2 'Displayed as Two-Digit Calendar Dates';
   format DepartureDate date7.;
run;

proc print data=mylib.tourdates7;
   title 'Tour Departure Dates Using the DATE7. Informat';
   title2 'Displayed as Four-Digit Calendar Dates'; 
   format DepartureDate date9.;
run;

The PRINT procedures format DepartureDate using two-digit year (DATE7.) and four-digit year (DATE9.) calendar dates. The following output displays the results:

Using the Wrong Informat Can Produce Invalid SAS Data Sets

                 Tour Departure Dates Using the DATE7. Informat                1
                     Displayed as Two-Digit Calendar Dates

                                         Departure
                   Obs    Country          Date       Nights
                                              1          2 
                     1    Japan           13MAY20        0  
                     2    Greece          17OCT99       12  
                     3    New Zealand     03FEB20        1  
                     4    Brazil          28FEB20        1  
                     5    Venezuela       10NOV00        9  
                     6    Italy           25APR20        1  
                     7    Russia          03JUN19       97  
                     8    Switzerland     14JAN20        1  
                     9    Australia       24OCT98       12  
                    10    Ireland         27AUG20        0  
                 Tour Departure Dates Using the DATE7. Informat                2
                     Displayed as Four-Digit Calendar Dates

                                         Departure
                   Obs    Country          Date       Nights
                                               3 
                     1    Japan          13MAY1920       0  
                     2    Greece         17OCT1999      12  
                     3    New Zealand    03FEB1920       1  
                     4    Brazil         28FEB1920       1  
                     5    Venezuela      10NOV2000       9  
                     6    Italy          25APR1920       1  
                     7    Russia         03JUN2019      97  
                     8    Switzerland    14JAN1920       1  
                     9    Australia      24OCT1998      12  
                    10    Ireland        27AUG1920       0  

Notice that the four-digit years in the input file do not match the years in MYLIB.TOURDATES7 for observations 1, 3, 4, 6, 7, 8, and 10:

[1] SAS stopped reading the date after seven characters; it read the first two digits, the century, and not the complete four-digit year.

[2] To read the data for the next variable, SAS moved the pointer one column and read the next two numeric characters (the years 00, 01, and 97) as the value for the variable Nights. The data for Nights in the input file was ignored.

[3] When the dates were formatted for four-digit calendar dates, SAS used the YEARCUTOFF= 1920 system option to determine the century for the two-digit year. What was originally 1997 in observation 7 became 2019, and what was originally 2000 and 2001 in observations 1, 3, 4, 6, 8, and 10 became 1920.


Using Dates as Constants

If the tour of Switzerland leaves on January 21, 2001 instead of January 14, then you can use the following assignment statement to make the update:

if Country = 'Switzerland' then DepartureDate = '21jan2001'd;

The value '21jan2001'D is a SAS date constant. To write a SAS date constant, enclose a date in quotation marks in the standard SAS form ddMMMyyyy and immediately follow the final quotation mark with the letter D. The D suffix tells SAS to convert the calendar date to a SAS date value. The following DATA step includes the use of the SAS date constant:

options pagesize=60 linesize=80 pageno=1 nodate;
data correctdates;
   set mylib.tourdates;
   if Country = 'Switzerland' then DepartureDate = '21jan2001'd;
run;

proc print data=correctdates;
   title 'Corrected Departure Date for Switzerland';
   format DepartureDate date9.;
run;

The following output displays the results:

Changing a Date by Using a SAS Date Constant

                    Corrected Departure Date for Switzerland                   1

                                         Departure
                   Obs    Country          Date       Nights

                     1    Japan          13MAY2000       8  
                     2    Greece         17OCT1999      12  
                     3    New Zealand    03FEB2001      16  
                     4    Brazil         28FEB2001       8  
                     5    Venezuela      10NOV2000       9  
                     6    Italy          25APR2001       8  
                     7    Russia         03JUN1997      14  
                     8    Switzerland    21JAN2001       9  
                     9    Australia      24OCT1998      12  
                    10    Ireland        27AUG2000       7  

Previous Page | Next Page | Top of Page