Econ 390 Using STATA Part 3 Do File
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
Econ 390
Using STATA Part 3
Do File
New STATA Commands:
1) capture log close
2) log using xxx.log, replace
3) log close
4) do
Note: STATA commands are case sensitive. I capitalized commands and variables (e.g. CLEAR, DVDES25...) in the notes because I want to highlight them. The variable names should be entered (could be either uppercase or lowercase, check the codebook) as in the original data.
1. Create a ‘do’ file.
Up to now, we have been using the ‘interactive’ command mode of STATA. There is an alternative way to run commands using ‘.do’ files.
Do files are sets of commands that are listed in a program. This way of running instructions in STATA is much better because you can easily re-run the same set of commands at a later time and recreate your results.
Everything inside /* */ will be ignored by STATA. So you can put your own comments inside to remind yourself what you are trying to do.
a) Your task is to write a program that does steps 1 to 10 as in Using STATA Part 2.
As an example, here is a sample incomplete program:
/* Sample program */
/* ECON 390 */
clear /* this clears the memory */
capture log close /* this closes any logs that may already be open */
log using "c:\Users\Arts User\Desktop\econ390.log", replace text
/* this opens a log so that screen output can be saved */
/* the ‘replace’ option means that any previous file is replaced */
/* the ‘t’ option means that the log is saved as an ASCII text file */
use "c:\Users\Arts User\Desktop\econ390.dta"
drop if mard==0
keep region rrsp
table region, c(mean rrsp)
log close /* closes the log at the end of the program*/
b) After you have finished and saved the do file as program390.do, you can then run it by using the DO command:
do "c:\Users\Arts User\Desktop\program390.do"
c) After the do file has successfully run in STATA, statistical results generated by such as SUMM and TABLE commands and also regression (REG) results (Part 4) will be saved in the log file.
Now try to open the log file and see what is inside by double clicking the file.
At the end of term, you have to submit your do file and log file with your final paper.
Codebook:
The variables are derived from the 1984 version of the Family Expenditure Survey conducted by Statistics Canada.
Variable |
Description |
FAMINC |
Family income in 1984 dollars. |
MARD |
Marital status. Takes the value 1 if married or in common-law relationship; 0 otherwise. |
AGE |
The age of the head of the household. |
RRSP |
The dollar value of Registered Retirement Savings Plan contributions in 1984, in 1984 dollars. |
MTR |
Marginal tax rate. The combined (federal/provincial) rate of income tax payable on the last dollar of the head of household’s income. |
REGION |
Region of residence. Takes the value 1 for Atlantic provinces; 2 for Quebec; 3 for Ontario; 4 for Prairie provinces; 5 for BC. |
Some frequently used STATA commands:
Command |
Use |
cd c:\ |
Changes directory to c:\ |
clear |
Clears the memory so that a new dataset can be loaded. |
dir |
Displays contents of current directory |
count |
Provides a count of the number of observations |
desc |
Shows variables currently in memory with description |
summ |
Shows means and standard deviations for all variables - can use ',detail' to get percentiles |
reg y x |
Runs OLS using y as dependent variable and x as independent variable |
gen |
Creates new variable |
replace |
Replaces old value with new value |
drop x |
Drops variable x |
keep x |
Drops all variables except for x |
drop if x==0 |
Drops all observations with x=0 |
keep if x==0 |
Keeps only those observations with x=0 |
dprobit |
Runs a probit regression, reports marginal probabilities |
use data.dta |
Brings dataset data.dta into memory |
save data.dta |
Saves dataset data.dta to disk (add ', replace') to overwrite existing data.dta |
table x |
Shows a table with the frequency distribution for variable x |
table x, c(mean y) |
Shows the mean of y for each value of x |
compress |
Compresses the dataset to take up the minimum amount of memory possible |
insheet x y using data.dat, t |
Brings variables x and y from ascii file data.dat (tab delimited) into memory |
log using econ390.log, t |
Creates a log file called econ390.log, text format |
log close |
Closes any open log file |
capture log close |
Closes any open log file, but doesn’t crash if there is no open log file |
real (x) |
Transfers variable x from text to numeric form. e.g. gen numbvar = real(textvar) |
2022-10-26