Open do files stata


















Buick Regal 20 9. Buick Riviera 16 Buick Skylark 19 Deville 14 Eldorado 14 Seville 21 Chevette 29 Impala 16 Malibu 22 Monte Carlo 22 Monza 24 Nova 19 Dodge Colt 30 Dodge Diplomat 18 Dodge Magnum 16 Dodge St.

Regis 17 Ford Fiesta 28 Ford Mustang 21 Continental 12 Mark V 12 Versailles 14 Bobcat 22 Cougar 14 Marquis 15 Monarch 18 XR-7 14 Zephyr 20 Olds 98 21 Olds Cutl Supr 19 Olds Cutlass 19 Olds Delta 88 18 Olds Omega 19 Then open a new log file. We suggest giving a log file the same name as the do file it records, so either:. The replace option tells Stata it's okay to replace previous versions of that file.

This clears out any data or stored results from whatever you were doing before running this do file. The command to open a Stata data set is use. If you're doing our example, type:. You don't need to type. If the data set you want to work with is not in Stata format, you'll need the import command instead of use. You can click on File , Import in the main Stata window and then use the graphical user interface to set up the right options for reading in your data set, but be sure to copy the resulting command into your do file.

This will give you the mean age of the GSS respondents, along with other summary statistics. If you're doing homework, add whatever commands you need to do your assignment. Do files written for research may have hundreds of commands at this point. Most homework assignments do not require you to save any changes to your data set.

But if you have made changes to the data, like creating a new variable, and you want those changes to be available in the future, use the save command to save the modified data set:.

Again, the replace option means Stata can replace old versions of that file. However, note that the file name in the save command is not the same as in the earlier use command. Never save a modified data set over your original data file. If you do, and it turns out you made a mistake, you will have to get fresh copy of the data from its source. But if you keep the original data set intact, all you have to do is correct the mistake in your do file and run it again.

You can run a do file by pressing Ctrl-D or by clicking the button on the far right of the top menu that looks like a "play" button. If you select part of the do file, pressing Ctrl-D or clicking play will only run that part. Our suggestion is that you keep your do files short enough that when you're working on one of them you can easily wrap your head around it. You also want to keep do files short so they run as quickly as possible: working on a do file usually requires running it repeatedly, so moving any code that you consider "done" to a different do file will save time.

Comments are text included in a do file for the benefit of human readers, not for Stata. Comments can explain what the do file does and why, and if anyone else ever needs to read and understand your do file they'll be very grateful for good comments. But you are the most likely beneficiary of your comments, when you have to figure out how your do file works months or years after writing it.

You don't need to comment every command—most Stata code is fairly easy to read. But be sure to comment any code that required particular cleverness to write, or you'll need to be just as clever to figure out what it does later.

Comments need to be marked as such so that Stata will not try to execute them. Here's an example of commenting code:. A useful programmer's trick is to "comment out" code you don't want to run right now but don't want to delete entirely. This allows you to break up commands over multiple lines for readability:. U niversity of W isconsin —Madison. Join the SSCC. Anatomy of a Do File Almost all do files carry out the same basic steps.

Create a Log File to Store Results The first thing your do file should do is set up a log file which will store its results. Make sure that no previous log files are still open with: capture log close This is important because if your do file crashes before it gets to the command to close its log at the end, it will leave the log file open.

Then go back to the do file itself and type: log using first. Clear Stata's Memory Another key to reproducibility is always starting with a blank slate, so the next command should be: clear all This clears out any data or stored results from whatever you were doing before running this do file.

Open a Data Set In general, you'll load data with the use command. However, since we're using the auto data set that comes with Stata, you'll open it with sysuse : sysuse auto Every time you run this do file, it will load a fresh copy of the data from disk into memory.

Do Your Work You're now ready to do your work. For now just add: list make if foreign Of course real do files will have many more and much more useful commands at this point. Save your Data Do files that carry out data wrangling will change the data set, and need to save the new version of the data at the end.

This do file does not change the data, but save it anyway for practice: save autoV2, replace The replace option again allows Stata to overwrite the output from previous attempts to run the do file. Close your Log The last line of the do file will normally be: log close If you don't close the do file's log, any commands you run after the do file finishes will be recorded in the the log.

Running a Do File The easiest way to run a do file is to press Ctrl-d in the Do File Editor, or click the icon on the far right that looks like a "play" button over some code. Output Files Because you did not tell Stata where to put first. How long should a do file be? Comments Comments are text included in a do file for the benefit of human readers, not for Stata.



0コメント

  • 1000 / 1000