SAS Macro Examples - Control Structures

Last updated:

WIP Alert This is a work in progress. Current information is correct but more content may be added in the future.

Macro If then Else

%if ... %then %do;
    ...
%end;
%else %if ... %then %do;
    ...
%end;
%else %do;
...
%end;

Macro do block

%do i = 0 %to &var %by 2;
    ...
%end;

Do block with value ranges

do i = 1 to 3 , 8, 20;
    put i= ;
end;

Do-while block

loop testing is done at the top of the loop

do while ( <expression is true> );
    ...
end;
Twitter Linkedin YC Hacker News Reddit

Do-until block

loop testing is done at the bottom so the loop is always iterated at least once

do until ( <expression is false> );
    ...
end;

Heads-up: If then Else

When data type numeric is used, zero and all missing values are false, and everything else is true.

Twitter Linkedin YC Hacker News Reddit

Subsetting if

An if-statement without a then clause, preferably at the end of a data step.

Dialogue & Discussion