here’s a table summarizing some common SAS List Date functions with their syntax and examples:
| Here’s a breakdown of some key categories with representative functions, syntax, and examples: | ||||||
| 1. Retrieving Dates: | ||||||
| Function | Syntax | Description | Example | Output (assuming today’s date is 2024-05-11) | ||
| DATE | DATE | Returns today’s date as a SAS date value. | date_var = DATE; | `date_var | ||
| TODAY | TODAY | Similar to DATE, returns today’s date as a SAS date value. | today_var = TODAY(); | `today_var | ||
| DATEJUL(juldate) | Datejul(juldate) | Converts a Julian date (juldate) in YYDDD or YYYYDDD format to a SAS date value. | julian_date = Datejul(99001); | `julian_date | ||
| drive_spreadsheetExport to Sheets | ||||||
| 2. Constructing Dates: | ||||||
| Function | Syntax | Description | Example | Output | ||
| Mdy(m,d,y) | Mdy(m,d,y) | Creates a SAS date value from numeric month (m), day (d), and year (y). | birth_date = Mdy(10,26,1980); | `birth_date | ||
| Ymd(y,m,d) | Ymd(y,m,d) | Similar to Mdy, creates a date from year (y), month (m), and day (d). | hire_date = Ymd(2023,01,15); | `hire_date | ||
| DHMS(date, hour, minute, second) | DHMS(date, hour, minute, second) | Creates a SAS datetime value from a date, hour (0-23), minute (0-59), and second (0-59). | datetime_var = DHMS(TODAY(),10,30,0); | `datetime_var | ||
| drive_spreadsheetExport to Sheets | ||||||
| 3. Extracting Date Components: | ||||||
| Function | Syntax | Description | Example | Output (assuming today’s date is 2024-05-11) | ||
| DAY(date) | DAY(date) | Extracts the day of the month (1-31) from a SAS date value. | day_of_month = DAY(birth_date); | `day_of_month | ||
| MONTH(date) | MONTH(date) | Extracts the numeric month (1-12) from a SAS date value. | birth_month = MONTH(birth_date); | `birth_month | ||
| YEAR(date) | YEAR(date) | Extracts the year as a four-digit number from a SAS date value. | birth_year = YEAR(birth_date); | `birth_year | ||
| QTR(date) | QTR(date) | Extracts the quarter (1-4) from a SAS date value. | birth_quarter = QTR(birth_date); | `birth_quarter | ||
| WEEKDAY(date) | WEEKDAY(date) | Extracts the day of the week (1=Sunday, 7=Saturday) as a numeric value from a SAS date value. | day_of_week = WEEKDAY(today_var); | `day_of_week | ||
| drive_spreadsheetExport to Sheets | ||||||
| 4. Date Calculations: | ||||||
| Function | Syntax | Description | Example | Output (assuming today’s date is 2024-05-11) | ||
| INTNX(‘unit’,date, n) | INTNX(‘unit’,date, n) | Adds or subtracts a specified number (n) of units (e.g., ‘YEAR’, ‘MONTH’, ‘DAY’) to a date. | future_date = INTNX(‘YEAR’, today_var, 5); | `future_date | ||
In SAS, DATDIF() and YRDIF() are functions used to calculate the difference between two dates in terms of days and years, respectively.
1. DATDIF() Function:
- Syntax:
DATDIF(start_date, end_date, 'interval') - Description: Calculates the difference between two dates in terms of days or another specified interval.
- Example:sasCopy code
data output; set input; days_difference = DATDIF(start_date, end_date, 'ACTUAL'); run; - In this example,
days_differencewill contain the number of days betweenstart_dateandend_date.
2. YRDIF() Function:
- Syntax:
YRDIF(start_date, end_date, 'method') - Description: Calculates the difference between two dates in terms of years, fractionally or rounded to the nearest whole year.
- Example:
data output; set input; years_difference = YRDIF(start_date, end_date, 'ACTUAL'); run; - In this example,
years_differencewill contain the difference in years betweenstart_dateandend_date.
Discover more from HintsToday
Subscribe to get the latest posts sent to your email.