using loops in stata andtrendinsp.com/wp...uso_loops_en_r_y_stata_estandar.pdf · uses for loops...
TRANSCRIPT
USING LOOPS IN STATA AND R
Presented by Carlos Pineda and Nerissa Nance
Grupo de Transparencia en la Dirección de Economía
Instituto Nacional de Salud Pública
Cuernavaca,México
22 Noviembre 2017
WHAT IS A LOOP?
"Automating a process of multiple actions by organizing
sequences of actions that group the parts that need to be repeated"
"A loop allows you to run a section of code several times until acertain condition is fulfilled and the loop stops"
EXAMPLE
year mtemp1 mtemp2 mtemp3 mtemp4 mtemp5 mtemp6 mtemp7 mtemp8 mtemp9 mtemp10 mtemp11 mtemp12
generate fmtemp1 = mtemp1*(9/5)+32
generate fmtemp2 = mtemp2*(9/5)+32
generate fmtemp3 = mtemp3*(9/5)+32
generate fmtemp4 = mtemp4*(9/5)+32
generate fmtemp5 = mtemp5*(9/5)+32
generate fmtemp6 = mtemp6*(9/5)+32
generate fmtemp7 = mtemp7*(9/5)+32
generate fmtemp8 = mtemp8*(9/5)+32
generate fmtemp9 = mtemp9*(9/5)+32
generate fmtemp10 = mtemp10*(9/5)+32
generate fmtemp11 = mtemp11*(9/5)+32
generate fmtemp12 = mtemp12*(9/5)+32
foreach v of varlist mtemp1-mtemp12 { generate f̀ v' = `v'*(9/5)+32
}
CONDITIONS AND TYPES OF LOOPS
Condicional
• If
• If else
Loops
•For
•For each•While
•Do While
Start
Execute the
action
End
trueHas the
condition
been met?
false
If If - else
Start
End
trueHas the
condition
been met?
false
Execute the alternative
action
CONDITIONS
Execute the action
WHILE
The loop will be repeated until a certain condition is no longer fulfilled.
Start
Evaluate the condition
Execute the
repeating code
true
false
End
DO-WHILE
This loop operates under the same concept as the while loop with the exception that do-while will
always run the code to repeat at least once
Start
Evaluate the condition
End
true
false
Execute the code
FOR
The loop is repeated a certain number of times.
This loop is a good option when the number of repetitions is
known and can be assigned by the programmer.
Start
Counter statement
Evaluate the sequence
Execute the
repeating code
Counter increment
true
false
End
NESTED LOOPS
Useful when you need to do "sweeps" in different directions
Start
Counter statement
Evaluate the sequence
Counter increment
true
false
End
EXAMPLES OF LOOPS IN STATA
But first…
• Macro: allows us to maintain or store a unique value
• Local: a macro that is only visible locally (i. E. In the same program, do file, script, session)... `varname’
• Global: a macro that is visible globally (i.e. Between different programs, do files, scripts, sessions etc.)… $varname
USES FOR LOOPS
Loops are useful for the following tasks:
• Generate variables and variable values
• Create interaction variables
• Adjust and analyze models using different values of a variable
• Modify variables using the same pattern
• Creating a macro that holds accumulated information
• Retrieving information stored in Stata
→ Understanding how to use loops helps you read other people's code as
well
Long, J. Scott. 2009. The Workflow of Data Analysis Using Stata. College Station, TX: Stata Press
LOOPS AND TRANSPARENCY
Using loops to improve
transparency in your coding:
• The less code you use, the less probability there is for aerror
• Your code will be cleaner, and therefore easier to read and understand
THANK YOU