project organisation in stata adrian spoerri and marcel zwahlen department of social and preventive...
TRANSCRIPT
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
/data
/do
/graphres
/orig
/log
/textres
non-Stata files, e.g. mdb, dbf, xls, txt
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 /* enlarge results window buffer */
set memory 100M /* sets memory to 100 megabyte */
set varlabelpos 20 /* sets position of label in variable
window */
* hard drive in use
global dr="d"
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 /* enlarge results window buffer */
set memory 100M /* sets memory to 100 megabyte */
set varlabelpos 20 /* sets position of label in variable
window */
* hard drive in use
global dr = "c"
*or
*global dr = "d"
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)
qui {*define name of projectglobal np="Testproject for research seminar"*define path to new project, here without drive letterglobal pp="/seminar070115"*general project path settingsglobal 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 (2)
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 (3)
qui {*define name of projectglobal np="Testproject for research seminar"*define path to new project, here without drive letterglobal pp="/seminar070115"*general project path settingsglobal 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 (4)
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 templatecapture log closeglobal logfile="$ld/cr_name_01.log"log using "$logfile",replace
/* - template of do-file- describe here the main purpose of the do-fileauthors: a.spoerri / m.zwahlendate: 14.1.07*/
use $od/dataset.dta, clear
*further commands
save $dd/dataset_prep.dta, replacelog closeexit
Example of do-filecapture log closeglobal logfile="$ld/cr_exp_01.log"log using "$logfile",replace
/* example of do-file using global macros authors: a.spoerri / m.zwahlendate: 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" /* creates cleaned data file*/do "$dod/an_sem01.do" /* 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_offprogram define tr_offversion 8
quietly capture log closequietly capture log using "$logfile" , appendset linesize 175
end
Example of an_seminar_01.do
*description of diagnosesforvalues z= 0/1 {
use "$dd/example_2.dta", clearkeep if sex==`z'tr_on example_demogr_`z'
tab age educ, rowtr_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