SAS:DATE TIME FUNCTIONS

IS THIS A GOOD TIME..?


FUNCTIONS :
1.       DATE 
2.       WEEKDAY 
3.       QTR 
4.       DHMS
5.       MONTH 
6.       DATEPART 
7.       HMS 
8.       INTCK 
9.       INTFMT 
10.    YEAR
11.    TIMEPART 
12.    HOUR
13.    INTNX 
14.    TODAY
15.    YRDIF 
16.    MDY
17.    DAY 
18.    DATDIF
19.    MINUTE
SAS - Date Time Functions

1.DATE 

Information:
Any variable assigned to this function returns today's date as a numeric value.
Category: Date Functions
Usage:
y = date();

Result: 17954
Additional Info:Can be interchangeably used for Today function.
-----------------------------------------------------------------------------------------------------------------------
2.WEEKDAY 
Information:
It produces an integer that represents the day of the week, where 1=Sunday, 2=Monday, ..., 7=Saturday
Category: Date Functions
Usage:
x= weekday('16mar97'd);
put x;

Result: 1
Additional Info:A weekday function can be used only on Dates and not Datetimes
-----------------------------------------------------------------------------------------------------------------------------------------
3.QTR 
Information:
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
Category: Date Functions
Usage:
x='20jan94'd;
y=qtr(x);put y=;

Result: y=1
Additional Info:


------------------------------------------------------------------------------------------------------------------------

4.DHMS
Information:
Date variables can be combined with time variables to create datetime variables with the DHMS function. It returns a numeric value which can be positive or negative. It is normally used to convert a character datetime value to a SAS datetime variable.
Category: Date and Time Functions
Syntax: DHMS(date,hour,minute,second)
Usage:
dtime=dhms('01jan03'd,15,30,15);
put dtime;
put dtime datetime.;

Result:
1357054215
01JAN03:15:30:15

Additional Info:• If the original variable is in raw Datetime format, it has to be split into separate date and time values to create a SAS date and SAS time values before passing these to the DHMS function
• The date and time portions are obtained using substr function along with the index function used to find the blank between the date and time. Using input function they are converted to mmddyy10. and time8. format respectively. These are then passed into the DHMS function to obtain a new datetime value with the DATETIME19. format.
o Date: input(substr(dtime,1,blankpos-1), mmddyy10.)
o Time: input(substr(dtime,blankpos+1), time8.)
• If you have a time value , you do not have to separate out the hours, minutes and seconds. You can use 0 for the hours and minutes, and use the time variable for the seconds value.
-------------------------------------------------------------------------------------------------------
5.MONTH 
Information:The MONTH function takes a SAS date value as an argument. The MONTH function returns a numeric value that represents the month from a SAS date value.
Category: Date and Time Functions
Syntax: MONTH (SAS date value);
Usage:
a=MONTH (‘01FEB2000'D);
Put a;
Result: 2
Additional Info:
Numeric values can range from 1 through 12. 

------------------------------------------------------------------------------------------------------------------------
6. DATEPART 
Information:
The DATEPART function takes a SAS date time value as an argument. The DATEPART function computes the SAS date value for the date part of a SAS date time value.
Category:
Syntax:
Usage:
a=DATEPART (‘01FEB94:8:45:'Dt);
Put a worddate;
Result: February 1, 1994.
------------------------------------------------------------------------------------------------------------------------
7.HMS 
Information:
The HMS function takes hour, minute and second as argument. The HMS function returns a SAS time value that from hour, minute and second values. Category: Date and Time Functions Syntax: HMS (hour, minute, second); Usage:
a=HMS (12, 45, 10);
Put a;
Result: 12:45:10

Additional Info:
Hour, Minute and Second values should be in numeric. 


------------------------------------------------------------------------------------------------------------------------
8.INTCK 
Information:
The INTCK function takes interval, from and to as argument. The INTCK function returns the integer count of the number of interval boundaries between two dates, two times or date time.
Category: Date and Time Functions
Syntax: INTCK (‘interval’, from, two); Interval - Specifies a character constant, a variable, or an expression that contains an interval name such as WEEK, MONTH, or QTR. Interval can appear in uppercase or lowercase. From - Specifies a SAS expression that represents the starting SAS date, time, or datetime value. To - Specifies a SAS expression that represents the ending SAS date, time, or datetime value. Usage:
a=INTCK (‘QTR’,’10JAN95’D, ‘01JUL95’d);
Put a;
Result: 2

Additional Info:
The INTCK function counts the number of times the beginning of an interval is reached in moving from the from argument to the to argument. It does not count the number of complete intervals between those two values.
--------------------------------------------------------------------------------------------------------
9.INTFMT 
Information:
The INTFMT function takes interval and size as argument. The INTFMT function returns a recommended SAS format when a datetime interval is specified.
Category: Date and Time Functions
Syntax: INTFMT (‘interval’, ‘size’); Interval - Specifies a character constant, a variable, or an expression that contains an interval name such as WEEK, MONTH, or QTR. Interval can appear in uppercase or lowercase. Size - Specifies either LONG or SHORT. When a format includes a year value, LONG or L specifies a format that uses a four-digit year. SHORT or S specifies a format that uses a two-digit year. Usage:
a=INTFMT (‘QTR’, ’S’);
Put a;
Result: MONYY7.
Additional Info:
The INTFMT function returns a recommended format depending on a date, time, or datetime interval for displaying the time ID values that are associated with a time series of a given interval. The valid values of SIZE (LONG, L, SHORT, or S) specify whether to use a two-digit or a four-digit year when the format refers to a SAS date value. For more information about working with date and time intervals
--------------------------------------------------------------------------------------------------------
10.YEAR
Information:
The YEAR function takes an argument that represents a SAS date value and produces a four-digit numeric value that represents the year. The argument can be a variable consisting SAS date value, or it can be a constant (the number would represent the number of days since 1960). Category: Date/Time Functions Syntax: YEAR(argument) Usage:
1) date='25dec97'd;
y=year(date);
put y;
Result: y = 1997

2) data _null_;
Y = year(today());
Put y;
Run;
Result: y = 2009 (current year)
Info: The argument is mandatory.
-------------------------------------------------------------------------------------------------------------------------
11.TIMEPART 
Information:
The TIMEPART function takes an argument that represents a SAS date/time value and outputs the corresponding SAS value to the input time. The argument is a variable consisting SAS date value. Category: Date/Time Functions Syntax: TIMEPART(datetime) Usage:
datim=datetime();
time=timepart(datim);
put y;

