In this tutorial we will discuss about extracting date, time and datetime in SAS using inbuilt functions. To get a better understanding we will search for data and time separately, then we will use these to populate datetime.

How to extract date in SAS

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

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

result :

20210321 21MAR2021

How to find time in SAS

In this example we will use the 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 the inbuilt system function as well as the contact function to populate the daytime 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 on how you can extract dates, times, and datetime in SAS.