Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research."— Presentation transcript:

1 Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research seminar, 15th January 2007

2 Project organisation in Stata Organisation of Stata-folders Do-files –profile.do –00_run_first.do Global macros Do-file templates Redirection of output

3 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)

4 Project folders e.g.d:/projects/bag/std/stata.. or d:/data/snc/stata.. good practice to seperate programs and data

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

6 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

7 Hint Forward or back slash? Windows systems: \ Mac: / and Stata? –both are possible –in do-files: always use: /

8 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

9 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

10 Project folders c:/seminar070115 /origdata /stata /data /do /graphres /orig /log /textres non-Stata files, e.g. mdb, dbf, xls, txt

11 Project folders c:/newproject /origdata /stata /data /do /graphres /orig /log /textres

12 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

13 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"

14 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

15 Macros (2) local macro: –is a substitute valid within a do-file –local thismacro = "gender" –regress bp `thismacro‘ –regress bp gender mind the quotes: –leading left single quote and trailing right single quote

16 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"

17 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)

18 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 (2)

19 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)

20 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 (4)

21 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

22 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

23 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

24 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

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

26 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

27 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

28 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

29 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 }

30 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

31 Organisation of do-files cr‘s: e.g. cr_sem01.do: do-file, where *.dta are created, normally after different data management steps includes: generating and dropping of vars, labeling, grouping... saves processed data file, ready for analyses

32 Organisation of do-files (2) an‘s: e.g. an_sem01.do: do-file, where analyses are performed attention: in an-do-files datasets are never changed! exception: temporary changes

33 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


Download ppt "Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research."

Similar presentations


Ads by Google