Date: Mon, 4 May 2009 17:09:53 -0700 "Richard A. DeVenezia" "SAS(r) Discussion" "Richard A. DeVenezia" http://groups.google.com Re: Count number of distinct value of an array To: sas-l@uga.edu text/plain; charset=ISO-8859-1

On May 4, 4:16 pm, dorjeta...@GOOGLEMAIL.COM (karma) wrote: > Hi Paul, > > Liking the idea of the second set statement :-) , putting together > some of the good ideas so far, we can also do: > > 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 > ; > data want ; > set have; > array xs x:; > call sortc (of x: ) ; > do _n_ = 1 to dim(xs)-1; > if xs(_n_)=xs(_n_+1) then call missing(xs(_n_)); > end; > UniqueVals=countW(catx(' ',of x:)); > set have ; > run ; >

Here is a variant with a guard position in the array to simplify the looping, and no extraneous post looping function call. In other words, post sort a comparison alone is sufficient in determining an increment in the distinct accumulator.

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 ; data want ; set have; call sortc (of x: ) ; array xs guard x:; count = 0; do _n_ = 2 to dim(xs); count + (not missing (xs(_n_)) and xs(_n_) ne xs(_n_-1)); end; run ;

The problem with using SORT is that the original data order is lost. So if you want to retain the original order, you would need to sort a copy, or perhaps just use the HASH approach you showed. -- Richard A. DeVenezia http://www.devenezia.com

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