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 (July 2000, week 4)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Mon, 24 Jul 2000 12:08:00 +0200
Reply-To:     Jim Groeneveld <J.Groeneveld@ITGROUPS.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Jim Groeneveld <J.Groeneveld@ITGROUPS.COM>
Subject:      Re: Tidy up code
Comments: To: "Kerrison, James" <James.Kerrison@FMR.COM>
Content-Type: text/plain

Foster,

My rewrite of your code would be:

data allin ; merge one (in = yes) next2 ; by factor ; if yes ;

IF (uprice = d1) THEN DO; IF (uprice = d2) THEN b1 = 1; IF (uprice = d10) THEN b10 = 0; * Are you sure this must be 0? You say it works all right; * ( no ELSE IF here, possibly not exclusive ); * This has moved from the end to the beginning because of the inserted ELSE statements; END; if uprice < d2 then b1 = 1 ; %MACRO Uprice(); %DO I = 2 %TO 9; %* Because d1--d10 are ordered values the ELSE statement may be used; ELSE if d&I <= uprice < d%EVAL(&I+1) then b&I = 1 ; %END; %MEND Uprice; %Uprice; ELSE if uprice => d10 then b10= 1 ;

allfactor = sum (of b1-b10);

Another alternative would be (equivalent to initializing all b# variables to 0):

data allin ; merge one (in = yes) next2 ; by factor ; if yes ;

B1 = uprice = d1 = d2; IF B1 NE 1 /* from above */ THEN B1 = uprice < d2; %MACRO Uprice(); %DO I = 2 %TO 9; B&I = d&I <= uprice < d%EVAL(&I+1); %END; %MEND Uprice; %Uprice; B10 = uprice => d10; * B10 = NOT (uprice = d1 = d10); * NA, because it may become 1, where in the original code it would remain missing, see next line; IF uprice = d1 = d10 THEN B10 = 0; * Are you sure this must be 0 if true? You say it works all right;

allfactor = sum (of b1-b10);

All this code has NOT been tested, but its principles actually HAVE.

Regards - Jim. -- Y. (Jim) Groeneveld, MSc IMRO TRAMARKO tel. +31 412 407 070 senior statistician, P.O. Box 1 fax. +31 412 407 080 head IT department 5350 AA BERGHEM IMRO TRAMARKO: a CRO J.Groeneveld@ITGroups.com the Netherlands in clinical research

My computer likes BeOS over Windoesn't. I'll let it taste Linux. - Jim.

> -----Original Message----- > From: Kerrison, James [SMTP:James.Kerrison@FMR.COM] > Sent: Friday, July 21, 2000 8:20 PM > To: SAS-L@LISTSERV.UGA.EDU > Subject: Tidy up code > > Could somebody point me towards making this code more elegant? I am > getting > bogged down in trying to make it neater. Right now it works, but it looks > ugly. > > What I am trying to do is to assign a value to B1 depending on whether or > not the value of uprice falls within a range which is between the value of > D1 & D2, D2& D3... > > So if uprice falls within D1 and D2 then B1 gets populated by a 1.....The > code where uprice = d1 and d2 is to cover the situation where there was no > variation in price across the range of D1-D11. > > I tried looping, but it gets pretty messy - at least my rework does! > > Code: > > data allin ; > merge one (in = yes) next2 ; > by factor ; > if yes ; > > if (uprice = d2 and uprice = d1) then b1 = 1 ; > else if uprice < d2 then b1 = 1 ; > if uprice => d2 and uprice < d3 then b2 = 1 ; > if uprice => d3 and uprice < d4 then b3 = 1 ; > if uprice => d4 and uprice < d5 then b4 = 1 ; > if uprice => d5 and uprice < d6 then b5 = 1 ; > if uprice => d6 and uprice < d7 then b6 = 1 ; > if uprice => d7 and uprice < d8 then b7 = 1 ; > if uprice => d8 and uprice < d9 then b8 = 1 ; > if uprice => d9 and uprice < d10 then b9 = 1 ; > if uprice => d10 then b10= 1 ; > if uprice = d1 and uprice = d10 then b10= 0 ; > > allfactor = sum (of b1-b10); > > TIA, > > Foster Kerrison.


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