Manipulating SAS Date Values with Functions

SAS Date Functions

SAS stores date, time, and datetime values as numeric values. You can use several functions to create these values. For more information about datetime values, see SAS Date, Time, and Datetime 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, 2018). 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)
2018
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 Cert.Temp that contains information about all temporary employees who were hired in November 2017. The data set Cert.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 the new data set include only the observations where the YEAR function extracts a value of 2017 and the MONTH function extracts a value of 11. The value of 11 stands for November.
data work.nov17; 
  set cert.temp; 
  if year(startdate)=2017 and month(startdate)=11; 
run;
When you add a PROC PRINT step to the program, you can view the new data set.
proc print data=work.nov17; 
  format startdate enddate birthdate date9.;
run;
The new data set contains information about only those employees who were hired in November 2017.
Output 14.3 PROC PRINT Output of Work.Nov17 (partial output)
Partial Output of PROC PRINT

Example: Finding the Year

Suppose you want to create a subset of the data set Cert.Temp that contains information about all temporary employees who were hired during a specific year, such as 2016. Cert.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 2016 are placed in the new data set.
data work.temp16; 
  set cert.temp; 
  if year(startdate)=2016; 
run;
When you add a PROC PRINT step to the program, you can view the new data set.
data work.temp16; 
  set cert.temp; 
  where year(startdate)=2016; 
run; 
proc print data=work.temp16; 
  format startdate enddate birthdate date9.;
run;
The new data set contains information for only those employees who were hired in 2016.
Output 14.4 PROC PRINT Output of Work.Temp16 (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 Cert.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 cert.sch; 
  if weekday(airdate)in(1,7); 
run;
proc print data=work.schwkend;
run;
Output 14.5 PROC PRINT Output of 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.

Example: MDY Function

In the data set Cert.Dates, 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 work.datestemp; 
  set cert.dates; 
  Date=mdy(month,day,year); 
run;
proc print data=work.datestemp;
  format date mmddyy10.;
run;
Output 14.6 PROC PRINT Output of Work.Datestemp
PROC PRINT of Work.Datestemp
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 work.datestemp;
  set cert.dates;
  DateCons=mdy(6,17,2018);
run;
proc print data=work.datestemp;
  format DateCons mmddyy10.;
run;
Output 14.7 PROC PRINT Output of Work.Datestemp
PROC PRINT of Work.Datestemp
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 Work.DatesTenp output.

Example: Finding the Date

The data set Cert.Review2018 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 2018.
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 work.review2018 (drop=Day);
  set cert.review2018;
  ReviewDate=mdy(12,day,2018);
run;
proc print data=work.review2018;
  format ReviewDate mmddyy10.;
run;
Output 14.8 PROC PRINT Output of Work.Review2018
The MDY function creates a new variable named ReviewDate in the data set Work.Review2018.
Note: If you specify an invalid date in the MDY function, SAS assigns a missing value to the target variable.
data work.review2018 (drop=Day);
  set cert.review2018;
  ReviewDate=mdy(15,day,2018);
run;
proc print data=work.review2018;
  format ReviewDate mmddyy10.;
run;

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 return values for date and time 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 return values for date and time 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 Cert.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. 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. The output that is created by this PROC PRINT step appears below.
Note: For this example, the SAS date values shown below were created by submitting this program on July 20, 2018.
data work.tempdate; 
  set cert.dates; 
  EditDate=date(); 
run;
proc print data=work.tempdate; 
  format EditDate date9.; 
run;
Output 14.9 PROC PRINT Output of Work.TempDate
PROC PRINT Work.TempDate

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’ , from, to )
  • interval’ specifies a character constant or a variable. Interval can appear in uppercase or lowercase. The value can be one of the following:
    • DAY
    • WEEKDAY
    • WEEK
    • TENDAY
    • SEMIMONTH
    • MONTH
    • QTR
    • SEMIYEAR
    • YEAR
  • from specifies a SAS date, time, or datetime value that identifies the beginning of the time span.
  • to specifies a SAS date, time, or datetime value that identifies the end of the time span.
Note: The type of interval (date, time, or datetime) must match the type of value in from.

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. 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','31dec2017'd,'01jan2018'd);
0
Months=intck('month','31dec2017'd,'01jan2018'd);
1
Years=intck('year','31dec2017'd,'01jan2018'd);
1
Because December 31, 2017, is a Sunday, no WEEK interval is crossed between that day and January 1, 2018. 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, 2016, and June 15, 2018.
Years=intck('year','15jun2016'd,'15jun2018'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','15jun2016'd,'15jun2018'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','01jan2018'd,'31dec2018'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.anniversary; 
  set cert.mechanics(keep=id lastname firstname hired); 
  Years=intck('year',hired,today()); 
  if years=20 and month(hired)=month(today()); 
run;
proc print data=work.anniversary; 
  title '20-Year Anniversaries'; 
run;
The following output is created when the program is run in July 2018.
Output 14.10 PROC PRINT Output of Work.Anniversary
The INTCK function compares the value of the Hired variable and creates the variable Years for those who are having a 20–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 22281, which corresponds to January 1, 2021.
TargetYear=intnx('year','20Jul18'd,3);
Likewise, the following statement assigns the value for the date July 1, 2018, to the variable TargetMonth.
TargetMonth=intnx('semiyear','01Jan18'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','01jan2018'd,5,'b');
21336 (June 1, 2018)
MonthX=intnx('month','01jan2018'd,5,'m');
21350 (June 15, 2018)
MonthX=intnx('month','01jan2018'd,5,'e');
21365 (June 30, 2018)
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
data _null_;
  x=yrdif('16feb2016'd,'16jun2018'd,'30/360');
  put x;
run;
2.3333333333
data _null_;
  x=yrdif('16feb2016'd, '16jun2018'd, 'ACT/ACT');
  put x;
run;
2.3291114604
data _null_;
  x=yrdif('16feb2016'd, '16jun2018'd, 'ACT/360');
  put x;
run;
2.3638888889
data _null_;
  x=yrdif('16feb2016'd, '16jun2018'd, 'ACT/365');
  put x;
run;
2.3315068493
Last updated: August 23, 2018
..................Content has been hidden....................

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