Manipulating SAS Date Values with Functions

SAS Date and Time Values

SAS includes a variety of functions that enable you to work with SAS date values. SAS stores a date value as the number of days from January 1, 1960, to a given date.
Figure 14.4 SAS Date Values
SAS Date Values
A SAS time value is stored as the number of seconds since midnight.
Figure 14.5 SAS Time Values
SAS Time Values
A SAS datetime value is stored as the number of seconds between midnight on January 1, 1960, and a given date and time.
Figure 14.6 SAS Datetime Values
SAS Datetime Values
SAS stores date values as numbers so that you can easily sort the values or perform arithmetic computations. You can use SAS date values as you use any other numeric values.
data test; 
   set hrd.temp; 
   TotDay=enddate-startdate; 
run;
When you execute the program, TotDay has a value of 54 based on the StartDate and EndDate values in the Hrd.Temp data set.
To display SAS date values in a variety of forms, associate a SAS format with the values. The format affects only the way the dates are displayed, not the date values in the data set. For example, the FORMAT statement below associates the DATE9. format with the variables StartDate and EndDate. A portion of the output created by this PROC PRINT step appears below.
proc print data=hrd.temp; 
   format startdate enddate date9.; 
run;
Figure 14.7 HTML Output: PROC PRINT (partial output)
This is a partial output of the PRINT procedure, which displays the SAS date values by associating SAS formats with the values.
SAS date values are valid for dates that are based on the Gregorian calendar from 1582 C.E. through 20,000 C.E.
Figure 14.8 SAS Date Values
SAS Date Values

SAS Date Functions

SAS stores date, time, and datetime values as numeric values. You can use several functions to create these values.
Table 14.4 Typical Use of SAS Date Functions
Function
Example Code
Result
MDY
date=mdy(mon,day,yr);
SAS date
TODAYDATE
now=today();
now=date();
today's date as a SAS date
TIME
curtime=time();
current time as a SAS time
Use other functions to extract months, quarters, days, and years from SAS date values.
Table 14.5 Selected Functions to Use with SAS Date Values
Function
Example Code
Result
DAY
day=day(date);
day of month (1-31)
QTR
quarter=qtr(date);
quarter (1-4)
WEEKDAY
wkday=weekday(date);
day of week (1-7)
MONTH
month=month(date);
month (1-12)
YEAR
yr=year(date);
year (4 digits)
INTCK
x=intck('day',d1,d2);
x=intck('week',d1,d2);
x=intck('month',d1,d2);
x=intck('qtr',d1,d2);
x=intck('year',d1,d2);
days from D1 to D2
weeks from D1 to D2
months from D1 to D2
quarters from D1 to D2
years from D1 to D2
INTNX
x=intnx('interval',
start-from,increment);
date, time, or datetime value
DATDIF
YRDIF
x=datdif(date1,date2,'ACT/ACT');
x=yrdif(date1,date2,'ACT/ACT');
days between date1 and date2
years between date1 and date2

YEAR, QTR, MONTH, and DAY Functions

Overview of YEAR, QTR, MONTH, and DAY Functions

Every SAS date value can be queried for the values of its year, quarter, month, and day. You extract these values by using the functions YEAR, QTR, MONTH, and DAY. They each work the same way.
Syntax, YEAR, QTR, MONTH, and DAY functions:
YEAR(date)
QTR(date)
MONTH(date)
DAY(date)
date is a SAS date value that is specified either as a variable or as a SAS date constant.
The YEAR function returns a four-digit numeric value that represents the year (for example, 2002). The QTR function returns a value of 1, 2, 3, or 4 from a SAS date value to indicate the quarter of the year in which a date value falls. The MONTH function returns a numeric value that ranges from 1 to 12, representing the month of the year. The value 1 represents January, 2 represents February, and so on. The DAY function returns a numeric value from 1 to 31, representing the day of the month.
Table 14.6 Selected Date Functions and Their Uses
Function
Description
Form
Sample Value
YEAR
Extracts the year value from a SAS date value.
YEAR(date)
2002
QTR
Extracts the quarter value from a SAS date value
QTR(date)
1
MONTH
Extracts the month value from a SAS date value.
MONTH(date)
12
DAY
Extracts the day value from a SAS date value
DAY(date)
5

Example: Finding the Year and Month

