Convert Character Variable to Date
The following SAS code creates a sample SAS dataset for demonstration purpose.
data example;
input dateofbirth $20.;
cards;
05/11/1980
07/05/1990
04/14/1981
;
run;
Convert Character Variable to SAS Date:
The INPUT function is used to convert character variable (string) to numeric. With MMDDYY10. format, we assign format of the date.
data out;
set example;
dateofbirth2 = input(strip(dateofbirth),MMDDYY10.);
format dateofbirth2 MMDDYY10.;
run;
How to Convert Character Variable to Different Date Format
As you can see our original dateofbirth variable is in Month-Date-Year format but stored as a character. If we need to convert it to Date-Month-Year format and stored as in SAS date format.
data out;
set example;
dateofbirth2 = input(strip(dateofbirth), MMDDYY10.);
format dateofbirth2 DDMMYY10.;
run;
How to convert character dates of DD-MMM-YYYY or DD/MMM/YYY format?
You can use the format date11. to convert character values in DD-MMM-YYYY format.
DATA temp;
INPUT dt $11.;
dt2 = input(strip(dt),date11.);
FORMAT dt2 date11.;
CARDS;
10/JUN/2024
;
PROC PRINT NOOBS;
RUN;
What are Formats and Informats?
- Formats is used to tell SAS how to display or write values of a variable whereas Informats is used to tell SAS how to read a variable. Informats is basically used when you read or import data from either an external file (Text/Excel/CSV) or read in sample data which was created using CARDS/DATALINES statement. It is also used when you create a new variable in a dataset.
- Formats can be used in both Data Steps and PROC Steps whereas Informats can be used only in Data Steps.
Let’s understand by examples –
n the program below, we have used INFORMATS ddmmyy8. and ddymmyy10. to read dates in SAS. It creates a dataset called sampledata which is stored in WORK library.
DATA sampledata;
INPUT @6 date1 ddmmyy8. @15 date2 ddmmyy10.;
CARDS;
30-12-16 30-12-2016
;
RUN;
8 and 10 refers to width of the date.
It returns 20818 as it is in SAS date value form. It is not meaningful if you look at the value. You cannot tell which date it is. To display in real date form, use FORMAT statement.
DATA sampledata;
INPUT @6 date1 ddmmyy8. @15 date2 ddmmyy10.;
FORMAT date1 ddmmyy8. date2 ddmmyy10.;
cards;
30-12-16 30-12-2016
;
RUN;
How to read DD-MMM-YY format
You can use date11. format for both DD-MMM-YY and DD-MMM-YYYY format.
DATA temp;
INPUT @6 dt date11.;
FORMAT dt date11.;
CARDS;
10-oct-14
;
PROC PRINT NOOBS;
RUN;
Today() function:
The today() function can be used to generate current date.
data _null_;
dt=today();
format dt yymmdd10.;
put dt ;
run;
How to Display Date in Word Format
1. Short Word Date Format
The format date9. returns 30DEC2016.
format dt date9.;
Complete Word Date Format
The format WORDDATE. returns DECEMBER 30, 2016. No need to specify width in this format. It automatically adjusts the width depending on the month.
format dt WORDDATE.;
Including WEEK
The format WEEKDATE. returns Friday, December 30, 2016
format dt WEEKDATE.;
Display DAY / MONTH / YEAR
In this section, we will see how we can display only day, month, year and weekday.
data _null_;
dt=today();
put “Day :” dt DAY.;
put “Month :” dt MONTH.;
put “YEAR:” dt YEAR.;
put “WEEKDAY:” dt DOWNAME.;
run;
NOTE: output in log file
Some of the commonly used date formats are listed below –
| Formats | Result |
| DDMMYYP10. | 30.12.2016 |
| DDMMYYS10. | 30/12/2016 |
| MMDDYYP10. | 12.30.2016 |
| MMDDYYS10. | 12/30/2016 |
| WORDDATX19. | 30 DECEMBER 2016 |
The INTCK and INTNX functions in SAS are incredibly useful for working with dates and intervals. Here’s a quick overview:
INTCK Function
- Purpose: Calculates the number of intervals (e.g., days, months, years) between two dates.
- Syntax:
INTCK(interval, start-date, end-date, [method])interval: Specifies the unit of measurement (e.g., ‘DAY’, ‘MONTH’, ‘YEAR’).start-dateandend-date: The two dates to calculate the difference between.[method]: Optional; can be ‘CONTINUOUS’ or ‘DISCRETE’ (default is ‘DISCRETE’).
- Example:
·data example;
·date1 = '01JAN2020'd;
·date2 = '01JAN2022'd;
·years_diff = intck('YEAR', date1, date2);
·format date1 date2 date9.;
·run;
This calculates the number of years between the two dates.
INTNX Function
- Purpose: Increments a date by a specified number of intervals.
- Syntax:
INTNX(interval, start-date, increment, [alignment]) - Doing Advance month year and day.
interval: Specifies the unit of measurement (e.g., ‘DAY’, ‘MONTH’, ‘YEAR’).
start-date: The starting date.
increment: Number of intervals to add (can be positive or negative).
[alignment]: Optional; aligns the result to the ‘BEGINNING’, ‘MIDDLE’, ‘END’, or ‘SAME’ day of the interval.
- Example:
data example;
start_date = '01JAN2020'd;
new_date = intnx('MONTH', start_date, 3, 'BEGINNING');
format start_date new_date date9.;
run;
This adds three months to the starting date and aligns it to the beginning of the month.
In intck Also use only ‘c’
In INTNX Also use ‘s’ – Same date, ‘m’ -Middle date, ‘b’- Last to Last