• Laura Whiting

The Contract Command

The contract command replaces the currently loaded dataset with a new dataset, which contains all the combinations of two or more variables that the user specifies. It also adds a new variable, called “_freq” by default, which indicates the frequency of each unique combination. It is a useful summary cross-section of your data.

How to Use:

Worked Example:

For this example I am going to use the auto dataset, which you can load into Stata with the sysuse auto command. I will also illustrate the use of the preserve and restore commands. The preserve command allows you to save a snapshot of the data currently loaded in memory. The restore command can then be used at a later time to restore the data in memory to the preserved snapshot. Please note the use of restore will replace your current dataset with the previously preserved one, so if you still want your current dataset you should save it before you use the restore command.

In the command pane I type the following:

This gives me the following output:

The contract command has pulled the dataset down to two variables so I can compare them, which you can see in the second image above. I have then saved that contraction and restored the original dataset. This allows me to analyse the contraction while keeping my original dataset intact.

From the contract command I could infer that domestic cars have on average a better repair record that foreign cars. This command can be used on any number of variables in a dataset, not just the two that I have used here.

Note: This command will drop variables from your dataset. Make sure you either have an up-to-date saved copy of your dataset, or have made use of the preserve command, before you use contract or you will lose your pre-contracted dataset.

45 views0 comments