1 / 45

STATA User Group September 2007

STATA User Group September 2007. Shuk-Li Man and Hannah Evans. Content. MACROS -Description -Why use them? -How to store and useecall them -Applied examples LOOPS -Description -Why use them? -Applied examples-(using foreach, forval and while) MACROS WITH LOOPS

chadrick
Download Presentation

STATA User Group September 2007

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. STATA User Group September 2007 Shuk-Li Man and Hannah Evans

  2. Content • MACROS -Description -Why use them? -How to store and use\recall them -Applied examples • LOOPS -Description -Why use them? -Applied examples-(using foreach, forval and while) • MACROS WITH LOOPS -Why use them together? -Applied examples

  3. Aims of the Talk • An understanding of what macros and loops are and the principles behind using them. • Understand how loops and macros can be the solution to solve problems in a number of different in contexts. • Be able to apply macros and loops to your work

  4. MACROS

  5. What is a macro? A macro is a saved sequence of keyboard strokes that can be stored and then recalled with a single keyboard stroke. (ref: http://whatis.techtarget.com)

  6. Macros. Why use them? Macros Increases the scope of your do file How? Allows you to run the same do file on multiple datasets because you can get them to run under the conditions of the dataset.

  7. Macros in STATA • A sequence of keystrokes, the macrocontent are stored under a macroname (you can call it what ever you like). • Eitherlocal or globalmacros. - Alocal macro that is created in Stata can only be used within Stata. -A global macro made in Statacan be used or modified in other programs.

  8. Storing a macro (macro assignment) local animals dog cat mouse Here the macrocontent isdogcatmousestored under macronameanimals local sum =2+2 Here the macrocontent is4stored under macronamesum local add “=2+2” Here the macrocontent is=2+2stored under macronameadd

  9. Recalling a macro (after storing it) ` .’ macroname Single quotes are compulsory

  10. On the keyboard • left use buttons indicated in red circles • right use button indicated in green circle

  11. Using a macro “.” ` .’ macroname Double quotes are good practice

  12. Examples of recalling a macro-using display local animals dog cat mouse display “`animals’” . dogcatmouse local sum =2+2 display “`sum’” . 4 local add “=2+2” display “`add’” . =2+2

  13. Using Macros in different contexts In macro you can store…. • The categories/levels of a variable- e.g. gender, male or female • The output of a variable e.g. mean age, maximum height • Store the file names in a directory- e.g. all files in directory C:/datafiles

  14. Macros-Applied Example 1 • -Store the categories of a variable • Levelsof gender, local(genderlevels) • Stores the categories male and female from variable gender and stores it under the macronamegenderlevels • display “`genderlevels’” • . malefemale

  15. Macros-Applied Example 2 -Storing the output from a variable (1) Summarise age local k=r(max) di “`k’” . 96 * oldest person is 96

  16. Macros-Applied Example 3 -Storing the output from a variable (2) gen numword=wordcount(description) summarize numword . Variable Obs Mean Std. Dev. Min Max . numword 5 1 4 local k=r(max) di “ `k’ ” 4 * maximum number of words in the description is 4 and this is stored in macroname k

  17. Macros-Applied Example 4 -Store names of files in a directory GPRD example: Text files clinical1.txt, clinical2.txt & clinical3.txt stored in directory C:\GPRD files local mylist: dir “C:\GPRD files\” files “clinical*.txt” This stores all text files in directory “C:\GPRD files\” beginning with the word “clinical” and is Same as: cd “C:\GPRD files\” local mylist clinical1.txt clinical2.txt clinical3.txt You name

  18. LOOPS

  19. Loops. What is one? • A set of commands executed repeatedly for.. • 1. a list of elements (foreach) • or… • 2. a range of values (forval) • or… • 3. a specified condition (while) • (Not covered in this talk)

  20. Loops. Why use them? • Reduces length of do files • Reduces chances human error • Reduces checking time • Can reduce the amount of memory STATA needs to run a command • Increases the efficiency of a do file

  21. Loops. Example • Predictors of a prolonged HIV inpatient stay. • Factors include: • Ethnicity (categorical) • Clinical stage (3 indicator variables) • Age (continuous) • Immigration status (binary)

  22. Loops. Example 1: Forval forval num = 1/3 { tab clin_stage`num’ pro_stay } Same as: tab clin_stage1 pro_stay tab clin_stage2 pro_stay tab clin_stage3 pro_stay range lname –arbitrary name of range

  23. Loops. Example 1: Foreach foreach factor of varlist ethnicity immigration { recode `factor’ 99=. tab `factor’ pro_stay, row chi2 exact } Same as: recode ethnicity 99=. tab ethnicity pro_stay, row chi2 exact recode immigration 99=. tab immigration pro_stay, row chi2 exact lname – arbitrary name of the list

  24. Loops. Example 1: Foreach foreach factor of varlistethnicity immigration { recode `factor’ 99=. tab `factor’ pro_stay, row chi2 exact } Same as: recode ethnicity 99=. tab ethnicity pro_stay, row chi2 exact recode immigration 99=. tab immigration pro_stay, row chi2 exact lname – arbitrary name of the list Options: varlist, numlist, newvarlist, lmacname, gmacname

  25. Loops. Example 1: Foreach foreach factor of varlist ethnicity immigration { recode `factor’ 99=. tab `factor’ pro_stay, row chi2 exact } Same as: recode ethnicity 99=. tab ethnicity pro_stay, row chi2 exact recode immigration 99=. tab immigration pro_stay, row chi2 exact

  26. Loops. Example 2: Foreach GPRD example: • save all text files to a .dta format • append these files together. foreach x of clinical1 clinical2 clinical3 { insheet using `x’.txt save `x’, replace } Product: clinical1.dta, clinical2.dta and clinical3.dta

  27. Loops. Foreach-other examples Foreach x in { …. } Foreach x of numlist { …. } Foreach x of newlist { …. } Foreach x of `macroname’ { …. } General list e.g. filenames The same as using forval For strings previously unspecified in STATA This allows the loop to run through the list stored in a macro

  28. MACROS WITH LOOPS

  29. Macros and loops. Why use together? • Macros and loops advantage- Can use do file on multiple datafiles. • Loops advantage- Shorter less error prone do file • Loops advantage addition- Can get around functions that do not allow by() as a sub option

  30. Macros and Loops -Example 1: using Macros Example 1 Levelsof gender, local(levels) display “`levels’” malefemale Foreach x of `levels’ { tab1 smoking bmi if gender=“`x’” } Same as: tab1 smoking bmi if gender==“female” tab1 smoking bmi if gender==“male”

  31. Macros and Loops Example 2 HAVE WANT

  32. Macros and Loops Example 2 -using Macros Example 2 gen numbword=wordcount(description) summarise numbword . Variable Obs Mean Std. Dev. Min Max . numbword 5 1 4 local k=r(max) di “`k’” 4 Greatest number of words in the description is 4

  33. 2. Generate variables for each word forval i=1/`k' { gen word`i'=word( word, `i') }

  34. Macros and LoopsExample 2

  35. Macros and Loops usingMacros- Example 4 cont… 3. Generate variable for each type forval i=1/36 { gen type`i’=0 <-creates variables type1-type36 forval j=1/`k’ { replace type`i’=1 if real(word`j')==`i’<- line gives 1s to types`i’ for those words types that are present and ignores the words that are “&” -e.g. if type 36 present in original variable HPVtype then will change 0 to 1 in type36 variable } sum type`i’, mean Local k=r(max) <- stores maximum for type1-type36 drop type`i’ if “`k’”==0<-drops all those variables where maximum is zero • i.e. drops those types that are not in original variable “HPVtypes” } creates 36 variables called a variable all containing zero

  36. Macros and Loops Example 2-Output

  37. Macros and Loops Example 2 -updated dataset & 21 & 16

  38. Macros and LoopsExample 2 Problem: Want to append all files in a directory that begin with the word clinical

  39. REFRESHMacros- Example 5- -Store names of files in a directory Text files received from GPRD clinical1.txt,clinical2.txt and clinical3.dta stored in directory C:\GPRD files local mylist: dir “C:\GPRD files\” files “clinical*.txt" di “`mylist’” clinical1.txtclinical2.txtclinical3.txt

  40. REFRESHLoops-Foreach example 2 GPRD example: • save all text files to a .dta format • append these files together 1. foreach x of clinical1 clinical2 clinical3 { insheet using `x’.txt, clear save `x’, replace } Product: clinical1.dta, clinical2.dta and clinical3.dta

  41. Macros and Loops example 2 local mylist: dir “C:\GPRD files\” files “clinical*.dta” foreach x of `mylist’ { insheet using `x’.txt, clear save `x’, replace } gen row_no=_n sum row_no local k=r(max) drop in 1/`k’ foreach x of `mylist’ { append using `x’ save clinical_all, replace } *saves all text file with the name clinical at the beginning as .dta file * drops all observations from clinical3.dta * appends clinical1.dta, clinical2.dta, clinical3.dta together and saves as clinical_all.dta

  42. Macros and Loops Example 2 Result A file called clinical_all which contains all text files with file name starting with “clinical” from directory “C:\GPRD files\”.

  43. Go away today with… • An understanding of what macros and loops are and the principles behind using them. • Understand how loops and macros can be the solution to solve problems in a number of different in contexts. • Be able to apply macros and loops to your work

  44. And Hopefully…. BE PERSUADED THAT USING LOOPS AND MACROS IN DO FILES ARE REQUIRED FOR YOU TO WORK MORE EFFICEINTLY IN THE FUTURE AND BE MORE CONFIDENT THAT YOUR RESULTS ARE ALL CORRECT.

  45. ______________ Thank you

More Related