Suppose you want to create a subset of the data set Hrd.Temp that contains information about all temporary employees who were hired in November 1999. The data set Hrd.Temp contains the beginning and ending dates for staff employment, but there are no month or year variables in the data set. To determine the year in which employees were hired, you can apply the YEAR function to the variable that contains the employee start date, StartDate. Here is a way to write the YEAR function:
year(startdate)
Likewise, to determine the month in which employees were hired, you apply the MONTH function to StartDate.
month(startdate)
To create the new data set, you include these functions in a subsetting IF statement within a DATA step. The subsetting IF statement specifies that the new data set include only observations in which the YEAR function extracts a value of 1999 and the MONTH function extracts a value of 11 (for November).
data hrd.nov99; 
   set hrd.temp; 
   where year(startdate)=1999 and month(startdate)=11; 
run;
When you add a PROC PRINT step to the program, you can view the new data set. Notice that the PROC PRINT step includes a FORMAT statement to display the variables StartDate and EndDate with the DATE9. format.
data hrd.nov99; 
   set hrd.temp; 
   where year(startdate)=1999 and month(startdate)=11; 
proc print data=hrd.nov99; 
   format startdate enddate date9.; 
run;
The new data set contains information about only those employees who were hired in November 1999.
Figure 14.9 HTML Output: PROC PRINT (partial output)
Partial Output of PROC PRINT

Example: Finding the Year

Suppose you want to create a subset of the data set Hrd.Temp that contains information about all temporary employees who were hired during a specific year, such as 1998. Hrd.Temp contains the dates on which employees began work with the company and their ending dates, but there is no year variable.
To determine the year in which employees were hired, you can apply the YEAR function to the variable that contains the employee start date, StartDate. You write the YEAR function as follows:
year(startdate)
To create the new data set, you include this function in a subsetting IF statement within a DATA step. This subsetting IF statement specifies that only observations in which the YEAR function extracts a value of 1998 are placed in the new data set.
data hrd.temp98; 
   set hrd.temp; 
   where year(startdate)=1998; 
run;
When you add a PROC PRINT step to the program, you can view the new data set. Notice that the PROC PRINT step includes a FORMAT statement to display the variables StartDate and EndDate with the DATE9. format.
data hrd.temp98; 
   set hrd.temp; 
   where year(startdate)=1998; 
run; 
proc print data=hrd.temp98; 
   format startdate enddate date9.; 
run;
The new data set contains information for only those employees who were hired in 1998.
Figure 14.10 HTML Output: PROC PRINT (partial output)
Partial Output of PROC PRINT

WEEKDAY Function

Overview of the WEEKDAY Function

The WEEKDAY function enables you to extract the day of the week from a SAS date value.
Syntax, WEEKDAY function:
WEEKDAY(date)
date is a SAS date value that is specified either as a variable or as a SAS date constant.
The WEEKDAY function returns a numeric value from 1 to 7. The values represent the days of the week.
Table 14.7 Values for the WEEKDAY Function
Value
Equals
Day of the Week
1
=
Sunday
2
=
Monday
3
=
Tuesday
4
=
Wednesday
5
=
Thursday
6
=
Friday
7
=
Saturday

Example: WEEKDAY Function

For example, suppose the data set Radio.Sch contains a broadcast schedule. The variable AirDate contains SAS date values. To create a data set that contains only weekend broadcasts, you use the WEEKDAY function in a subsetting IF statement. You include only observations in which the value of AirDate corresponds to a Saturday or Sunday.
data work.schwkend; 
   set sasuser.sch; 
   where weekday(airdate)in(1,7); 
run;
proc print data=work.schwkend;
run;
Output 14.1 HTML Output: Weekday Function
WEEKDAY Function
Note: In the example above, the statement if weekday(airdate) in (1,7); is the same as if weekday(airdate)=7 or weekday(airdate)=1;

MDY Function

Overview of the MDY Function

The MDY function returns a SAS date value from month, day, and year values.
Syntax, MDY function:
MDY (month, day, year)
  • month specifies a numeric constant, variable, or expression that represents an integer from 1 through 12.
  • day specifies a numeric constant, variable, or expression that represents an integer from 1 through 31.
  • year specifies a numeric constant, variable, or expression with a value of a two-digit or four-digit integer that represents that year. The YEARCUTOFF=system option defines the year value from two-digit dates.

Details

In the data set Hrd.Temp, the values for month, day, and year are stored in the numeric variables Month, Day, and Year. It is possible to write the following MDY function to create the SAS date values:
mdy(month,day,year)
To create a new variable to contain the SAS date values, place this function in an assignment statement.
data hrd.newtemp(drop=month day year); 
   set hrd.temp; 
   Date=mdy(month,day,year); 
