SAS Character Functions, Date Functions

by | Apr 30, 2024 | SAS | 0 comments

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:      
       
FunctionSyntaxDescriptionExampleOutput (assuming today’s date is 2024-05-11)  
DATEDATEReturns today’s date as a SAS date value.date_var = DATE;`date_var  
TODAYTODAYSimilar 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:      
       
FunctionSyntaxDescriptionExampleOutput  
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:      
       
FunctionSyntaxDescriptionExampleOutput (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:      
       
FunctionSyntaxDescriptionExampleOutput (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 codedata output; set input; days_difference = DATDIF(start_date, end_date, 'ACTUAL'); run;
  • In this example, days_difference will contain the number of days between start_date and end_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_difference will contain the difference in years between start_date and end_date.

Written By HintsToday Team

undefined

Related Posts

SAS project that involves merging, joining, transposing large tables, applying PROC SQL lead/rank functions, performing data validation with PROC FREQ, and incorporating error handling, macro variables, and macros for various functional tasks

Let us create a comprehensive SAS project that involves merging, joining, transposing large tables, applying PROC SQL lead/rank functions, performing data validation with PROC FREQ, and incorporating error handling, macro variables, and macros for various functional...

read more

SAS First., Last. Syntax and uses with examples

In SAS, the FIRST. and LAST. automatic variables are used within a DATA step to identify the first and last occurrences of observations within a BY group. These variables are particularly useful when working with sorted data or when you need to perform specific...

read more

Explain SAS PDV step by step with a example

The Program Data Vector (PDV) is a critical concept in SAS programming, particularly in the context of the DATA step. It represents the current state of data processing during the execution of a DATA step. Let's delve into how the SAS PDV works in detail: 1....

read more

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *