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 (June 2004, week 4)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Fri, 25 Jun 2004 23:30:30 -0400
Reply-To:     sashole@bellsouth.net
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         "Paul M. Dorfman" <sashole@BELLSOUTH.NET>
Organization: Sashole of Florida
Subject:      Re: IF conditions and ARRAY subscripts
Comments: To: Talbot Michael Katz <topkatz@MSN.COM>
In-Reply-To:  <200406252349.i5PNnBc17677@listserv.cc.uga.edu>
Content-Type: text/plain; charset="us-ascii"

TMK,

Chang is quite right. Whether an OR-chain shortcircuits or not depend on its operands. There are no rules on that account, one can only talk of probabilities. A comparison with a literal is likely to shortcircuit; a more or less involved function may not. However, you can make it DEAD SURE the chain shortcircuits by replacing the OR-chain with An IF-THEN-ELSE or SELECT chain.

Selecting the right array-sorting algorithm is a different matter, and there are many words that I could say about it, but let's just say that if your array does not exceed a couple of dozen elements, one has to try really hard to make a sorting routine really bad. ORDINAL (you already know) and just about any simple hand-coded sort (insertion, even bubble) will do OK. Now let us pause here just for the record and make a note that the ORDINAL-based method was posted on SAS-L by John Whittington circa April 1998:

http://www.listserv@listserv.uga.edu/cgi-bin/wa?A2=ind9804D&L=sas-l&P=R9398

so let us dispense credits where they are due. However, raise the dimension to 100 with a sufficient number of observations, and you would wish you had something else to rely upon, because ORDINAL and other O(N**2) algorithms begin to really create vacuum.

In V9, of course, there is CALL SORTQ if a non-temporary array is to be ordered, and a hash table with (ordered:'y') passed to the object constructor can be a fast path to an efficient sorting routine. If you are still in V9-, though, and still want an efficient sort, roll your sleeves up and get Knuth-dirty... or just cannibalize:

http://www2.sas.com/proceedings/sugi26/p096-26.pdf

Note that I have received A LOT of, hmmm... inquiries why the %Qsort() macro published there "does not seem to work". MEA CULPA!!! (Or actually maybe not. During the publishing process, the tail of the macro had been chopped off, darn it, so what would one expect?) The code in the demo Data step preceding the macro WORKS JUST FINE. Or if you need the macro, I have subsequently posted it on SAS-L; see

http://www.listserv@listserv.uga.edu/cgi-bin/wa?A2=ind0204D&L=sas-l&P=R12753

Kind regards, ---------------- Paul M. Dorfman Jacksonville, FL ----------------

> -----Original Message----- > From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On > Behalf Of Talbot Michael Katz > Sent: Friday, June 25, 2004 7:49 PM > To: SAS-L@LISTSERV.UGA.EDU > Subject: IF conditions and ARRAY subscripts > > Hi. > > I have two "equivalent" versions of an IF statement with two > conditions, but one of them doesn't work. > > I am interleaving two sorted arrays in a single observation > into sorted individual output observations in SAS v8.2 (so I > can't use v9 built-in array sorts), keeping only one element > in case of a tie between the two arrays (all elements are > unique within each array). I have a clunky algorithm to do > this -- if you have a good one, by all means pass it along, > although that's not the subject of this question. (I saw a > more elegant- looking algorithm in Cody's book, "SAS > Functions by Example," that sorts an array using repeated > calls to the ORDINAL function, but does the ORDINAL function > have to sort the array every time you call it?) > > Anyway, I have two jobs, one which worked and one which did > not (I can provide logs, if necessary). The only difference > between them is an IF statement; one uses the DIM function, > and the other uses a variable containing the result of the > DIM function. > > 1. if jlow > dim(md) or cd{i} < md{jlow} then do; > > 2. if jlow > dimd or cd{i} < md{jlow} then do; > > In the second case, I have already defined dimd = dim(md). > So, these IF statements should be equivalent, right? I have > the first condition to test whether one of the array > subscripts is out of bounds. I thought that the rule for an > IF statement with OR conditions was that it checks individual > conditions successively, and stops checking as soon as it > finds one that is true. That worked in the second case; jlow > was bigger than dimd, so it ignored the second condition. > However, in the first case, although jlow was bigger than > dim(md), I got an error message because the subscript jlow > was out of bounds for array md. > > Can anyone explain this to me? Thanks! > > -- TMK -- >


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