Extract day, month and year from date or timestamp in SAS

Extract day, month and year from date in SAS is accomplished using day(), month() and year() respectively.  Extract day from date in SAS is done using day(). Extract month from date or timestamp in SAS is done using month(). Extract year from date or timestamp in SAS is done using year(). Extract monthyear from date in SAS.  Let’s see an example of each.

  • Extract day from date in SAS is done using hour() Function
  • Extract month from date in SAS is done using minute() Function
  • Extract year from date in SAS is done using second() Function
  • Extract monthyear from date in SAS

So we will be using EMP_DET Table in our example

Extract day, month and year from date or timestamp in SAS 1

 

Step 1: First get the Date part from timestamp and perform further operations like extracting Day, Month and Year from date in SAS.

Get Date Part:

/* first get only datepart and do operations on datepart */ 
 
data emp_det1; 
set emp_det; 
only_date = datepart(last_login); 
format only_date date9.; 
run; 

 

 

Step 2a: Extract day from date / timestamp in SAS:

Extracting day from date in SAS is accomplished using day() function. In the below example we have extracted day of the only_date column.

/* extract day */ 

data emp_det2; 
set emp_det1; 
day_part = Day(only_date); 
run;

So the resultant table with day extracted from date will be

Extract day, month and year from date or timestamp in SAS 2

 

Step 2b: Extract month from date / timestamp in SAS:

Extracting month from date in SAS is accomplished using month() function. In the below example we have extracted month of the only_date column.

/* extract month */ 

data emp_det2; 
set emp_det1; 
month_part = month(only_date); 
run;

So the resultant table with month extracted from date will be

Extract day, month and year from date or timestamp in SAS 3

 

Step2C: Extract Year from date / timestamp in SAS:

Extracting year from date in SAS is accomplished using year() function. In the below example we have extracted year of the only_date column.

/* extract year */ 

data emp_det2; 
set emp_det1; 
year_part = year(only_date); 
run; 

So the resultant table with year part extracted from date will be

Extract day, month and year from date or timestamp in SAS 4

 

Extract monthyear from date / timestamp in SAS:

Extracting monthyear from date in SAS is accomplished using put() function with monyy7. formatting. In the below example we have extracted monthyear of the only_date column.

/* Extract monthyear */ 
 
data emp_det2; 
set emp_det1; 
monthyear = put(only_date,monyy7.); 
run;

So the resultant table with monthyear extracted from date will be

Extract day, month and year from date or timestamp in SAS 5