3. 🔁 Loops and program syntax#

  • Old notes

  • Alt notes (for Stata 13)

Key Learning Philosophy#

Important Perspective: We cannot exhaust the number of Stata commands available - there are simply too many, and it’s not worth your time to memorize them all. Instead, focus on mastering the fundamental programming concepts that will make you efficient and effective.

Core Focus: Macros and loops are by far the central part of Stata programming. These are the tools that will transform you from a basic Stata user into a sophisticated programmer.


Part 1: Understanding Macros#

What is a Macro?#

A macro is a way to store and reuse information in Stata. Think of it as a container that holds either:

  • Temporary information (local macros)

  • Persistent information (global macros)

Types of Macros#

Local Macros#

  • Exist only within the current session/program

  • Syntax: local macro_name = 1 or local macro_name "some string"

  • Called using: macro_name' (note the backtick and apostrophe)

Global Macros#

  • Persist across sessions and programs

  • Syntax: global macro_name : di "some string"

  • Called using: $macro_name

Macro Flexibility#

The way we assign string values to macros is highly flexible - it’s not just for strings! You can use macros for:

  • Numbers and calculations

  • Variable names

  • Command options

  • File paths

  • Complex expressions

Pro Tip: Use help local to discover the full range of possibilities.

Macro Persistence and Scope#

Key Concept: Understanding which macros are redefined when new commands are executed, and which remain unchanged.

  • Refreshed with commands: return list, ereturn list

  • Remains constant and defined system-wide: creturn list


Part 2: Cross-Platform Programming with Macros#

The Slash Issue Problem#

Different operating systems use different directory separators:

  • MacOS: / (forward slash)

  • Windows: \ (backslash)

Solution: Use Macros for OS Detection#

// Example - use macros to address the slash issue in different OS
if ("`c(os)'" == "MacOSX") {
    global slash : di "/"
}
else if ("`c(os)'" == "Windows") {
    global slash : di "\"
}

This approach makes your code portable across different operating systems.


Part 3: Understanding Loops#

What is a Loop?#

There are many definitions, but here’s a practical understanding:

Loops are certain chunks of code that are consecutively executed until a designated end has been reached.

Every single execution in the loop is called an iteration of the loop.

How to Establish a Loop in Stata#

Basic Approaches:

  1. forvalues - to loop through a certain range of values

  2. foreach - to loop through a certain list

  3. while - to loop until a certain condition is satisfied/unsatisfied

Key Elements of a Loop#

  1. Loop defining keywords (forvalues/foreach/while)

  2. A name of the macro used to store the changing values

  3. Brackets to define the loop’s scope


Part 4: Practical Loop Examples#

Target 1: Display Information Multiple Times#

Let’s say we want to display the string “Hello World” for 5 times.

Without Loop#

di "Hello World"
di "Hello World"
di "Hello World"
di "Hello World"
di "Hello World"

With Loop (Much More Efficient!)#

forvalues i = 1/5 {
    di "Hello World"
}

Best Practices for Learning Stata Programming#

Professional Development Approach#

  • Use .do files for all your work - this is the professional standard

  • Focus on practical applications - learn concepts through real-world examples

  • Develop systematic habits - consistent approach to coding and documentation

Effective Learning Strategies#

  1. Annotation: Always comment your code to explain what each section does

  2. Live Testing: Run and modify code iteratively to understand behavior

  3. Reproducible Workflow: Build code that others (and future you) can understand and run

Building Your Programming Skills#

  • Save and organize your code examples for future reference

  • Create a personal library of useful macros and loop structures

  • Practice translating repetitive tasks into efficient programmatic solutions


Key Takeaways for Graduate Students#

  1. Don’t memorize commands - focus on understanding programming logic

  2. Master macros and loops - these are your power tools

  3. Think about portability - write code that works across different systems

  4. Practice with .do files - this is how professionals work

  5. Use loops to avoid repetition - if you’re copying and pasting code, there’s probably a loop solution

Next Steps for Continued Learning#

  • Practice the examples and concepts covered in these notes

  • Experiment with different types of loops in your own data projects

  • Start incorporating macros into your regular Stata workflow

  • Build a personal collection of useful code snippets and templates