100 likes | 229 Vues
In this session led by Elizabeth Garrett-Mayer, we will explore fundamental programming concepts in Stata, focusing on macros, looping, and writing commands. Attendees will learn about local and global macros, their scope, and practical examples of use, including how to shorthand repeated phrases and manage datasets. We will also cover different looping methods and the creation of commands in Stata. This workshop is designed for beginners and aims to enhance your programming skills for data analysis.
E N D
Computing for Research ISpring 2011 Stata Programming February 21 Primary Instructor: Elizabeth Garrett-Mayer
Some simple programming • Once again, princeton’s site has some great easy info: http://data.princeton.edu/stata/programming.aspx • We will discuss a few things: • ‘macros’ • looping • writing commands • We will not discuss ‘mata’: powerful matrix programming language
macros • macro = a name associated with some text. • macros can be local or global in scope. • Example of use: shorthand for repeated phrase • graphics title • set of ‘adjustment’ covariates
Example: covariates * use SCBC data use "I:\Classes\StatComputingI\SCBC2004.dta", clear * make tumor numeric and transform gen sizen=real(tumor) gen logsize = log(sizen) replace logsize = . if sizen==999 regress logsize age black graden *define local macro local adjusters age black graden regress logsize `adjusters' regress logsize `adjusters' i.ercat regress logsize `adjusters' i.prcat regress logsize `adjusters' i.ercati.prcat NOTE: must use accent (`) in upper left of keyboard as beginning quote and apostrophe (‘) (next to enter key) for end quote.
Example: titles * another example infile str14 country setting effort change /// using http://data.princeton.edu/wws509/datasets/effort.raw, clear graph twoway (lfitci change setting) /// (scatter change setting) /// , title("Fertility Decline by Social Setting") /// ytitle("Fertility Decline") /// legend(ring(0) pos(5) order(2 "linear fit" 1 "95% CI")) local gtitles title("Fertility Decline by Social Setting") ytitle("Fertility Decline") * with macro graph twoway (lfitci change setting) /// (scatter change setting) /// , `gtitles' legend(ring(0) pos(5) order(2 "linear fit" 1 "95% CI")) * without macro graph twoway (lfitci change setting) /// (scatter change setting) /// , legend(ring(0) pos(5) order(2 "linear fit" 1 "95% CI"))
Storing results * run regression and store r-squared value regress change setting local rsq = e(r2) display rsq * run new regression regress change setting effort display e(r2) see old saved r-squared display rsq * still there!
Global macros • Global macros have names of up to 32 characters and, as the name indicates, have global scope. • You define a global macro using global name [=] text and evaluate it using $name. (You may need to use ${name} to clarify where the name ends.) • “I suggest you avoid global macros because of the potential for name conflicts.” • A useful application, however, is to map the function keys on your keyboard. If you work on a shared network folder with a long name try something like this
More on macros • Macros can also be used to obtain and store information about the system or the variables in your dataset using extended macro functions. • For example you can retrieve variable and value labels, a feature that can come handy in programming. • There are also commands to manage your collection of macros, including macro list and macro drop. Type help macro to learn more.
Looping • foreach: loops over a set of variables • forvalues: loops over a set of values (index) • Also: • while loops • if and else sets of commands
Programming • ‘ado’ files • create commands in ado file and put them in the appropriate directory for Stata to find • Can also create them in do files for local use • See • http://data.princeton.edu/stata/programming.aspx • www.ssc.upenn.edu/scg/stata/stata-programming-1.ppt