|
Daniel,
Nested W--Do-loops (no, 'Do' does not stand for Dorfman here) are extremely
useful, and I use them often. However, in your application, they are not
called for. The reason for it is that for what you need to do, all indices
(two in your case) must be incremented in the body of the innermost loop,
while whenever nested loops are used, the outer index always stays put while
the inner loop iterates. As a result, if you nest two W-loops the way you
have, Genus will retain the same value for the duration of the Genus
By-group, whereas you want it to be incremented through. Note that in your
working code - which, by the way, is perfect for your purpose - the SET and
sum statements act independently, and n_species is knocked down to zero only
at the beginning of the Species By-group. You can still achieve the goal by
nesting W-loops, but only in a rather contrived way, and will still have to
have a separate sum statement. For example,
do n_genus = 0 by 0 until ( last.genus ) ;
do n_species = 1 by 1 until ( last.species ) ;
set <...> ;
by genus species ;
n_genus ++ 1 ;
output ;
end;
end;
Agree that your original code is more concise and transparent. Nested
W-loops are a wonderful tool when their programmatic structure fits the task
like a glove, but in this situation, it is not exactly the case.
Kind regards,
==================
Paul M. Dorfman
Jacksonville, FL
==================
> -----Original Message-----
> From: Kitzmann, Daniel J. [mailto:kitzmann.daniel@MAYO.EDU]
> Sent: Thursday, December 13, 2001 10:52 AM
> To: SAS-L@LISTSERV.UGA.EDU
> Subject: Nested Whitlock Do Loops?
>
>
> Dear Listers:
>
> Suppose I have a dataset sorted by two classificatory
> variables (genus and
> species) and I want to create a counter variable for each
> that increments
> within each by group. There are various ways to create such
> counters, of
> course, but I am curious if it can be done somehow with
> nested Whitlock do
> loops.
>
> For example, the following code works, employing the special
> do loop for the
> genus counter but reverting to the old-fashioned, bromidic
> if-then method of
> first.var logic for the species counter:
>
> data;
> do n_genus = 1 by 1 until (last.genus);
> set;
> by genus species;
> if first.species then n_species = 0;
> n_species ++ 1;
> output;
> end;
>
> Can both counters be created in a single data step employing
> nested special
> do loops? For instance, something perhaps in the form of
>
> data;
> do n_genus = 1 by 1 until (last.genus);
> do n_species = 1 by 1 until (last.species);
> . . . .
> end;
> end;
>
> I've dabbled with a few versions of the above, but I cannot
> think of a way
> to get it to work right.
>
> Thanks molto in advance for any clues or solutions.
>
> Cordially,
> Daniel Kitzmann
> kitzmann.daniel@mayo.edu
>
>
Blue Cross Blue Shield of Florida, Inc., and its subsidiary and
affiliate companies are not responsible for errors or omissions in this e-mail message. Any personal comments made in this e-mail do not reflect the views of Blue Cross Blue Shield of Florida, Inc.
|