Notes on STATA

Some nitty-gritties that economists remember but I don't

By Harshvardhan in STATA economics

February 16, 2022

Conventions through this page

x is the name of the variable.


Set Working Directory

cd "/directory/"

Use a Dataset

use data.dta, clear

Clear all items from memory

clear all

See documentation for details.

Display all items

list

Display first element only (useful for a scalar)

di x

Setting number of observations

qui set obs 30

qui says this command is executed quietly, i.e. with no output.

Summary of a variable

summarize x

This will number of observations, mean, standard deviation, minimum and maximum.

Describe

describe x

This will tell us the type of variable it is.

Functions

Functions in Stata are called Programs. See documentation.

Below is a sample program named onesample that generates 30 uniform random numbers and calculates their mean. We do need to specify rclass to specify the kind of return for the function. Other options is eclass.

drop _all is different from drop all ; the former drops all observations and the latter looks for a variable named all and deletes that.

program onesample, rclass
    drop _all
    qui set obs 30
    gen x = runiform()
    summ x
    return scalar mean = r(mean)
end

Monte Carlo Simulation

If the function is defined as above, we can use the following codes to perform Monte Carlo simulation.

* Simulate it 10000 times
simulate xbar = r(mean), seed(0) reps(10000): onesample

Visualisation

Histogram

hist x, width(0.1) title("Histogram of x")

See documentation for details.

Posted on:
February 16, 2022
Length:
2 minute read, 232 words
Categories:
STATA economics
See Also: