Computing for Research I Spring 2011

1 / 10

# Computing for Research I Spring 2011 - PowerPoint PPT Presentation

Computing for Research I Spring 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’

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' Computing for Research I Spring 2011' - gabby

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

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

*define local macro

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