How to Extract Date,Time and Datetime in SAS

In this tutorial we will discuss how to Extract Date,Time and Datetime in SAS using inbuilt functions.To get better understanding we will discover data and time seperately then we concatenate these to populate datatime.

How to extract Date in SAS

In this example we will use inbuilt system function to extract date and then compressing the delimiter using compress function.

 

%LET DT=%SYSFUNC(COMPRESS(%SYSFUNC(DATE(),YYMMDD10.),'-'));
%LET DATE=%SYSFUNC(TODAY(),DATE9.);
%PUT &DT. &DATE;

Result :

20210321 21MAR2021

How to extract Time in SAS

In this example we will use inbuilt system function to extract time.

 

%LET TM=%SYSFUNC(TIME(),TIME8.);
%PUT &TM.;

Result :

10:29:53

How to extract DateTime in SAS

In this example we will use inbuilt system function along with concatenate function to populate datetime value.

 

%LET DT=%SYSFUNC(COMPRESS(%SYSFUNC(DATE(),YYMMDD10.),'-'));
%LET TM=%SYSFUNC(TIME(),TIME8.);
%LET DTM=%SYSFUNC(CATS(&DT,:,&TM));
%PUT &DTM.;

Result :

20210321:10:32:04

Hope you are now more clear how you can Extract Date,Time and Datetime in SAS. Feel free to comment if you have any query or error you’re while executing this code.

Happy Learning !!