Automatically Save All Commands from Stata Command Window



There are many approaches to use Stata. I usually, save all commands in a DO file. This makes it very easy to reproduce the same process and results in the future. Also, it can save a lot of time if you find a mistake in your data or in your commands at a final stage of your analysis. 

The above approach is good, however, sometimes you might accidentally close Stata, or Stata crashes (which is not so often), or your operating system hangs, or you face a power outage, and so on. What if this happens and you have not saved your work? A simple solution is to create a log file at the start of each Stata session. 

If  'something can go wrong will go wrong' is true, then you might forget to start a log the start of Stata session and the unwanted happens. Don't worry, there is a solution. You can start a log file automatically when Stata is started. This can be done writing few lines to the file profile.do which exists in the Stata installation folder. In fact, you can enter as many commands as you want to this file. All such commands are executed when Stata is started. Here is my profile.do file.
 
   
set more off , perm
set maxvar 25000, perm
sysdir set PERSONAL D:\Dropbox\STATA\asprog
global root D
global F4 "cd D:\Dropbox\DATA ;"
global F5 "cd C:\temp ;"
global F6 "dir *.do;"
global F7 "dir *.dta;"
global data "D:\Dropbox\DATA"
cd C:/temp
cmdlog using "`c(current_date)'.do", append
 
You can read about other things that I have included in profile.do and learn what do they do, for time being, let us focus on the last line. The last line line will copy each command that you type in the command window, and will populate a do file with these commands. The do file will have a name of current date, and will be saved in your current directory.

                          **********************