Hi Everyone,
I would like to know, how do i create timestamp variable capturing execution start date and time and execution end date and time in sas dataset.
Thanks
Are you wanting to put the date and time the program executes and finishes into a data set, or are you wanting to just put the date and time to the log at the beginning of the run and again at the end? You can do either, although if you put it into the data set it will be there for every observation. You could do somehting like the following:
data final;
set have;
format StartDate mmddyy10. StartTime time10.;
StartDate=date();
StartTime=time();
run;
/*** Other program code ***/
data final;
set final;
format EndDate mmddyy10. EndTime time10.;
EndDate=date();
EndTime=time();
run;
Otherwise, you could always print the date and time to the log once the program begins execution and again when it completes. That would be like the following:
data _NULL_;
put "====================================";
put "Start Date: %sysfunc(date(),worddate.)";
put "Start Time: %sysfunc(time(),time.)";
put "====================================";
run;
/*** Other program code ***/
data _NULL_;
put "====================================";
put "End Date: %sysfunc(date(),worddate.)";
put "End Time: %sysfunc(time(),time.)";
put "====================================";
run;
The DATETIME() function will return the current datetime (aka timestamp) value.
Not sure how you want to use that but you could store it in macro variable using
%let currdt=%sysfunc(datetime());
Are you wanting to put the date and time the program executes and finishes into a data set, or are you wanting to just put the date and time to the log at the beginning of the run and again at the end? You can do either, although if you put it into the data set it will be there for every observation. You could do somehting like the following:
data final;
set have;
format StartDate mmddyy10. StartTime time10.;
StartDate=date();
StartTime=time();
run;
/*** Other program code ***/
data final;
set final;
format EndDate mmddyy10. EndTime time10.;
EndDate=date();
EndTime=time();
run;
Otherwise, you could always print the date and time to the log once the program begins execution and again when it completes. That would be like the following:
data _NULL_;
put "====================================";
put "Start Date: %sysfunc(date(),worddate.)";
put "Start Time: %sysfunc(time(),time.)";
put "====================================";
run;
/*** Other program code ***/
data _NULL_;
put "====================================";
put "End Date: %sysfunc(date(),worddate.)";
put "End Time: %sysfunc(time(),time.)";
put "====================================";
run;
Thanks!
Your code solution will help in:
1. creating timestamp in dataset
2. creating timestamp in log
Regards
One more thing, please can you provide solution for date time together instead of date and time seperately.
Just use the datetime() instead of date() and time(). Like this:
data final;
set have;
format StartDateTime datetime20.;
StartDateTime=datetime();
run;
/*** Other program code ***/
data final;
set final;
format EndDateTime datetime20.;
EndDateTime=datetime();
run;
Or if you're printing to the log, like this:
data _NULL_;
put "=============================================";
put "Start Date & Time: %sysfunc(datetime(),datetime.)";
put "=============================================";
run;
/*** Other program code ***/
data _NULL_;
put "=============================================";
put "End Date & Time: %sysfunc(datetime(),datetime.)";
put "=============================================";
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.