30
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 Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

Embed Size (px)

Citation preview

Page 1: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

Project organisation in Stata

Adrian Spoerri and Marcel Zwahlen

Department of Social and Preventive Medicine

University of Berne, Switzerland

Research seminar, 15th January 2007

Page 2: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

Project organisation in Stata

• Organisation of Stata-folders• Do-files

– profile.do– 00_run_first.do

• Global macros• Do-file templates• Redirection of output

Page 3: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

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)

Page 4: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

Project folders

• Project folders

e.g. d:/projects/bag/std/stata..

or d:/data/snc/stata..• good practice to seperate programs and data

Page 5: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

General folders

• general Stata folder: e.g. d:/projects/stata(for copy of profile.do, lic file)

• d:/projects/temp

Page 6: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

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

Page 7: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

Hint

Forward or back slash?• Windows systems: \• Mac: /• and Stata?

– both are possible– in do-files: always use: /

Page 8: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

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

Page 9: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

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

Page 10: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

Project folders

c:/seminar070115

/origdata

/stata

/data

/do

/graphres

/orig

/log

/textres

non-Stata files, e.g. mdb, dbf, xls, txt

Page 11: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

Project folders

c:/newproject

/origdata

/stata

/data

/do

/graphres

/orig

/log

/textres

Page 12: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

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

Page 13: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

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"

Page 14: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

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

Page 15: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

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"

Page 16: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

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)

Page 17: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

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)

Page 18: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

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)

Page 19: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

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)

Page 20: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

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

Page 21: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

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

Page 22: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

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

Page 23: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

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

Page 24: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

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 */

Page 25: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

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

Page 26: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

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

Page 27: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

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

Page 28: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

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}

Page 29: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

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

Page 30: Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research

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