run;
To display SAS date values in a more readable form, you can associate a SAS format with the values. For example, the FORMAT statement below associates the DATE9. format with the variable Date.
proc print data=hrd.newtemp; 
   format date date9.; 
run;
The MDY function can also add the same SAS date to every observation. This might be useful if you want to compare a fixed beginning date with different end dates. Just use numbers instead of data set variables when providing values to the MDY function.
data hrd.newtemp; 
   set hrd.temp; 
   DateCons=mdy(6,17,2002); 
proc print data=hrd.newtemp; 
   format datecons date9.; 
run;
Figure 14.11 HTML Output: PROC PRINT (partial output)
Partial Output of PROC PRINT Step
Note: The MDY function accepts two-digit values for the year, but SAS interprets two-digit values according to the 100-year span that is set by the YEARCUTOFF= system option. The default value of YEARCUTOFF= is 1926. For details, see SAS Date and Time Values.
The use of four-digit year values in the MDY function is recommended:
  • MDY(5,10,20) = May 10, 1920
  • MDY(5,10,2020) = May 10, 2020
To display the years clearly, format SAS dates with the DATE9. format. This forces the year to appear with four digits, as shown above in the Date and DateCons variables of the Hrd.Newtemp output.

Example: Finding the Date

The data set Dec.Review contains a variable named Day. This variable contains the day of the month for each employee's performance appraisal. The appraisals were all completed in December of 2010.
The following DATA step uses the MDY function to create a new variable named ReviewDate. This variable contains the SAS date value for the date of each performance appraisal.
data sasuser.review2010; 
   set sasuser.review; 
   ReviewDate=mdy(12,day,2010); 
run;
proc print data=sasuser.review2010 noobs;
run;
Figure 14.12 SAS Data Set Dec.Review2010
The MDY function creates a new variable named ReviewDate in the data set Dec.Review2010.
Note: If you specify an invalid date in the MDY function, SAS assigns a missing value to the target variable.
data dec.review2010; 
   set dec.review; 
   ReviewDate=mdy(15,day,2010); 
run;
Figure 14.13 SAS Data Set Dec.Review2010
The MDY function creates a new variable named ReviewDate in the data set Dec.Review2010 and SAS assigns a missing value if there is an invalid date in the MDY function.

DATE and TODAY Functions

Overview of the DATE Function

The DATE function returns the current date as a numeric SAS date value.
Note: If the value of the TIMEZONE= system option is set to a time zone name or time zone ID, the date and time values that are returned for this function are determined by the time zone.
Syntax, DATE function:
DATE ()
The DATE function does not require any arguments, but it must be followed by parentheses.
The DATE function produces the current date in the form of a SAS date value, which is the number of days since January 1, 1960.

Overview of the TODAY Function

The TODAY function returns the current date as a numeric SAS date value.
Note: If the value of the TIMEZONE= system option is set to a time zone name or time zone ID, the date and time values that are returned for this function are determined by the time zone.
Syntax, TODAY function:
TODAY ()
The TODAY function does not require any arguments, but it must be followed by parentheses.
The TODAY function produces the current date in the form of a SAS date value, which is the number of days since January 1, 1960.

Example: The DATE and TODAY Functions

The DATE and TODAY functions have the same form and can be used interchangeably. To add a new variable, which contains the current date, to the data set Hrd.Temp. To create this variable, write an assignment statement such as the following:
EditDate=date();
After this statement is added to a DATA step and the step is submitted, the data set that contains EditDate is created.
data hrd.newtemp; 
   set hrd.temp; 
   EditDate=date(); 
run;
Note: For this example, the SAS date values shown below were created by submitting this program on June 29, 2017.
To display these SAS date values in a different form, you can associate a SAS format with the values. For example, the FORMAT statement below associates the DATE9. format with the variable EditDate. A portion of the output that is created by this PROC PRINT step appears below.
proc print data=hrd.newtemp; 
   format editdate date9.; 
run;
Figure 14.14 SAS Output: SAS Data Set Hrd.Newtemp (partial output)
The FORMAT statement formats the variable EditDate with DATE9 format.

INTCK Function

Overview of the INTCK Function

