• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / SAS / Do Loop in SAS Macro Language

Do Loop in SAS Macro Language

April 8, 2022 Leave a Comment

When using the SAS Macro language, we can easily create do loops. The syntax for a SAS Macro do loop is shown below.

%macro example;
    %do i = 1 %to 10;
        /* do stuff here */
    %end;
%mend;

The SAS Macro language is incredibly powerful and allows us to build dynamic programs.

One common piece of code fundamental to all programming languages is the loop. In the SAS Macro language, the main loop is a do loop.

We can use do loops to dynamically create new data and variables in a loop.

The syntax for a SAS macro do loop is shown below.

%macro example;
    %do i = 1 %to 10;
        /* do stuff here */
    %end;
%mend;

One example of using a do loop would be if you have a list of words that you want to loop over. Below is an example of how to loop over a list of words with the sas macro scan function.

%let string = This is a string of words.;

%macro scan_example;
%do i = 1 %to %sysfunc(countw(&string));
    %let current_word = %scan(&string,&i);
    %if &current_word = string %then %do;
        /* Do stuff here */
    %end;
%end;
%mend;

SAS Macro Do Loops with Increment

To create a do loop with an increment, all we need to do is add ‘%BY’ and the increment to the first line of the do loop.

Using an increment gives us the ability to go backwards from a starting point to stopping point, or step through the range of numbers with a greater step than just 1.

Below is an example of how to create a SAS macro do loop with an increment.

%macro example;
    %do i = 1 %to 10 %by 2;
        /* do stuff here */
    %end;
%mend;

Do Until Loops in a SAS Macro

Another kind of loop in the SAS Macro language you can use is a do until loop. Just like the name states, you can loop until a certain condition is no longer true.

Note, do until loops check the condition at the END of the iteration – therefore a do until loop will always iterate at least once.

Below is an example of how to create a do until loop in SAS with the SAS Macro language.

%macro example;
    %let i = 1;
    %do %until &i > 10;
        /* do stuff here */ 
        %let i = %eval(&i+1);
    %end;
%mend;

Hopefully this article has been useful for you to learn how to create do loops in your SAS macros.

Other Articles You'll Also Like:

  • 1.  SAS nodupkey – How to Remove Duplicates with PROC SORT by Key
  • 2.  SAS round – Rounding Numbers in a SAS Data Step
  • 3.  SAS floor – Round Down to Floor of Number in a SAS Data Step
  • 4.  How to Combine Datasets Vertically in SAS
  • 5.  SAS ceil – Round Up to Ceiling of Number in a SAS Data Step
  • 6.  intcx SAS – Find Time Periods Between Two Dates in SAS Data Step
  • 7.  SAS include – Execute Code from Other Files in SAS with %include
  • 8.  SAS Not In – How to Check if Variable is Not in List of Values
  • 9.  SAS Not Equal – Check if a Variable is Not Equal to Another in Data Step
  • 10.  SAS rand – Generate Random Numbers in a SAS Data Step

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy