```Date: Thu, 18 Dec 2008 07:21:16 -0800 Reply-To: Bill McKirgan Sender: "SAS(r) Discussion" From: Bill McKirgan Organization: http://groups.google.com Subject: Re: comparing score range between variables Comments: To: sas-l@uga.edu Content-Type: text/plain; charset=ISO-8859-1 On Dec 17, 9:34 pm, hs AT dc-sug DOT org ("Howard Schreier)" wrote: > On Wed, 17 Dec 2008 13:19:55 -0800, Bill McKirgan > wrote: > > > > > > >Hi SASers, > > >I'm trying to think of a more elegant (read shorter) way of coding the > >example I provide below. In the example I am comparing a variable > >called FSIQ with another variable PIQ to see if PIQ is within plus/ > >minus 5 points of FSIQ. This works, but I need to apply it to a bunch > >of other variables, and would like to avoid a large copy/paste/modify > >job that may cause me to introduce errors. > > >Can anyone suggest one or more shorter ways of doing this? Perhaps an > >array? Any help would be greatly appreciated. I'm stumped. Many > >thanks in advance. > > >-- Bill > > >data testing; set itbs.iq_data_final; > > >piq_within_5pts = 0; > > >if fsiq = piq then piq_within_5pts = 1; else > > >if fsiq = piq + 1 then piq_within_5pts = 1; else > >if fsiq = piq + 2 then piq_within_5pts = 1; else > >if fsiq = piq + 3 then piq_within_5pts = 1; else > >if fsiq = piq + 4 then piq_within_5pts = 1; else > >if fsiq = piq + 5 then piq_within_5pts = 1; else > > >if fsiq = piq - 1 then piq_within_5pts = 1; else > >if fsiq = piq - 2 then piq_within_5pts = 1; else > >if fsiq = piq - 3 then piq_within_5pts = 1; else > >if fsiq = piq - 4 then piq_within_5pts = 1; else > >if fsiq = piq - 5 then piq_within_5pts = 1; > > >put fsiq piq @20 piq_within_5pts ; > > >run; > > I trust that there are no missing values. Bill's code actually returns a 1 > (true) result if both variables are missing; probably not wanted. > > Here is a technique which is almost exactly equivalent, but is much shorter > and generates a 0 (false) given two missing values: > > piq_within_5pts = (piq - fsiq) in (-5 : 5);- Hide quoted text - > > - Show quoted text - Howard, That is a fantastic solution. I never would have thought of that. Yes, there are missing values in our data and I would have tried to handle it in a different way that would involve many more lines of code. This is very elegant. Thank you. Now I have to laugh that my mind was so blocked yesterday that I did not think of using subtraction wiht the abs() function as Joe and Chang suggested. Sometimes I fail to consider alternatives and it is wonderful to be able to ask for help here on SAS-L Thanks everyone! Bill ```

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