How to Populate Current Date and Datetime in SAS

You can easily populate the current date and datetime in SAS using today() and datetime() functions. More specifically, current date can be populated using today() function and date with time can be populated using datetime() function.

We’re going to use the work data set “work.hightemp” to insert a new variable “date_now” with current date or current datetime. 

Create table query:

data work.hightemp;
input Place $ 1-13 Record_Date date9. Degree_f Degree_c;
format Record_Date date9.;
datalines;
South Africa 21jan2010 122 50
Israel 21jun1942 129 54
Argentina 02jan1920 120 49
Saskatchewan 05jul1937 113 45
India 18jun1905 124 51
Poland 29jul1921 104 40
;
run;

work.hightemp data set:

Create data set in SAS with datetime column
work.hightemp

This is how you can insert current date or time in SAS dataset:

1. Populate current date in SAS using today() function

  • Use date. Format
  • Use date9. format

2. Populate current datetime in SAS using datetime() function

1. Populate Current Date in SAS using today() function

Today() function returns todays date in SAS numeric format.

Syntax

TODAY()

The following example gives you more clarity on how SAS stores value and how formatted date looks like. For the demonstration, Let’s assume today is 08th Aug 2022

data _null_;
  dt=today();
  put 'SAS date=' dt;
  put 'formatted date (in date. format)=' dt date.;
  put 'formatted date (in date9. format)=' dt date9.;
run;

Execute the above code and observe the logs generated in the LOG window. You can see todays date in numeric, date. and date9. format.

Get current date and datetime in SAS LOG Window

Populate Current Date in SAS (using date. format)

You can get the current or today’s date using today() function but date. format converts it into desired date format. As you can see in the previous example, with the date. format you will get date value as 08AUG22 format.

We are creating a new data set with name hightemp_1 and adding a new variable “date_now” to populate the current date with date. format.

/* Add a new variable "date_now" and populate todays date in date. format. */
data hightemp_1;
set hightemp ;
date_now=today();
format date_now date.;
run;
Insert todays date in SAS data set
work.hightemp_1

Populate Current Date in SAS (using date9. format)

You can get the current or today’s date using the today() function but date9. format converts it into desired date format ( that is 08AUG2022 )

We are creating a new data set with name hightemp_2 and adding a new variable “date_now” to populate the current date with date9. format.

/* Add a new variable "date_now" and populate todays date in date9. format. */
data hightemp_2;
set hightemp ;
date_now=today();
format date_now date9.;
run;
Insert current date in SAS data set
work.hightemp_2

2. Populate Current Datetime in SAS using datetime() function

The function today() only gives you today’s date but if you want to populate the current date and time in SAS, you need to use the datetime() function.

Syntax:

DATETIME();

We are creating a new data set with name hightemp_3 and adding a new variable “date_now” to populate the current datetime with datetime22. format.

/* Add a new variable "date_now" and populate today's datetime in datetime22. format. */
data hightemp_3;
set hightemp ;
date_now=datetime();
format date_now datetime22.;
run;
Populate current datetime in SAS data set
work.hightemp_3

These are the two popular methods mostly used to insert current date and datetime in the SAS data set. The current date and time column gives you possibilities to track when and what time that particular data set has been created or updated and can be easily identified those rows.

NEXT: → Learn how to print todays date and time in the SAS Log.