Extract date and time from timestamp in SAS – datepart()

Extract date and time from timestamp in SAS is accomplished using datepart() and timepart() respectively.  Extract date from timestamp is SAS done using datepart(). Extract time from timestamp is SAS done using timepart(). Let’s see an example of each.

  • Extract Date part from timestamp in SAS using datepart()
  • Extract Time part from timestamp in SAS using timepart()

So we will be using EMP_DET Table in our example

Extract date and time from timestamp in SAS 1

 

Extract date from timestamp in SAS:

Extracting Date part from timestamp in SAS is accomplished using datepart() function.

Syntax datepart() in SAS:

DATEPART(datetime)

In the below example we have extracted datepart of the last_login.


data emp_det1; 
set emp_det; 
only_date = datepart(last_login); 
format only_date date9.; 
run;

So the resultant table with date extracted from datetime will be

Extract date and time from timestamp in SAS 2

 

 

Extract time from timestamp in SAS:

Extracting time part from timestamp in SAS is accomplished using timepart() function.

Syntax timepart() in SAS:

TIMEPART(datetime)

In the below example we have extracted timepart of the last_login.


data emp_det1; 
set emp_det; 
only_time = timepart(last_login); 
format only_time time8.; 
run;

So the resultant table with time extracted from datetime will be,

Extract date and time from timestamp in SAS 3

 

 

                                                                                               

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.