Stata: Can you autosave a .do file with a time stamp every time you execute that .do file in the do file editor?

StackOverflow https://stackoverflow.com//questions/20007232

  •  20-12-2019
  •  | 
  •  

Question

I wondered if it would be possible to autosave a .do file so it contains a time stamp that matches the time stamp on the log file that I create within the .do file. For example, I start out every .do file I have to look something like this:

#delimit ;
capture log close;
display _n(250);

*******Sets up the log for the output;
local a1=substr(c(current_time),1,2);
local a2=substr(c(current_time),4,2);
local a3=substr(c(current_time),7,2);
local b =       c(current_date);

log using "H:\HSRE\Hospice Payment Reform\Plotzke\Ad Hoc Tasks\OY1\Monitoring for macs\Log\BPA Log (`b')`a1'_`a2'_`a3'",  t;

<rest of the program>

display "Start: `starttime'";
display "End: " "$S_TIME";
log close;

Is there anything I can add at the very end of the program that would save the .do file that I just ran (in the .do file editor) with a time stamp (so I can match the code with the log)?

For example, currently I have the file:

"K:\Common\HSRE\Hospice Payment Reform\Plotzke\Ad Hoc Tasks\OY1\BenefitPeriodAnalysis\code\Benefit period analysis (Code).do"

It would be great if every time I run the .do file it would save:

"K:\Common\HSRE\Hospice Payment Reform\Plotzke\Ad Hoc Tasks\OY1\BenefitPeriodAnalysis\code\Benefit period analysis (Code)(`b')`a1'_`a2'_`a3'.do"   

[Or some variation]

Was it helpful?

Solution

You can always do the following to have a really blunt type of versioning:

Say you are operating out of a master .do file:

"K:\...\Benefit period analysis (Code).do"

You can just add a line into your code (after you set all the locals) to copy that file with a new timestamped name:

copy "K:\...\Benefit period analysis (Code).do" "K:\...\Benefit period analysis (Code)(`b')`a1'_`a2'_`a3'.do"

But note that you would want to always save your open .do file before running, and that your .do file editor wouldn't switch to display this newly created file: it would remain displaying the master (non-timestamped version). But I think this will achieve what you are after.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top