Wednesday, May 24, 2017

SAS Arrays (SAS)


Certain aspects of the SAS programming language do not comply with established programming paradigms. This is abundantly clear when utilizing SAS arrays. In this article, I hope to clarify how basic arrays can be utilized within the SAS programming language.

SAS allows you to define the number of elements within an array. However, I am not sure why you would ever want to do this. Instead, I would recommend using the ‘*’ (wildcard character) to define the number of array elements. This enables you to add array elements without having to manually re-count the number of elements each time a new array element is manually added.

We could create an array such as:

ARRAY <ArrayName> {*} /* Number of elements contained within the array */ <Array elements separated by spaces> ;

Do i = <number1> to <number2>; /* Creates and initiates the counter variable, unfortunately, you will need to manually count the array elements. The sum of elements is what the "i" value will equal. */


In this case, the array will scan for values within a data set, and create a variable flag if matching values are found.

If strip(<ARRAYNAME{i}>) in ('1' , '2', '3', '4') then FLAGVAR = 1;

End;

Run;


The strip function removes potential blank spaces from variable observations listed as array elements. This function is utilized to reduce potential errors when matching data values.

As an example, let us utilize the following code and data set:


Data Fruitsetb (drop=i);

Set Fruitseta;

ARRAY FruitArray {*} /* Number of elements contained within the array */ VAR1 VAR2 VAR3 VAR4 VAR5 VAR6 ;

Do i = 1 to 6; 
/* Creates and initiates the counter variable, unfortunately, you will need to manually count the array elements. The sum of elements is what the "i" value will equal. */ 

If strip(FruitArray {i}) in('Apples' , 'Oranges', 'Cherries') then FLAGVAR = 1;

End;

Run;


In the example, we are scanning across the variable columns (VAR1, VAR2, VAR3, VAR4, VAR5, VAR6) and looking for observations which contain (Apples OR Oranges OR Cherries). For each row observation which contains a match, a flag variable of 1 is generated. '(drop = i);' removes the newly created counter variable column.

The new set which is created from this code would resemble:


Since you are scanning across sequential variables, you could also utilize the following code and obtain the same results:

Data Fruitsetb (drop=i);

Set Fruitseta;

ARRAY FruitArray {*} /* Number of elements contained within the array */ VAR1-VAR6 ;

Do i = 1 to 6; 
/* Creates and initiates the counter variable, unfortunately, you will need to manually count the array elements. The sum of elements is what the "i" value will equal. */ 

If strip(FruitArray {i}) in('Apples' , 'Oranges', 'Cherries') then FLAGVAR = 1;

End;

Run;


This is just a basic introduction to arrays. SAS Arrays have many other uses which can accomplish more difficult tasks. For more information on SAS Arrays, please visit The SAS University website.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.