INTNX Function in SAS is used to Add date and time. INTNX Function adds date in Days months and years to SAS date. INTNX Function also adds Time in Minutes Seconds and hours to SAS Timestamp. INTNX Function in SAS also populates first and last date of a month. First and Last date of a year
Let’s see an Example for Each
- Add days to Date in SAS
- Add weeks to Date in SAS
- Add Quarter to Date in SAS
- Add Year to Date in SAS
- Populate First date of a month in SAS
- Populate Last date of a month in SAS
- Populate First date of year in SAS
- Populate Last Date of year in SAS
Syntax INTNX in SAS :
INTNX(‘Interval’, start_date, number of intervals to add)
- The available intervals are Day, Week, Month, Qtr (quarter) or Year and must be enclosed in quotes.
- The start date must be a SAS date
- The number of intervals must be an integer value
There is also the 4th argument which is used to return the date which is used to have complete control on the date that is being returned
b – The date of the beginning of the interval is returned (first day of the week/ month/year). This is also the default if nothing is entered.
e – The date of end of the interval is returned (last day of the week/ month/year).
m – The date of the middle of the interval (middle day of the week/ month/year).
s – The date of the same day within the interval is returned (same day of the week/ month/year).
So we will be using EMP_DET Table in our example
INTNX – add days to date
INTNX() Function takes ‘day’ and 7 as argument which adds 7 days to birthday column
/* add 7 days */ data emp_det; set emp_det; dayplus =intnx('day', Birthday, 7); format dayplus date9.; run;
So the resultant table will be
INTNX – add weeks to date
INTNX() Function takes ‘week’ and 6 as argument which adds 6 weeks to birthday column. Argument ‘s’ adds same day to the birthday column
‘b’ – beginning,
‘m’ – middle,
‘e’ – end,
‘s’ – sameday. The default value is ‘b’
/* add 6 weeks */ data emp_det1; set emp_det; weekplus=intnx('week', Birthday, 6,'s'); format weekplus date9.; run;
So the resultant table will be
INTNX – add month to date
INTNX() Function takes ‘month’ and 4 as argument which adds 4 month to birthday column. Argument ‘s’ adds same day to the birthday column
/* add 4 month */ data emp_det1; set emp_det; monthplus=intnx('month', Birthday, 4, 's'); format monthplus date9.; run;
So the resultant table will be
INTNX – add year to date
INTNX() Function takes ‘year’ and 2 as argument which adds 2 years to birthday column. Argument ‘s’ adds same day to the birthday column
/* add 2 year */ data emp_det1; set emp_det; yearplus=intnx('year', Birthday, 2, 's'); format yearplus date9.; run;
So the resultant dataframe will be
INTNX – Populate First date of a Month
In order to populate first date of a month in SAS we will be using INTNX() Function. It takes ‘month’ as argument along with ‘b’ which populates the first date of that particular month
data emp_det1; set emp_det; firstdate=intnx('month', Birthday, 0,'b'); format firstdate date9.; run;
So the resultant table will be
INTNX – Populate Last date of a Month
In order to populate last date of a month in SAS we will be using INTNX() Function. It takes ‘month’ as argument along with ‘e’ which populates the last date of that particular month
data emp_det1; set emp_det; lastdate=intnx('month', Birthday, 0,'e'); format lastdate date9.; run;
So the resultant table will be
INTNX – Populate First date of a year
In order to populate First date of year in SAS we will be using INTNX() Function. It takes ‘year’ as argument along with ‘b’ which populates the first date of that particular year
data emp_det1; set emp_det; firstdate=intnx('year', Birthday, 0,'b'); format firstdate date9.; run;
So the resultant table will be
INTNX – Populate Last date of a Year
In order to populate last date of year in SAS we will be using INTNX() Function. It takes ‘year’ as argument along with ‘e’ which populates the last date of that particular year
data emp_det1; set emp_det; lastdate=intnx('year', Birthday, 0,'e'); format lastdate date9.; run;
So the resultant table will be
INTNX – Add hours minutes and seconds to the datetime
In order to add Seconds, Minutes and hours of timestamp in SAS we will be using INTNX() Function. INTNX() Function takes ‘second’ ,’minute’ and ’hour’ to add seconds , minutes and hours to timestamp respectively
data temp; mydt = '03MAR2019:09:27:00'dt; seconds=intnx('second',mydt , 1); minutes=intnx('minute',mydt , 1); hours=intnx('hour', mydt , 1, 's'); format mydt seconds minutes hours datetime20.; proc print NOOBS; run;
So the result will be