Click to compare with Python equivalents
| command | [action] |
|---|---|
| 3.1. do filename | [ runs commands in file.do ] |
| 3.2. log using filename, replace | [ makes a full record of your Stata session ] |
| 3.3. doedit | [ opens do-file editor ] |
| command | [action] |
|---|---|
| 3.1. python3 filename | [ runs commands in script.py ] |
| 3.2. log_file = open("filename.log", "w") | [ opens writable log file ] |
| 3.3. sys.stdout = log_file | [ redirects output to log_file ] |
Do-files are are text files that contain commands that can be executed by Stata (they end with .do). There are 3 ways you can use Stata. First, you can use the drop down menus. This may seem easier when you are beginning but there are a lot of limitations to this method. Second, you can enter commands directly into the command prompt has we have been doing in class. The third and prefered way, however, is to use a do-file. A do-file allows you to reproduce your work in the future. It also means you don't have to start from scratch each time you open Stata. You can save your do-file close Stata and go home without worrying about losing your progress.
You can create a do-file using any text editor on your computer or you can use the built-in do-file editor in Stata (ctrl+9 or command doedit)
When you use a do-file it is important that you provide clear infomration about the do-file's purpose. It is also important that you use relative file paths. If you use absolute file paths your do-file will not run on a diffrent computer. Finally, make sure you include comments so you can remember why you included specific commands and so that others can follow what you are doing.
There are 4 ways you can add comments in your do-file. This means that Stata will ignore them when running the commands.
• Begin line with * and Stata will ignore the line
• Place the comment inside /* */
• Place the comment after //
• Place the comment after /// to join the next line with the current one
Stata also allows you to keep a log of your session. It is good practice to start each do-file by opening a log.
Here is an example of a basic do-file. After looking at this example, make your own do-file. Make sure you include a log-file, comments, and some commands. Also, remember to close your log file after you're finished. After you run your do-file, make sure you can find the log-file Stata produced. It will be in your working directory.
Python scripts are are text files that contain commands that can be executed by python (they end with .py). There are 3 ways you can use python. First, you can run code directly in the terminal as we have been doing. Second, you can use an integrated development environment (IDE). The third and prefered way, however, is to use a script file. A scipt file allows you to reproduce your work in the future. It also means you don't have to start from scratch each time you open python. You can save your script file and go home without worrying about losing your progress.
You can create a script file using any text editor on your computer or you can use a specialized IDE.
When you use a scipt file it is important that you provide clear infomration about the script's purpose. It is also important that you use relative file paths. If you use absolute file paths your file may not run on a diffrent computer. Finally, make sure you include comments so you can remember why you included specific commands and so that others can follow what you are doing.
You can comment in python using a #. This means that python will ignore items after # when running the commands.
Python also allows you to keep a log of your session. It is good practice to start each script file by opening a log.
Here is an example of a basic scipt-file. After looking at this example, make your own script file. Make sure you include a log-file, comments, and some commands. Also, remember to close your log file after you're finished. After you run your script, make sure you can find the log-file Stata produced. It will be in your working directory.