Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 37 ยท Data Manipulation

SAS Arrays and DO Loops: Iterative, WHILE and UNTIL

Repeat work across many variables with SAS arrays and DO loops: iterative DO, DO WHILE and DO UNTIL, DIM, temporary arrays, character arrays and reshaping wide data to long.

Imagine a survey with twelve monthly sales columns that must all be cleaned the same way. Writing twelve nearly identical statements invites typos. SAS arrays and DO loops let you write the rule once and apply it to every column. This lesson covers both, plus the classic reshaping task of turning wide data into long data.

DO loops

Iterative DO

An iterative DO repeats a block a fixed number of times:

SAS
data growth;
   balance = 1000;
   do year = 1 to 5;
      balance = balance * 1.05;
      output;
   end;
run;

This creates five observations, one per year, because output writes inside the loop. The index variable (year) is a normal variable and is kept in the output. After a loop ends, the index holds the first value beyond the range (6 here), a surprise if you use it later. You can count down or step: do i = 10 to 1 by -1; and do x = 0 to 1 by 0.25;, or list values: do city = "Rome", "Oslo", "Lima";.

DO WHILE and DO UNTIL

When you do not know the number of repetitions in advance:

SAS
data doubling;
   amount = 100;
   years = 0;
   do while (amount < 200);       /* test BEFORE each pass */
      amount = amount * 1.07;
      years + 1;
   end;
run;

data doubling2;
   amount = 100;
   years = 0;
   do until (amount >= 200);      /* test AFTER each pass */
      amount = amount * 1.07;
      years + 1;
   end;
run;

DO WHILE checks the condition before every pass and may never run; DO UNTIL checks after, so it always runs at least once. A loop whose condition never becomes false runs forever; if that happens, interrupt the session and add a safety counter.

You can combine forms: do i = 1 to 100 while (total < 500);.

Arrays

An array is a temporary name for a group of variables that lets you refer to them by position. It exists only for the duration of the DATA step, and it is not a stored data structure.

SAS
data clean;
   set monthly;
   array m{12} jan feb mar apr may jun jul aug sep oct nov dec;
   do i = 1 to 12;
      if m{i} = -99 then m{i} = .;    /* -99 means "not recorded" */
   end;
   drop i;
run;

array m{12} ...; declares an array named m with 12 elements, which refer to the twelve variables in the order listed. m{i} is the i-th one, and square brackets or parentheses both work. Remember:

  • Arrays hold all numeric or all character variables. Declare character arrays with $: array names{3} $ n1 n2 n3;.
  • If the variables are numbered consecutively, abbreviate: array q{4} q1-q4; or let SAS count: array q{*} q1-q4;.
  • dim(m) returns the number of elements, which keeps loops correct when the array changes: do i = 1 to dim(m);.
  • _numeric_ and _character_ are special names for all variables of a type: array nums{*} _numeric_;.
  • Always drop the loop index if you do not need it.

Temporary arrays

array with _temporary_ creates elements that are not variables: they do not appear in the output and are retained automatically. Good for lookup tables:

SAS
data banded;
   array limits{4} _temporary_ (18 30 45 65);
   set people;
   band = 0;
   do i = 1 to dim(limits);
      if age >= limits{i} then band = i;
   end;
   drop i;
run;

Computing across columns

SAS
array scores{3} test1-test3;
total = sum(of scores{*});     /* of with array name and {*} */
best  = max(of scores{*});

Wide to long with arrays

Data is wide when a measurement repeats in many columns (jan, feb, ...) and long when each measurement is its own row. Many procedures need long data:

SAS
data long;
   set wide;
   array m{3} jan feb mar;
   do month = 1 to 3;
      sales = m{month};
      output;
   end;
   keep id month sales;
run;

Each input row becomes three output rows. The reverse (long to wide) is usually done with PROC TRANSPOSE, and wide to long can be done with it too; the array method gives you the most control.

Debugging loops

  • Print the index and values to the log with put i= m{i}=;.
  • Make sure output is inside or outside the loop on purpose.
  • Check dim() against the number of variables you meant to include. If the number of variables does not match, SAS stops with an error about the number of elements.
  • Remember the index value after the loop.

Array behaviour in Python

This Python stand-in (not SAS) shows a loop with recoding of -99 to missing, dim-style counting, and wide-to-long reshaping.

Python Python stand-in: arrays, DO loops and wide to long

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does 'array m{12} jan feb mar apr may jun jul aug sep oct nov dec;' do?

    An array is a grouping of existing variables for the length of the DATA step; it is not a stored structure.
  2. 2. What is the difference between DO WHILE and DO UNTIL?

    DO WHILE may not execute at all; DO UNTIL executes at least once.
  3. 3. What value does the loop index have right after 'do i = 1 to 5; ... end;' finishes?

    The index is incremented once more before the test fails, so it is 6.
  4. 4. Which function gives the number of elements in an array?

    dim(m) returns the number of elements in array m.
  5. 5. In a wide-to-long reshape with arrays, where is the OUTPUT statement placed?

    Each pass of the loop writes one long row.

Summary

  • do i = a to b by c; repeats a fixed number of times; do while tests first, do until tests last.
  • An array names a group of same-type variables for one DATA step: array m{12} ...; and m{i}.
  • Use dim(), _numeric_, _temporary_ and of arrayname{*} to write general code.
  • Place output inside a loop to reshape wide data to long.

Next: Combining Data Sets: SET, MERGE and IN=