
This would replace the existing data set with a new one with one row per state, containing a variable indicating which state, a variable indicating the average BMI in that state, and a variable indicating the percent of the state which is female.Ĭollapse is a destructive command after running it the original data (and any unsaved changes) is lost. collapse (mean) bmi (percent) female, by(state) For example, if we have some sort of census data. The collapse command generates a summary data set by collapsing the existing data by a given variable and creating summary statistics. To see an example of it, consider the collapse command. preserve takes a snapshot of the data as it currently is, and restore switches back to it. The preserve and restore commands can be very useful for switching between files or making destructive modifications to an existing data set. You can also pass clear as an option to use/ sysuse/ webuse/ import to save one step. The clear command will remove the existing data and allow you to load a new data set.

If you try and open a new data set when there are un-saved changes in the existing data, Stata will refuse. Passing the replace option allows Stata to overwrite an existing file. You can save a data set with the save command which works as. After you run the import, the command it generated is echoed and you can save it to run again. The Import dialog box has a live preview as you change settings which can be very useful. import excel and import delimited for importing Excel or CSV files.įor importation, I’d recommend using the File -> Import menu instead of trying to type the command.sysuse or webuse to access Stata example data sets (often used in Help examples).use to load a local file, or when passed a URL, an online file.The benefit of having a single data set open is that you never need to refer to it - any command you give must operate on the open data set. If you want to operate on multiple data sets, you either need to be constantly switching between them, or merge them into one file (for help with merging, look into the append and merge commands). One quirk of Stata is that only a single data set can open at a time. The 1/100 syntax is equivalent to writing 1 2 3 4. in operates on specific rows, and may be useful for testing slow running code. Performs linear regression only on the female subsample. Most commands support an if or in option to operate on a subset of the data. I would not recommend using abbreviations in most cases (a lot aren’t as clear as reg), but you may see others use them. For example, the regression command could have been typed as. Stata supports abbreviations for most commands. The noconstant operation forces \(\beta_0 = 0.\) This is defining a linear regression model predicting y based upon the continuous variables x1 and x2. Almost all commands support some number of options, most commands do not require any options.įor example, a regression would be as simple as. The options are space-separated words (e.g.

The order of variables may matter (for example, any regression model treats the first listed variable as the response.) The number of variables which needs to be/can be passed obviously varies by command.

Most Stata commands follow the same basic format.