The INTCK function returns the number of interval boundaries of a given kind that lie between two dates, times, or datetime values. You can use it to count the passage of days, weeks, months, and so on.
Syntax, INTCK function:
INTCK (interval <multiple> <.shift-index>, start-date, end-date, <‘method’>)
INTCK(custom-interval, start-date, end-date, <‘method’>)
  • interval specifies a character constant, a variable, or an expression that contains an interval name. Interval can appear in uppercase or lowercase. The type of interval (date, datetime, or time) must match the type of value in start-date. Multipliers and shift indexes can be used with the basic interval names to construct more complex interval specifications.
    • interval<multiple.shift-index> the parts of the interval name are listed below:
      • interval specifies the name of the basic interval type. For example, YEAR specifies yearly intervals.
      • multiple specifies an optional multiplier that sets the interval equal to a multiple of the period of the basic interval type. For example, the interval YEAR2 consists of two-year, or biennial, periods.
      • custom-interval specifies a user-defined interval that is defined by a SAS data set. Each observation contains two variables, begin and end.
      • shift-index specifies an optional shift index that shifts the interval to start at a specified subperiod starting point. For example, YEAR.3 specifies yearly periods shifted to start on the first of March of each calendar year and to end in February of the following year.
        Note: The shift index cannot be greater than the number of subperiods in the entire interval. For example, you could use YEAR2.24, but YEAR2.25 would be an error because there is no 25th month in a two-year interval. If the default shift period is the same as the interval type, then only multiperiod intervals can be shifted with the optional shift index. For example, MONTH type intervals shift by MONTH subperiods by default. Thus, monthly intervals cannot be shifted with the shift index. However, bimonthly intervals can be shifted with the shift index, because there are two MONTH intervals in each MONTH2 interval. For example, the interval name MONTH2.2 specifies bimonthly periods starting on the first day of even-numbered months.
  • start-date specifies a SAS expression that represents the starting SAS date, time, or datetime value.
  • end-date specifies a SAS expression that represents the ending SAS date, time, or datetime value.
  • <‘method’> specifies that intervals are counted using either a discrete or a continuous method. You must enclose method in quotation marks. Method can be one of these values:
    • CONTINUOUS specifies that continuous time is measured. The interval is shifted based on the starting date.
    • DISCRETE specifies that discrete time is measured. The discrete method counts interval boundaries (for example, end of month).

Details

The INTCK function counts intervals from fixed interval beginnings, not in multiples of an interval unit from the from value. Partial intervals are not counted. Intervals can be one of the following values:
  • DAY
  • MONTH
  • QTR
  • SEMIMONTH
  • SEMIYEAR
  • TENDAY
  • WEEK
  • WEEKDAY
  • YEAR
For example, WEEK intervals are counted by Sundays rather than seven-day multiples from the from argument. MONTH intervals are counted by day 1 of each month, and YEAR intervals are counted from 01JAN, not in 365-day multiples.
Consider the results in the following table. The values that are assigned to the variables Weeks, Months, and Years are based on consecutive days.
Table 14.8 Examples of SAS Statements and Their Values
Example Code
Value
Weeks=intck('week','31dec2000'd,'01jan2001'd);
0
Months=intck('month','31dec2000'd,'01jan2001'd);
1
Years=intck('year','31dec2000'd,'01jan2001'd);
1
Because December 31, 2000, is a Sunday, no WEEK interval is crossed between that day and January 1, 2001. However, both MONTH and YEAR intervals are crossed.

Examples: INTCK Function

The following statement creates the variable Years and assigns it a value of 2. The INTCK function determines that two years have elapsed between June 15, 1999, and June 15, 2001.
Years=intck('year','15jun1999'd,'15jun2001'd);
Note: As shown here, the from and to dates are often specified as date constants.
Likewise, the following statement assigns the value 24 to the variable Months.
Months=intck('month','15jun1999'd,'15jun2001'd);
However, the following statement assigns 0 to the variable Years, even though 364 days have elapsed. In this case, the YEAR boundary (01JAN) is not crossed.
Years=intck('year','01jan2002'd,'31dec2002'd);

Example: The INTCK Function and Periodic Events

A common use of the INTCK function is to identify periodic events such as due dates and anniversaries.
The following program identifies mechanics whose 20th year of employment occurs in the current month. It uses the INTCK function to compare the value of the variable Hired to the date on which the program is run.
data work.anniv30; 
       set flights.mechanics(keep=id lastname firstname hired); 
   Years=intck('year',hired,today()); 
   if years=30 and month(hired)=month(today()); 
run;
proc print data=work.anniv30; 
   title '30-Year Anniversaries This Month'; 
run;
The following output is created when the program is run in June 2017.
Figure 14.15 30–Year Anniversaries This Month
The INTCK function compares the value of the Hired variable and creates the variable Years for those who are having a 30–year anniversaries this month.

