Friday, April 1, 2011

DO OVER

An unadvertised form of the iterative Do group is the Do Over. It processes arrays just like the Index Variable group, but without having to specify the Index Variable or its control value. The total number of elements in the array is passed from the DATA Step to the Do Over. The coding forms below access their arrays exactly the same way: from element one (1) through element five (5).
Do Over Index Variable
Array Test X1-X5 (1,3,5,7,9);
Do over test;
Put test(_I_)=;
End;
Array Test (5) X1-X5 (1,3,5,7,9);
Do J = 1 to 5;
Put test(j)=;
End;
Both of these examples use the initial values option to create five variables, X1 – X5, and load numeric values to those variables.
The Do Over syntax is no longer documented by SAS, but it is still available since it was released in earlier versions of Base SAS. Info on its usage can be found in the SAS Global Forum proceedings at http://support.sas.com/events/sasglobalforum/previous/online.html. Just be aware that it can only be used with implicitly subscripted arrays, which is why Test doesn’t have a subscript in that example. Also, implicit arrays have the step-generated counter of _I_.
The benefit of the Do Over form can now be obtained with the Dim function, which returns the number of elements in an array. The ‘Do Over Test;’ shown above would be changed to ‘Do J = 1 to Dim(test);’ and the ‘Put test(_I_)=;’ would become ‘Put test(j)=;’.

No comments: