SAS Character Functions, Date Functions

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.

Discover more from AI HintsToday

Subscribe to get the latest posts sent to your email.

Leave a Reply

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

Latest Entries:-

  • Data Engineering Job Interview Questions :- Datawarehouse Terms
  • Oracle Query Execution phases- How query flows?
  • Pyspark -Introduction, Components, Compared With Hadoop
  • PySpark Architecture- (Driver- Executor) , Web Interface
  • Memory Management through Hadoop Traditional map reduce vs Pyspark- explained with example of Complex data pipeline used for Both used
  • Example Spark submit command used in very complex etl Jobs
  • Deploying a PySpark job- Explain Various Methods and Processes Involved
  • What is Hive?
  • In How many ways pyspark script can be executed? Detailed explanation
  • DAG Scheduler in Spark: Detailed Explanation, How it is involved at architecture Level
  • CPU Cores, executors, executor memory in pyspark- Expalin Memory Management in Pyspark
  • Pyspark- Jobs , Stages and Tasks explained
  • A DAG Stage in Pyspark is divided into tasks based on the partitions of the data. How these partitions are decided?
  • Apache Spark- Partitioning and Shuffling
  • Discuss Spark Data Types, Spark Schemas- How Sparks infers Schema?
  • String Data Manipulation and Data Cleaning in Pyspark

Discover more from AI HintsToday

Subscribe now to keep reading and get access to the full archive.

Continue reading