--------------------------------------------------------------------------------------------------------
12.HOUR
Information:
The HOUR function returns a numeric value that represents the hour from a SAS time or datetime value. Numeric values can range from 0 through 23. HOUR always returns a positive number. The argument is time/datetime. Time/Datetime is a numeric constant, variable, or expression that specifies a SAS time/datetime value. Category: Date/Time Functions Syntax: HOUR(
now='1:30't;
h=hour(now);
put h;
RESULT: h = 1

-------------------------------------------------------------------------------------------------------
13.INTNX 
Information:
The INTNX function increments a date, time, or datetime value by intervals such as DAY, WEEK, QTR, and MINUTE. The increment is based on a starting date, time, or datetime value, and on the number of time intervals that you specify.
The INTNX function returns the SAS date value for the beginning date, time, or datetime value of the interval that you specify in the start-from argument. (To convert the SAS date value to a calendar date, use any valid SAS date format, such as the DATE9. format.)
Category: Date/Time functions
Syntax: INTNX(interval<.shift-index>, start-from, increment<,alignment>)
Arguments:
  • Interval - specifies a character constant, variable, or expression that contains a time interval such as WEEK, SEMIYEAR, QTR, or HOUR. Interval can appear in uppercase or lowercase.
  • Interval - Here are the three parts of the interval name:
  • 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.
  • 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.
  • Start-From - specifies a SAS expression that represents a SAS date, time, or datetime value that identifies a starting point.
  • Increment - specifies a negative, positive, or zero integer that represents the number of date, time, or datetime intervals. Increment is the number of intervals to shift the value of start-from.
  • Alignment - controls the position of SAS dates within the interval. Alignment can be one of these values: BEGINNING, MIDDLE, END, SAME
Usage: The following example shows how to determine the date of the start of the week that is six weeks from the week of October 17, 2003.
x=intnx('week', '17oct03'd, 6);
put x date9.;
RESULT: x = 23NOV2003.

------------------------------------------------------------------------------------------------------
14.TODAY
Information:
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. Category: Date/Time Functions Syntax: TODAY () Usage:
 data _null_;
d = today();
put d date9.;
RESULT: d = current date 

---------------------------------------------------------------------------------------

15.YRDIF 
Information:
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.
Category: Date/Time Functions
Syntax: YRDIF(sdate,edate,basis)
Arguments
Sdate - specifies a SAS date value that identifies the starting date.
Edate - specifies a SAS date value that identifies the ending date.
Basis - identifies a character constant or variable that describes how SAS calculates the date difference. The following character strings are valid:
'30/360'
specifies a 30-day month and a 360-day year in calculating the number of years. Each month is considered to have 30 days, and each year 360 days, regardless of the actual number of days in each month or year.
'ACT/ACT'
uses the actual number of days between dates in calculating the number of years. SAS calculates this value as the number of days that fall in 365-day years divided by 365 plus the number of days that fall in 366-day years divided by 366.
'ACT/360'
uses the actual number of days between dates in calculating the number of years. SAS calculates this value as the number of days divided by 360, regardless of the actual number of days in each year.
'ACT/365'
uses the actual number of days between dates in calculating the number of years. SAS calculates this value as the number of days divided by 365, regardless of the actual number of days in each year.
Usage:
data _null_;
sdate='16oct1998'd;
edate='16feb2003'd;
y30360=yrdif(sdate, edate, '30/360');
yactact=yrdif(sdate, edate, 'ACT/ACT');
yact360=yrdif(sdate, edate, 'ACT/360');
yact365=yrdif(sdate, edate, 'ACT/365');
put y30360= yactact= yact360= yact365=;
run;

y30360= 4.333333333
yactact= 4.3369863014
yact360= 4.4
yact365= 4.3397260274

--------------------------------------------------------------------------------------------------------
16.MDY
Information: The MDY function takes three numeric constants, variables or expressions representing the month, day and year and returns a date value comprising of the month, day and year supplied as arguments. The month argument has to lie in the range 1-12, and the day argument in the range 1-31. The year argument can be 2 or 4 integers – in the first case, the year will be picked based on the YEARCUTOFF option.
Category: Date Functions
Syntax:
Usage:
a = mdy(7,14,1983);
put a = date9.;

Result: 14JUL1983
Default: All three arguments are required. 

--------------------------------------------------------------------------------------------------------------------------

17.DAY 
Information: The DAY function takes a SAS date value as input argument and returns an integer representing the day of the month. Returns values between 1 and 31.
Category: Date Functions
Syntax: day(date)
Usage:
a = “14JUL1983”d;
b = day(a)
put b;

Result: 14

----------------------------------------------------------------------------------------------------------
18.DATDIF
Information: The DATDIF function returns the number of days between two dates. The arguments required are the start_date, end_date and basis. The end_date specifies the date to subtract from (ie. the more recent of the two dates) and the start_date specifies the date to be subtracted (ie. the less recent of the two dates). The basis is a character constant or variable that specifies the number of days in a month and year that SAS should assume to calculate the difference. “act/act” specifies the actual values and is aliased by “Actual”. “30/360” assumes a 30 day month and 360 days in an year.
Category: Date Functions
Syntax: datedif(start_date,end_date,basis)
Usage:
a = “14JUL1983”d;
b = “14JUL1984”d;
c = datedif(a,b,”act/act”);
d = datedif(a,b,”30/60”);
put c;
put d;

Result: 366
Result: 360
Default: Requires all arguments. Assumes 30 days in a month and 360 days in an year if third argument is not “30/360” or “act/act” or “Actual”.
Additional Info: This function will also admit character input for the first two arguments provided it’s the number of days since 1 January 1960. 

--------------------------------------------------------------------------------------------------------
19.MINUTE
Information: The MINUTE function takes a SAS time OR datetime value as input argument and returns an integer representing the minute of the time part. Returns values between 0 and 59.
Category: Time/Datetime Functions
Syntax: minute(date|datetime)
Usage:
a = “3:14:15”t;
b = minute(a)
put b;

Result: 14









  1. gravatar

    # by Anonymous - May 28, 2010 at 2:12 AM

    This was really helpful...Thanks

Blog Widget by LinkWithin
If you liked this post, make sure you subscribe to the RSS feed!

Search this blog..

Loading