using loops in stata andtrendinsp.com/wp...uso_loops_en_r_y_stata_estandar.pdf · uses for loops...

15

Upload: others

Post on 25-Mar-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: USING LOOPS IN STATA ANDtrendinsp.com/wp...Uso_LOOPS_en_R_y_Stata_estandar.pdf · USES FOR LOOPS Loops are useful for the following tasks: • Generate variables and variable values
Page 2: USING LOOPS IN STATA ANDtrendinsp.com/wp...Uso_LOOPS_en_R_y_Stata_estandar.pdf · USES FOR LOOPS Loops are useful for the following tasks: • Generate variables and variable values

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

Page 3: USING LOOPS IN STATA ANDtrendinsp.com/wp...Uso_LOOPS_en_R_y_Stata_estandar.pdf · USES FOR LOOPS Loops are useful for the following tasks: • Generate variables and variable values

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"

Page 4: USING LOOPS IN STATA ANDtrendinsp.com/wp...Uso_LOOPS_en_R_y_Stata_estandar.pdf · USES FOR LOOPS Loops are useful for the following tasks: • Generate variables and variable values

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

}

Page 5: USING LOOPS IN STATA ANDtrendinsp.com/wp...Uso_LOOPS_en_R_y_Stata_estandar.pdf · USES FOR LOOPS Loops are useful for the following tasks: • Generate variables and variable values

CONDITIONS AND TYPES OF LOOPS

Condicional

• If

• If else

Loops

•For

•For each•While

•Do While

Page 6: USING LOOPS IN STATA ANDtrendinsp.com/wp...Uso_LOOPS_en_R_y_Stata_estandar.pdf · USES FOR LOOPS Loops are useful for the following tasks: • Generate variables and variable values

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

Page 7: USING LOOPS IN STATA ANDtrendinsp.com/wp...Uso_LOOPS_en_R_y_Stata_estandar.pdf · USES FOR LOOPS Loops are useful for the following tasks: • Generate variables and variable values

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

Page 8: USING LOOPS IN STATA ANDtrendinsp.com/wp...Uso_LOOPS_en_R_y_Stata_estandar.pdf · USES FOR LOOPS Loops are useful for the following tasks: • Generate variables and variable values

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

Page 9: USING LOOPS IN STATA ANDtrendinsp.com/wp...Uso_LOOPS_en_R_y_Stata_estandar.pdf · USES FOR LOOPS Loops are useful for the following tasks: • Generate variables and variable values

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

Page 10: USING LOOPS IN STATA ANDtrendinsp.com/wp...Uso_LOOPS_en_R_y_Stata_estandar.pdf · USES FOR LOOPS Loops are useful for the following tasks: • Generate variables and variable values

NESTED LOOPS

Useful when you need to do "sweeps" in different directions

Start

Counter statement

Evaluate the sequence

Counter increment

true

false

End

Page 11: USING LOOPS IN STATA ANDtrendinsp.com/wp...Uso_LOOPS_en_R_y_Stata_estandar.pdf · USES FOR LOOPS Loops are useful for the following tasks: • Generate variables and variable values

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

Page 12: USING LOOPS IN STATA ANDtrendinsp.com/wp...Uso_LOOPS_en_R_y_Stata_estandar.pdf · USES FOR LOOPS Loops are useful for the following tasks: • Generate variables and variable values

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

Page 13: USING LOOPS IN STATA ANDtrendinsp.com/wp...Uso_LOOPS_en_R_y_Stata_estandar.pdf · USES FOR LOOPS Loops are useful for the following tasks: • Generate variables and variable values

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

Page 14: USING LOOPS IN STATA ANDtrendinsp.com/wp...Uso_LOOPS_en_R_y_Stata_estandar.pdf · USES FOR LOOPS Loops are useful for the following tasks: • Generate variables and variable values

THANK YOU

Page 15: USING LOOPS IN STATA ANDtrendinsp.com/wp...Uso_LOOPS_en_R_y_Stata_estandar.pdf · USES FOR LOOPS Loops are useful for the following tasks: • Generate variables and variable values