```Date: Wed, 7 Dec 2011 13:07:30 -0500 Reply-To: "Bian, Haikuo" Sender: "SAS(r) Discussion" From: "Bian, Haikuo" Subject: Re: Using Min and Max Values of a Single Variable To Create a New Variable Comments: To: R B In-Reply-To: Content-Type: text/plain; charset="us-ascii" Obviously like Toby has showed, SQL is the native tool for this task. Just in case you are wondering how this could be done using datastep, here is one approach: data have; infile cards; input x1; cards; 1 4 23 41 -3 0 ; data need (drop=_:); retain _min _max; do until (last); set have end=last; _min=min(_min, x1); _max=max(_max, x1); end; do until (last1); set have end=last1; x1_normalized=( X1 - _min ) / ( _max - _Min); output; end; run; proc print;run; Kindly Regards, Haikuo -----Original Message----- From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of R B Sent: Tuesday, December 06, 2011 7:20 PM To: SAS-L@LISTSERV.UGA.EDU Subject: Re: Using Min and Max Values of a Single Variable To Create a New Variable Perfect! Thanks! On Tue, Dec 6, 2011 at 6:59 PM, toby dunn wrote: > Proc SQL ; > Create Tale Need As > Select * , ( ( X1 - Min( X1 ) ) / ( Max( X1 ) - Min( X1 ) ) ) As > x1_normalized > From Have ; > Quit ; > > > > Toby Dunn > > > If you get thrown from a horse, you have to get up and get back on, unless > you landed on a cactus; then you have to roll around and scream in pain. > > "Any idiot can face a crisis-it's day to day living that wears you out" > ~ Anton Chekhov > > > > > Date: Tue, 6 Dec 2011 18:51:28 -0500 > > From: ryan.andrew.black@GMAIL.COM > > Subject: Using Min and Max Values of a Single Variable To Create a New > Variable > > To: SAS-L@LISTSERV.UGA.EDU > > > > Hi, > > > > My dataset is structured as: > > > > x1 > > -- > > 1 > > 4 > > 23 > > 41 > > -3 > > 0 > > > > Note that 41 is the maximum value and -3 is the minimum value. > > > > Within a datastep, I'd like to compute the values for a new variable, > > "x1_normalized", using the following equation: > > > > x1_normalized = [x1 - (minimum value of x1)] / [(maximum value of x1) - > > (minimum value of x1)] > > > > So, the value of the first case for "x1_normalized" would be: > > > > (1 - (-3)) / (41 - (-3)) = 4 / 44 = .090909090909 > > > > The value of the second case for "x1_normalized" would be: > > > > (4 - (-3)) / (41 - (-3)) = 7 / 44 = .15909090909 > > > > My new dataset should look like this: > > > > x1 x1_normalized > > -- -------------------- > > 1 .090909090909 > > 4 .159090909090 > > 23 etc. > > 41 etc. > > -3 etc. > > 0 etc. > > > > The MIN and MAX functions do not seem appropriate for what I'm trying to > do > > here. > > > > Any ideas? > > > > Thanks, > > > > Ryan > > ----------------------------------------- Email messages cannot be guaranteed to be secure or error-free as transmitted information can be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or contain viruses. The Centers for Medicare & Medicaid Services therefore does not accept liability for any error or omissions in the contents of this message, which arise as a result of email transmission. CONFIDENTIALITY NOTICE: This communication, including any attachments, may contain confidential information and is intended only for the individual or entity to which it is addressed. Any review, dissemination, or copying of this communication by anyone other than the intended recipient is strictly prohibited. If you are not the intended recipient, please contact the sender by reply email and delete and destroy all copies of the original message. ```

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