SAS Dates....!


                                   Hey, here the another post about SAS dates. SAS normally stores the date into a single number—the number of days from January 1, 1960. Dates after January 1, 1960, are positive integers; dates before January 1, 1960, are negative integers. The below are the some of the example how SAS stores the data.
December 31, 1959    -1

       June 15, 2006             16,967
       October 21, 1950        -3,359
                                   To avoid these we can use SAS date format  to display the date correctly in the output. There are few Date formats which we can used based on your need. Two very popular date formats are DATE9. and MMDDYY10. Also there is some difference in reading two digit year and four digit year in SAS. SAS can read four digit year flawlessly but for two year you have to specify the YEARCUTOFF option.The default value for this option is 1920 in SAS 8 and SAS®9. This value determines the start of a 100-year interval that SAS uses when it encounters a two-digit year. Now let us do some problems in the dates.

#Problem:

                                  The below data contains three dates, the first two in the form mm/dd/yyyy
descenders and the last in the form ddmmmyyyy. We have to name the three date variables
Date1, Date2, and Date3. Also we have to format all three using the MMDDYY10. format. Also we can see how to include the number of years from Date1 to Date2 (Year12) and the number of
years from Date2 to Date3 (Year23) and finally we round these years to the nearest number.

01/03/1950 01/03/1960 03Jan1970
05/15/2000 05/15/2002 15May2003
10/10/1998 11/12/2000 25Dec2005

# Solution:

data A15028.A28_dates;
* using the date informats;
input Date1 : mmddyy10.
Date2 : mmddyy10.
Date3 : date9.;
*Using yrdif function to compute number of years between two dates;
*By giving actual we get the exact number of days;
Year1-2 = round(yrdif(Date1,Date2,'Actual'));
Year2-3 = round(yrdif(Date2,Date3,'Actual'));
format Date1-Date3 mmddyy10.;
datalines;
01/03/1950 01/03/1960 03Jan1970
05/15/2000 05/15/2002 15May2003
10/10/1998 11/12/2000 25Dec2005
;
Proc Print data=A15028.A28_threedates;
run;


#Output;


# Learning and reading the data from the output:                

                         In the above program we used two date informats (mmddyy10. & date9.). This tell SAS that how we read the data from the datalines. Don't forget to use . after the format. Then we use YRDIF function which is help to calculate the difference between the two dates and ACTUAL helps to get the exact difference which include the leap year. Finally we use formats which is different from informats. In formats we tell SAS how to write the data in output.


No comments:

Post a Comment