SAS versus R : using arrays

In a general programming language, an array is considered to be a data structure which stores variables of similar data type. In SAS, this is not the case. An array is only a compile-time statement and thus, it will not exist outside the scope of the calling data step.

In R, arrays are created from a data structure called vectors using a c() function. This function combines values of similar data types. Once a vector is created, the vector name is passed as a value to an “array” function.

Example in SAS

data temp;

array color_array{3} $8 ( ‘Purple’, ‘Brown’, ‘Red’ );
putlog “Second element in the array is ” color_array{2};


Example in R

> colors <- c( ‘Purple’, ‘Brown’, ‘Red’)
> print ( colors )
[1] “Purple” “Brown” “Red”
> color_array <- array ( colors )
> print ( class ( color_array ))
[1] “array”
> print( color_array[2] )
[1] “Brown”


SAS versus R : using arrays

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s