Date: Wed, 17 Dec 2008 16:58:01 -0500
Reply-To: Chang Chung <chang_y_chung@HOTMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Chang Chung <chang_y_chung@HOTMAIL.COM>
Subject: Re: comparing score range between variables
On Wed, 17 Dec 2008 13:19:55 -0800, Bill McKirgan <Bill.McKirgan@GMAIL.COM>
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;
hi, Bill,
here is one way of using abs() function. it is easy to write a simple
function (in 9.2) or a function-style macro. If you have many *many* such
pairs of vars, then you may need other solutions. hth.
cheers,
chang
/* test data */
data one;
input piq fsiq;
cards;
0 0
1 6
6 3
1 7
2 .
;
run;
/* a simple function-style macro to calc a flag */
%macro within5(var1, var2);
(ifn(missing(&var1) or missing(&var2)
, . %*-- missing --*;
, ifn(abs(&var1 - &var2)<=5, 1,0)))
%mend within5;
/* make flags */
data two;
set one;
piq5 = %within5(piq, fsiq);
run;
/* check */
proc print data=two noobs;
run;
/* on lst
piq fsiq piq5
0 0 1
1 6 1
6 3 1
1 7 0
2 . .
*/
|