Date: Tue, 5 May 2009 09:23:26 +0200
Reply-To: karma <dorjetarap@GOOGLEMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: karma <dorjetarap@GOOGLEMAIL.COM>
Subject: Re: Count number of distinct value of an array
In-Reply-To: <200905050306.n44JLs5Z029867@malibu.cc.uga.edu>
Content-Type: text/plain; charset=ISO-8859-1
Wow! Took a while for me to figure how it worked, but this is
brilliant as usual. I can't get this to work with temporary arrays, do
you know why? Also how did you chose the upper bound of the array
(256**2-1)?
I also have been relegated to using V8 at work :'( I had to use a hand
coded hash recently but decided on using the indexw function to
generate the key as I thought sparsely populated arrays might not be
so efficient, but it looks like it doesn't make any significant
difference.
Thanks
2009/5/5 Paul Dorfman <sashole@bellsouth.net>:
> Jerry,
>
> *IF* your values are really limited to 2 bytes, you can get what you need
> as
>
> data have ;
> input (x1-x8)($2. +1) ;
> cards ;
> V1 V2 V3 V1 V2 V4 V3
> V4 V4 V4 V3 V2 V2 V3
> V1 V4 V4 V4 V1 V1 V4
> ;
> run ;
>
> data need (drop = _:) ;
> array _x [0 : %eval(256**2-1)] ;
> set have ;
> array x x: ;
> do over x ;
> if missing (x) then continue ;
> _r = input (x, pib2.) ;
> if _x [_r] then continue ;
> n_uniq = sum (n_uniq, 1) ;
> _x [_r] = 1 ;
> end ;
> run ;
>
> Of course, if you look at it closely, you will see that this is yet another
> variation on the [degenerated] hash theme. Trying it on more than 2 bytes
> is not a good idea - the DATA step will take a long while to compile, and
> it may well run out of memory before that happens. In V9, the built-in hash
> showed in this thread is a better solution. If you are still on V8,
> converting the above to simple linear-probing hash (you can find plenty of
> code for that here on -L or SUGI proceedings).
>
> Since I have mentioned V8, I must say I had not believed anyone was still
> running it. Now that I have found myself consulting for a corporate part of
> an organization, whose wholly owned subsidiary (for which I have also
> consulted meanwhile) have been running V9 since at least 2004, I have to
> code in V8.2 again - without all those nice V9 things I have gotten used to
> over the years. Perhaps the only good thing about it is the ego mildly
> stroked by using hand-coded hash last presented in Long Beach. Other than
> that, going from V9 to V8 DOES. NOT. FAIL. TO. SUCK.
>
> Kind regards
> ------------
> Paul Dorfman
> Jax, FL
> ------------
>
>
> On Mon, 4 May 2009 11:08:29 -0700, Jerry <lihais@GMAIL.COM> wrote:
>
>>Dear SAS-L,
>>
>>I want to count the number of distinct value for several variables, I
>>tried to use array, but how to do that? Suppose I have variables x1-
>>x4 , I want to count distinct number of them. The desired data should
>>be like following:
>>
>>x1 x2 x3 x4 num
>>si ma ma fc 3
>>mn mn mn mn 1
>>si ma mn fc 4
>>
>>Any suggestion will be appreciated!
>>
>>Jerry
>
|