Proc SQL and the power of select into
I have referenced this paper many times. Great help in using the power of proc sql with macro variables. Great for building lists of values and dynamic code.
http://www.nesug.org/Proceedings/nesug97/coders/eddlesto.pdf
Syntax:
SELECT
object-item <,objectitem>...
<\ INTO macro-variablespecification <, macro-variablespecification> ...>
FROM from-list ...;
SAS DIM function – Counting the elements in an array
The DIM function returns the number of literal elements in an array. It functions against multi-dimensional arrays as well as one-dimensional arrays.
1-dimensional array example
DIM(array_name)
Multi-dimensional array examples
DIM(m_array) -> returns the number of elements in the first dimension of the array
DIM5(m_array) -> returns the number of elements in the 5th dimension of the array
DIM(m_array, 5) -> returns the number of elements in the 5th dimension of the array
The classic use case for the DIM function is to return the number of elements in an array for the upper bound of a do loop process. Example:
array array_name(5) var1 var2 var3 var4 var5;
do i=1 to dim(array_name);
some SAS statements here
end;
SAS arrays ( one dimensional )
A SAS array is a group of variables (or values) under a single name. The grouping is only temporary and only exists for the duration of the data step. There are many ways to declare an array. Additionally, arrays can be modified once they are created.
Here is one example of an array.
data new_ds;
set old_ds;
array test_array{5} test1 test2 test3 test4 test5;
do i=1 to 5;
sum_var+test_array{i};
end;
run;
Here are a few other ways to declare one-dimensional arrays:
Using the {*} designation allows SAS to determine the number of elements in the array.
array test_array{*} test1 test2 test3 test4 test5;
You can also use a range to specify values.
array test_array{5} test1-test5;
The first value doesn't have to be 1. You can specify the values if necessary.
array test_array{10:15} test1-test5;
You can also designate all the variables created into an array. NOTE: you cannot have both character and numeric variables in the same array.
array test_array{*} _NUMERIC_; ( All current numeric variables )
or
array test_array{*} _CHARACTER_; ( All current character variables )
or
array test_array{*} _ALL_; ( All current variables if they are of the same type )
There are also multi-dimensional arrays. We will discuss them in a later post.
If you need help working with arrays. We have an experience team of programmers available for short term help or contract engagements. Don't hesitate to contact us.
SAS Do loops – Do While
Here is yet another Do Loop post.
The basic form of a do loop is as follows:
Data new_ds;
Set old_ds;
Do some_index_var= 1 to 50 by 1;
Some SAS statements go here;
End;
run;
However, specifically in this post we want to talk about DO WHILE loops. Do while loops are different because they evaluate the condition at the top of the loop. Example:
Data new_ds;
Set old_ds;
Do while var > 100;
Some SAS statements;
End;
Run;
This loop will not execute the loop if the condition is not met upon the first execution. This is what makes WHILE so valuable. Conditional processes that allows you to exit the loop before the first iteration.
SAS Do Loop – Do Until
Do loops are great for a long list of tasks. Understanding do loops can save you an innumerable amount of programming and work arounds.
The basic form of a do loop is as follows:
Data new_ds;
Set old_ds;
Do some_index_var= 1 to 50 by 1;
Some SAS statements go here;
End;
run;
However, specifically in this post we want to talk about DO UNTIL loops. Do until loops are different because they evaluate the condition at the bottom of the loop. Example:
Data new_ds;
Set old_ds;
Do until var > 100;
Some SAS statements;
End;
Run;
This loop will execute the SAS statements at least one time and evaluate the var>100 condition at the bottom of the loop. If the condition is false, the loop will execute again.