Data analysis 2 (Basic loop)

Macros

command [action]
2.1. local macro-name value [ stores value under macro-name ]
2.2. sleep milliseconds [ wait milliseconds before executing next command ]
2.3. while expression [ executes command if expression is true]
2.4. forvalues macro-name = range [ executes command if value of macro is in range ]
2.5. foreach macro-name = list [ executes command for each element of macro ]

Make a local macro using a string (words):

    local name "John"

Print local macro "name":

    display "`name'"

Make a local macro using numbers:

    local age = 31

Print local macro age:

    display `age'

Print both local macro:

    display "`name' is `age' years old"

Basic loop using while true

    local i = 1

counter set to 1

    while `i'< 10 {
    display "hello world `i'"
    local i = `i'+ 1
    sleep 1000
    }

Basic loop using forvalues

    forvalues i = 1/10 {
    display "hello world `i'"
    sleep 1000
    }

Counting by 2:

    forvalues i = 1(2)10 {
    display "hello world `i'"
    sleep 1000
    }

Basic loop using foreach

Create list of juice

    local juice "apple orange grape"

loop over each element of list "juice"

    foreach i in `juice' {
    display "`i' juice"
    sleep 1000
    }