I showed a SAS/IML customer a debugging tip, and she said that I should blog about it because she had never seen it before. The tip is very simple: inside of a DO loop, use the MOD function to selectively print the values of variables.

Recall that the expression MOD(a,b) is zero when a is a multiple of b. Suppose that you have a DO loop that iterates from 1 to 100. You can do something every 10 iterations by using the MOD function, as follows:

```proc iml; do i = 1 to 100; x = sum(1:i); if mod(i,10)=0 then do; /* print every 10 iterations */ print i x; end; end;```

The example prints the values of x for when i equals 10, 20,..., 100. In a real program, you might want to print the current state of an iterative algorithm such as Newton's method to view partial results and determine whether the algorithm is converging.

This technique can be useful for debugging iterative algorithms, or for limiting the amount of output in the loop of a long-running program.

Share Distinguished Researcher in Computational Statistics

Rick Wicklin, PhD, is a distinguished researcher in computational statistics at SAS and is a principal developer of SAS/IML software. His areas of expertise include computational statistics, simulation, statistical graphics, and modern methods in statistical data analysis. Rick is author of the books Statistical Programming with SAS/IML Software and Simulating Data with SAS.

1. 