*Copyright @ www.mycsg.in;


Iterative %DO loops and %LOCAL %GLOBAL scope in SAS macros

Why iteration and scope matter in macros

Create sample data

Iterative %DO %TO %END loops

What is a %DO loop in a macro

Use a %DO loop to create one dataset per year

Use %BY to control the loop step size

%DO %WHILE and %DO %UNTIL loops

%DO %WHILE - repeat while a condition is true

%DO %UNTIL - repeat until a condition becomes true

Macro variable scope - %LOCAL and %GLOBAL

What is scope in SAS macros

Default scope behaviour - a variable leaks to global

Use %LOCAL to keep a variable private to the macro

Use %GLOBAL to explicitly create a session-wide variable

Combining a loop with local scope - a complete example

Key points to remember