1 / 30

Project organisation in 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

nara
Download Presentation

Project organisation in Stata

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 • 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. 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"

  16. 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)

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

  18. 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

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

  20. How to start new project • Prepare Stata subdirecories (e.g. data, do, etc) • adapt 00_run_first.do for new project(define name, define project path) • execute 00_run_first.do • open template do-file, start writing commands in do-file => preparation of new project: <5 minutes

  21. 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

  22. 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

  23. 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

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

  25. 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

  26. 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

  27. 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

  28. 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 }

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

  30. 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

More Related