Section 7.4: Working with SAS Date and Time Variables SAS date and time constants help make your programs run better and in many cases more correctly. First note that if you have a choice, entering dates as character values such as "20050102" is asking for trouble for many reasons; and as you will see it makes writing a set of commands excessively difficult. What you should NEVER do with a date variable is to apply it as observed in this statement in a DATA step: release_date =11/11/2004; The result stored in release_date does not specify a valid SAS date. Rather, SAS interprets it as repeated division: 11/11 yields 1 followed by 1/2004 which yields a value much less than 1 which is essentially 0. If you assign release_date to a SAS date format, you may be puzzled why SAS interprets it to be January 1, 1960. SAS stores dates (day/month/hear) as integers which count the number of days since January 1, 1960. The 0 point, 01/01/1960, was the standard for the 'epoch' used in date/time programming on mainframes namely from the IBM influence back in the 1970s when SAS began. To read why SAS chose this date go to: http://support.sas.com/news/insider/dates.html Date values are comparisons with this arbitrary 0 point; negative numbers correspond to dates prior to 1960. Its essential purpose is to provide ways to work with date values easily. The SAS date constant '02jan2005'd converts what looks like a character string of characters into an integer. DATA _null_; dt ='02jan2005'd ; put dt; run; The variable dt equals 16438, the number of days since 1/1/1960. When entering dates in a DATA step, there are different statements which do it including: release1="11/11/2004"d; release2=mdy(11,11,2004); release3=INPUT('11/11/2004',mmddyy10.) With any of these statements (and date values in general) you should include a FORMAT statement which only affects the visual display of the numerical value so the value stored in SAS prints as an actual date. FORMAT release1 release2 release3 mmddyy10. ; SAS times are the number of seconds since midnight. Your numbers may not be giving you the times you think. Use SAS time constants instead. Write ten-thirty am as '10:30:00am't instead so that you can fiddle with seconds. DATE FUNCTIONS The DATE or the TODAY function returns today’s date from the system clock and requires no arguments. The () are still required to distinguish it from a variable name. DATA new; X1=DATE(); X2=TODAY(); PROC PRINT DATE=new NOobs; VAR x1 x2; FORMAT x1 x2 mmddyy10.; RUN; prints: X1 X2 05/13/2003 05/13/2003 Convert a character date value into a SAS formatted date variable: DATA _null_ ; FORMAT numdate date7. numtime time5. ; chardate = "08mar2003" ; chartime = "1740" ; numdate = INPUT(chardate,date9.) ; numtime = INPUT(substr(chartime,1,2)||":"||substr(chartime,3,2),time.) ; PUT numdate= numtime= ; run; NUMDATE=08MAR03 NUMTIME=17:40 Use the DATEPART function when you want to extract the year, month, and/or day values of a date variable stored with a datetime format such as datetime22.3. DATA _null_; FORMAT mth_dy_yr mmddyy10. ; dt='23oct1954:07:20:08.003'dt; mth_dy_yr=DATEPART(dt); year=YEAR(DATEPART(dt)); month=MONTH(DATEPART(dt)); day=DAY(DATEPART(dt)); PUT mth_dy_yr= year= month= day=; RUN; In the log file you will find: mth_dy_yr=10/23/1954 year=1954 month=10 day=23 In rare situations you may need to convert a SAS date into an 8 digit character equivalent: DATA ooo; FORMAT dt mmddyy10.; * format a positive integer as a SAS date; dt=today(); * SAS function for today's date; char_date=put(dt,yymmddn8.); * 8-digit character value as printed below; proc print; run; char_ Obs dt date 1 05/15/2006 20060515