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 2012, week 2)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:   Wed, 9 May 2012 09:36:19 -0500
Reply-To:   Joe Matise <snoopy369@GMAIL.COM>
Sender:   "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:   Joe Matise <snoopy369@GMAIL.COM>
Subject:   Re: need helps for data mamipulation
Comments:   To: ethan zhou <e75wez1@gmail.com>
In-Reply-To:   <CAKATOfpRGwYSr63zToawFUCJzk8RAk5CVHaN=4gBHVfCCU2inQ@mail.gmail.com>
Content-Type:   text/plain; charset=ISO-8859-1

I recommend the SAS product documentation if you encounter a new function, that part of it is perhaps the most useful :)

In this case, whichn selects one of N values based on the value (presumably in a variable) provided in the first parameter.

So:

x=4; y = whichn(x, 1,4,9,16,25,36); will give you a value of 16 in y, while x=2; y = whichn(x, 1,4,9,16,25,36); will give you a value of 4 in y.

http://support.sas.com/documentation/cdl/en/lefunctionsref/63354/HTML/default/p0zs0pv38mel2jn1in4lte2akx4d.htm

WHICHC is the character version of WHICHN, so it still takes a number for the first parameter but it returns a character value.

-Joe

On Wed, May 9, 2012 at 9:24 AM, ethan zhou <e75wez1@gmail.com> wrote:

> Mike, > Your solution is so simple. I like it. > > But I'm not familiar the SAS function "whichn" . Could you please explain > it to me? > > Also in my real data set, the variable names are like " cfed11 cfed12 > cfed13, pct2 pct3 pct4" other than "product1 product2 product3, pct1 pct2 > pct3" with consecutive numerical suffix. > I wonder if one can use your code without renaming those variables to > "product1 product2 product3, pct1 pct2 pct3". > > Thanks for your helps. > > Ethan > On Tue, May 8, 2012 at 2:14 PM, bbser 2009 <bbser2009@gmail.com> wrote: > > > Below are two alternative algorithms to do this. > > The first one is using bubble sorting. > > The second one is based on the ad hoc method of sorting three values, > just > > for fun. > > > > Regards, Max > > (Maaxx) > > > > data have1 have2; > > input ID product1 $ product2 $ product3 $ pct1 pct2 pct3; > > cards; > > > > 1 C D A 20 50 30 > > 2 B A E 55 40 5 > > 3 A C B 0 0 100 > > 4 F E B 40 50 10 > > 5 A D E 40 20 40 > > ; > > > > ***Bubble Sorting; > > data have1; > > modify have1; > > array product{3} $ 1; > > array pct{3}; > > dim=dim(pct); > > do i=1 to dim-1; > > do j=2 to dim; > > pcti=pct{i}; > > pctj=pct{j}; > > if pcti<pctj then > > do; > > pct{j}=pcti; > > pct{i}=pctj; > > temp_product=product{i}; > > product{i}=product{j}; > > product{j}=temp_product; > > end; > > end; > > end; > > run; > > proc print;run; > > > > ***Ad hoc sorting; > > data have2; > > modify have2; > > array product{3} $ 1; > > array pct{3}; > > array pr{3} $ 1 ; > > array pc{3}; > > do i=1 to 3; > > pr{i}=product{i}; > > pc{i}=pct{i}; > > end; > > pct1=max(of pc{*}); > > pct3=min(of pc{*}); > > pct2=100-pct1-pct3; > > do i=1 to 3; > > do j= 1 to 3; > > if pct{i}=pc{j} then > > do; > > product{i}=pr{j}; > > pc{j}=-1; > > leave; > > end; > > end; > > end; > > run; > > proc print;run; > > > > > > -----Original Message----- > > From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of > Ethan > > Zhou > > Sent: May-07-12 1:46 PM > > To: SAS-L@LISTSERV.UGA.EDU > > Subject: [SAS-L] need helps for data mamipulation > > > > Hi all, > > > > I have a data with up to 3 different products associated with their > > percentages usage respectively. Those percentages add up to 100%. > > > > > > How to horizontally sort the product1, product2 and product3 based on > > their percentage value in descending order? > > > > Here is the sample data: > > > > data have; > > input ID product1$ product2 $ product3 $ pct1 pct2 pct3; > > cards; > > > > 1 C D A 20 50 30 > > 2 B A E 55 45 5 > > 3 A C B 0 0 100 > > 4 F E B 40 50 0 > > 5 A D E 40 20 40 > > ; > > > > The wanted results: > > Id product1 product2 product3 pct1 pct2 pct3 > > 1 D A C 50 30 20 > > 2 B A E 55 45 5 > > 3 B A C 100 0 0 > > 4 E F B 50 40 0 > > 5 A E D 40 40 20 > > > > > > > > Thank you so much for your help. > > > > John > > >


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