LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (May 1997, week 1)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Sat, 3 May 1997 11:19:36 EDT
Reply-To:     whitloi1@WESTATPO.WESTAT.COM
Sender:       "SAS(r) Discussion" <SAS-L@UGA.CC.UGA.EDU>
From:         Ian Whitlock <whitloi1@WESTATPO.WESTAT.COM>
Subject:      Re: ARRAY STATEMENT
Comments: To: taharris@IX.NETCOM.COM

Subject: ARRAY STATEMENT Summary: Arrays allow data values to specify variables. Respondent: Ian Whitlock <whitloi1@westat.com>

Terri Harris <taharris@IX.NETCOM.COM> wrote:

>I am an 'experienced' SAS user who must confess to having never used >an ARRAY statement. Could someone tell me how they use the statement, >what particular value or advantage as a data step shortcut it offers, >and so on? Much appreciated, Terri Harris

From an abstract point of view, SAS arrays allow data values to specify variables. This means that tedious code can often be exchanged by the introduction of data values to drive the code.

The principle can be important whenever

1) A few lines of code are needed for each variable and there are a lot of variables.

I have 36 problem flag FLAG1 - FLAG36. Set the flags to 0. I can write 36 statements

flag1 = 0 ; flag2 = 0 ; ....

or use

array flags (36) flag1 - flag36 ; do i = 1 to dim ( flags ) ; flag ( i ) = 0 ; end ;

2) The number of variables to change is specified in the data.

N is a number on the input data set. Change the first N flag variables to 1.

Now I can write 36 statements

if 1 <= n then flag1 = 1 ; if 2 <= n then flag2 = 1 ; ...

or use

do i = 1 to n ; flag ( i ) = 1 ; end ;

3) Which variable is specified in the data.

N2 is a number on the input data set. Change the flag variable corresponding to this number to 99.

I can write 36 statements

if n2 = 1 then flag1 = 99 ; if n2 = 2 then flag2 = 99 ; ....

or I write

flag ( n2 ) = 99 ;

This example if very revealing because it shows that an array can serve as a master if switch when some condition names the variable to be changed. I often find simple code cluttered up with many IF statements can be shortened using arrays because the IF statements are passing judgement on "is this the variable I want" instead of going directly to the variable.

In studying the above examples it should be intuitively clear that one can always eliminate the array at the expense of writing a lot more "make work" type code when the list of variables is known. Of course at some point it becomes simpler to learn about arrays.

Two of the three examples above require iterative DO-loops. I would like to see other examples of array usage that don't involve iterative DO-loops and conversely examples of iterative DO-loops that don't involve arrays.

Ian Whitlock <whitloi1@westat.com>


Back to: Top of message | Previous page | Main SAS-L page