INTNX Function

Overview of the INTNX Function

The INTNX function is similar to the INTCK function. The INTNX function applies multiples of a given interval to a date, time, or datetime value and returns the resulting value. You can use the INTNX function to identify past or future days, weeks, months, and so on.
Syntax, INTNX function:
INTNX('interval',start-from,increment<,'alignment'>)
  • 'interval' specifies a character constant or variable.
  • start-from specifies a starting SAS date, time, or datetime value.
  • increment specifies a negative or positive integer that represents time intervals toward the past or future.
  • 'alignment' (optional) forces the alignment of the returned date to the beginning, middle, or end of the interval.
Note: The type of interval (date, time, or datetime) must match the type of value in start-from and increment.

Details

When you specify date intervals, the value of the character constant or variable that is used in interval can be one of the following:
  • DATETIME
  • DAY
  • QTR
  • MONTH
  • SEMIMONTH
  • SEMIYEAR
  • TENDAY
  • TIME
  • WEEK
  • WEEKDAY
  • YEAR

Example: INTNX Function

For example, the following statement creates the variable TargetYear and assigns it a SAS date value of 13515, which corresponds to January 1, 1997.
TargetYear=intnx('year','05feb94'd,3);
Likewise, the following statement assigns the value for the date July 1, 2001, to the variable TargetMonth.
TargetMonth=intnx('semiyear','01jan2001'd,1);
SAS date values are based on the number of days since January 1, 1960. Yet the INTNX function can use intervals of weeks, months, years, and so on.
The purpose of the optional alignment argument is to specify whether the returned value should be at the beginning, middle, or end of the interval. When specifying date alignment in the INTNX function, use the following values or their corresponding aliases:
  • BEGINNING Alias: B
  • MIDDLE Alias: M
  • END Alias: E
  • SAME Alias: SAMEDAY or S
The best way to understand the alignment argument is to see its effect on identical statements. The following table shows the results of three INTNX statements that differ only in the value of alignment.
Table 14.9 Alignment Values for the INTNX Function
Example Code
Date Value
MonthX=intnx('month','01jan1995'd,5,'b');
12935 (June 1, 1995)
MonthX=intnx('month','01jan1995'd,5,'m');
12949 (June 15, 1995)
MonthX=intnx('month','01jan1995'd,5,'e');
12964 (June 30, 1995)
These INTNX statements count five months from January, but the returned value depends on whether alignment specifies the beginning, middle, or end day of the resulting month. If alignment is not specified, the beginning day is returned by default.

DATDIF and YRDIF Functions

The DATDIF and YRDIF functions calculate the difference in days and years between two SAS dates, respectively. Both functions accept start dates and end dates that are specified as SAS date values. Also, both functions use a basis argument that describes how SAS calculates the date difference.
Syntax, DATDIF, and YRDIF functions:
DATDIF(start_date,end_date,basis))
YRDIF(start_date,end_date,basis))
  • start_date specifies the starting date as a SAS date value.
  • end_date specifies the ending date as a SAS date value.
  • basis specifies a character constant or variable that describes how SAS calculates the date difference.
There are two character strings that are valid for basis in the DATDIF function, and four character strings that are valid for basis in the YRDIF function. These character strings and their meanings are listed in the table below.
Table 14.10 Character Strings in the DATDIF Function
Character String
Meaning
Valid in DATDIF
Valid in YRDIF
'30/360'
specifies a 30-day month and a 360-day year
yes
yes
'ACT/ACT'
uses the actual number of days or years between dates
yes
yes
'ACT/360'
uses the actual number of days between dates in calculating the number of years (calculated by the number of days divided by 360)
no
yes
'ACT/365'
uses the actual number of days between dates in calculating the number of years (calculated by the number of days divided by 365)
no
yes
The best way to understand the different options for the basis argument is to see the different effects that they have on the value that the function returns. The table below lists four YRDIF functions that use the same start date and end date. Each function uses one of the possible values for basis, and each one returns a different value.
Table 14.11 Examples of the YRDIF Function
Example Code
Returned Value
yrdif('16oct1998'd,'16feb2003'd,'30/360')
4.333333333
yrdif('16oct1998'd,'16feb2003'd,'ACT/ACT')
4.3369863014
yrdif('16oct1998'd,'16feb2003'd,'ACT/360')
4.4
yrdif('16oct1998'd,'16feb2003'd,'ACT/365')
4.3397260274
Last updated: January 10, 2018
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.61.170