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 (May 2009, week 1)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:   Mon, 4 May 2009 17:09:53 -0700
Reply-To:   "Richard A. DeVenezia" <rdevenezia@WILDBLUE.NET>
Sender:   "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:   "Richard A. DeVenezia" <rdevenezia@WILDBLUE.NET>
Organization:   http://groups.google.com
Subject:   Re: Count number of distinct value of an array
Comments:   To: sas-l@uga.edu
Content-Type:   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