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 (November 2010, week 4)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Sat, 27 Nov 2010 23:51:51 -0800
Reply-To:     Daniel Nordlund <djnordlund@FRONTIER.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Daniel Nordlund <djnordlund@FRONTIER.COM>
Subject:      Re: sum statement, if/then, loop
In-Reply-To:  <000001cb8eb8$bb2f5880$318e0980$@com>
Content-Type: text/plain; charset="utf-8"

> -----Original Message----- > From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of > bbser2009 > Sent: Saturday, November 27, 2010 8:57 PM > To: SAS-L@LISTSERV.UGA.EDU > Subject: sum statement, if/then, loop > > Running the code below makes all values of a2 to be 1000; > If I change "<" to ">", then all values of a2 are missing. > I do not know why. Please help. Thanks. > Max > > data a; > input a1; > a0+1; > array a{2}; > do i=1 to 2; > if a[i]<2 then a[i]+1000; > end; > drop i; > cards; > 1 > 3 > ; > proc print; > run;

What were you expecting to happen?

Since the array definition statement has no variables listed, SAS creates enough variables to assign to each element of the array, using the array name. So, SAS assigns a1 to the first element of the array and creates a2 for the second element of the array. Then in the data step, the IF statement

if a[i]<2 then a[i]+1000;

implicitly retains the array variables a1 and a2. The first time through the data step, the input statement assigns the value of 1 to a1, which is then incremented to 1001 in the DO loop (since 1 is less than 2). And, since the first time through the data step a2 is missing, a2 is incremented to 1000 (since missing is less than 2).

The second time through the data step, the input statement assigns 3 as the value of a1 and since 3 > 2 the value of a1 is not incremented in the DO loop. Now since a2 is retained, a2 has a value of 1000 and since 1000 is not less than 2, the value of a2 is not incremented and remains 1000.

Using the same process of following the changing values, you should be able to see why a2 takes on the value missing when you change the if statement to

if a[i]>2 then a[i]+1000;

Hope this is helpful,

Dan

Daniel Nordlund Bothell, WA USA


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