DATA Step, Macro, Functions and more

Create timestamp variable in sas

Accepted Solution Solved
Reply
Contributor
Posts: 52
Accepted Solution

Create timestamp variable in sas

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

 

 


Accepted Solutions
Solution
‎12-15-2015 04:30 PM
Frequent Contributor
Posts: 130

Re: Create timestamp variable in sas

Posted in reply to dkanand86

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;

View solution in original post


All Replies
Super User
Super User
Posts: 7,074

Re: Create timestamp variable in sas

Posted in reply to dkanand86

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());

 

Solution
‎12-15-2015 04:30 PM
Frequent Contributor
Posts: 130

Re: Create timestamp variable in sas

Posted in reply to dkanand86

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;
Contributor
Posts: 52

Re: Create timestamp variable in sas

Thanks!

 

Your code solution will help in:

1. creating timestamp in dataset

2. creating timestamp in log

 

Regards

Contributor
Posts: 52

Re: Create timestamp variable in sas

Posted in reply to dkanand86

One more thing, please can you provide solution for date time together instead of date and time seperately.

 

Frequent Contributor
Posts: 130

Re: Create timestamp variable in sas

Posted in reply to dkanand86

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;
🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 5425 views
  • 1 like
  • 3 in conversation