When you execute a SAS program through SAS Studio, SAS EG or simply any scheduler, it automatically generates a SAS log.
The logging in SAS can be defined in the SAS system configuration files or else it can be set manually in the individual code by adding options like mprint, mlogic, symbolgen, etc.
This is something SAS generates internally depending on the SAS statements being executed. But what if you want to print today’s date in SAS log explicitly?
Print today’s date into the SAS log:
- Method 1: Print today’s date in SAS using data _null_;
- Method 2: Print today’s date in SAS using macro variable;
1: Print date into the SAS log (using data _null_and PUT statement)
Function today() gives you a date in numeric format but explicitly you can specify your preferred format. In this example we will use date9. format.
Function datetime() gives you date and time in numeric format but explicitly you can specify your preferred format. In this example we will use datetime22. format.
PUT statement Syntax:
PUT '...write message here' ;
The below code will print Today’s date and datetime in numeric, date9. and datetime22. formats in the SAS log.
data _null_;
dt=today();
dttm=datetime();
put 'Todays date=' dt;
put 'Todays datetime=' dttm;
put 'Todays date=' dt date9.;
put 'Todays datetime=' dttm datetime22.;
run;
Observe the below output where we have printed today’s date in the different formats.
2: Print date into the SAS log (using macro variable and %put statement)
This method is commonly used among SAS programmers. You can easily print date and time by creating a new variable and assigning a date value to print into the SAS log.
In this example we will create new variables dt for date and dttm for datetime by using %let statement.
You can use the %put macro statement to print the value assigned to the macro variables.
/* Method 2: Print date using macro variable and %put statement */
%let dt= %SYSFUNC(today(), date9.);
%let dttm= %SYSFUNC(datetime(), datetime22.);
%put Todays date: &dt. ;
%put Todays datetime: &dttm. ;
Observe the below output where we have printed today’s date in the different formats.
BONUS: Print very specific date
This is very helpful when you want to assign specific dates from the past or future and print them into the SAS log.
Let’s say, you want to calculate and assign a date which is 3 days back from today. The INTNX function will help you to subtract 3 days from today.
%let date_comp= %SYSFUNC(INTNX(DAY,%SYSFUNC(TODAY()),-3),date9.);
%put &date_comp.;
And here is the output: