Have a good command of Stata

Download Report

Transcript Have a good command of Stata

Project organisation in Stata
Adrian Spoerri and Marcel Zwahlen
Department of Social and Preventive Medicine
University of Berne, Switzerland
Research seminar, 15th January 2007
Project organisation in Stata
• Organisation of Stata-folders
• Do-files
– profile.do
– 00_run_first.do
• Global macros
• Do-file templates
• Redirection of output
Stata folders
•
•
•
•
System folders
often: c:/stata9 or c:/programme/stata9
updates and adofiles: c:/stata9/ado
=> never save anything in these folders
yourself!
• other system folders:
see sysdir (updates, ado-files)
Project folders
• Project folders
e.g.
d:/projects/bag/std/stata..
or
d:/data/snc/stata..
• good practice to seperate programs and data
General folders
• general Stata folder:
e.g. d:/projects/stata
(for copy of profile.do, lic file)
• d:/projects/temp
Jumping around directories
• Most common commands
– cd or pwd: shows current path
– dir: lists files and folders in current path
– cd: changes directory, eg. cd stata/do
Hint
Forward or back slash?
• Windows systems: \
• Mac: /
• and Stata?
– both are possible
– in do-files: always use: /
Pathways
• absoulte path: c:/seminar070115/stata/data
• relative path
– relative to what? check pwd or cd
– change directory to project, e.g.
cd c:/seminar070115
– then use relative path:
cd stata/data
Is there a third way?
• Use shortcuts!
• Why?
– define path to project only once
– valid for all do-files of same project
– makes collaboration of several persons on
the same project easy
– compatible with ISPM standard
Project folders
c:/seminar070115
/origdata
/stata
non-Stata files,
e.g. mdb, dbf, xls, txt
/data
/do
/graphres
/orig
/log
/textres
Project folders
c:/newproject
/origdata
/stata
/data
/do
/graphres
/orig
/log
/textres
Do-files
• most simple: list of Stata commands
• a bit more sophisticated: complex files with
loops, programs and subroutine-calls
• „Basic do-file“:
c:/stata9/profile.do => general settings
• runs each time Stata is starting
Profile.do
set scrollbufsize 500000
set memory 100M
set varlabelpos 20
* hard drive in use
global dr="d"
/* enlarge results window buffer */
/* sets memory to 100 megabyte */
/* sets position of label in variable
window */
Macros
• global macro:
– is a substitute valid during the whole stata
session
– global dd = "gender"
– regress bp $dd
– regress bp gender
• local macro: different syntax
Profile.do (2)
set scrollbufsize 500000
set memory 100M
set varlabelpos 20
* hard drive in use
global dr = "c"
*or
*global dr = "d"
/* enlarge results window buffer */
/* sets memory to 100 megabyte */
/* sets position of label in variable
window */
00_run_first.do
•
•
•
•
first do-file in each project
sets project-specific directories
start it with double click in the explorer
or start it using a shortcut (a global macro
again)
00_run_first.do (2)
qui
{
*define name of project
global np="Testproject for research seminar"
*define path to new project, here without drive letter
global pp="/seminar070115"
*general project path settings
global dd="$dr:$pp/stata/data"
global dod="$dr:$pp/stata/do"
global gd="$dr:$pp/stata/graphres"
global ld="$dr:$pp/stata/log"
global od="$dr:$pp/stata/orig"
global td="$dr:$pp/stata/textres"
}
display "settings ready for: $np"
cd $dr:$pp/stata
00_run_first.do (3)
global dd="$dr:$pp/stata/data"
global dd="d:$pp/stata/data"
global dd="d:/seminar070115/stata/data"
in analysis:
use $dd/example_1.dta
00_run_first.do (4)
qui
{
*define name of project
global np="Testproject for research seminar"
*define path to new project, here without drive letter
global pp="/seminar070115"
*general project path settings
global dd="$dr:$pp/stata/data"
global dod="$dr:$pp/stata/do"
global gd="$dr:$pp/stata/graphres"
global ld="$dr:$pp/stata/log"
global od="$dr:$pp/stata/orig"
global td="$dr:$pp/stata/textres"
}
display "settings ready for: $np"
cd $dr:$pp/stata
How to start new project
1. Prepare Stata subdirecories (e.g. data, do,
etc)
2. adapt 00_run_first.do for new project
(define name, define project path)
3. execute 00_run_first.do
4. open template do-file, start writing
commands in do-file
=> preparation of new project: <5 minutes
do-file template
capture log close
global logfile="$ld/cr_name_01.log"
log using "$logfile",replace
/*
- template of do-file
- describe here the main purpose of the do-file
authors: a.spoerri / m.zwahlen
date:
14.1.07
*/
use $od/dataset.dta, clear
*further commands
save $dd/dataset_prep.dta, replace
log close
exit
Example of do-file
capture log close
global logfile="$ld/cr_exp_01.log"
log using "$logfile",replace
/* example of do-file using global macros
authors:
a.spoerri / m.zwahlen
date:
14.1.07
*/
clear
Example of do-file (2)
*load data
use $od/example_1.dta, clear
*generate variable
gen index=(sex==1 & agegrp==50)
tab agegrp index
*save new file
save $dd/example_2, replace
log close
exit
Master do-file
• generally: seperate do-files where you create
a new dataset (cr's) and do-files, which just
analyse an existing dataset (an's)
• for each project create a master do-file
• e.g. master_seminar070115.do:
do "$dod/cr_sem01.do"
do "$dod/an_sem01.do"
/* creates cleaned data file*/
/* descriptive analyses */
Redirecting Stata to Word
•
•
•
•
create a text file with your results in Stata
link this file to a Word document
update text file (e.g. if data change)
update Word doc
Profile.do tr_on
* redirect part of the output to textres
capture program drop tr_on
program define tr_on
version 8
set logtype text
set linesize 120
quietly capture log close
local name="$td"+"/"+"`1'"+".txt"
quietly capture log using "`name'" , replace
end
Profile.do tr_off
* cancel redirection
capture program drop tr_off
program define tr_off
version 8
quietly capture log close
quietly capture log using "$logfile" , append
set linesize 175
end
Example of an_seminar_01.do
*description of diagnoses
forvalues z= 0/1 {
use "$dd/example_2.dta", clear
keep if sex==`z'
tr_on example_demogr_`z'
tab age educ, row
tr_off
}
Update Stata output in Word
•
•
•
•
useful for technical reports
output looks like Stata result
output is logged in folder .../textres
create link in Word file:
INCLUDETEXT
"C:\\seminar070115\\Stata\\textres\\example_demogr
_0.txt" \c AnsiText
Stata on the intranet
• Shortly, the following files will be available:
– ppt of our presentation
– template of profile.do
– template of standard do-file
– template of 00_run_first.do
– standard folder structure for new projects