Date: Fri, 7 Jul 2006 11:12:01 0700
ReplyTo: shiling99@YAHOO.COM
Sender: "SAS(r) Discussion" <SASL@LISTSERV.UGA.EDU>
From: shiling99@YAHOO.COM
Organization: http://groups.google.com
Subject: Re: Recognizing Overlap in Ranges of Values
InReplyTo: <1152215488.820483.321860@m73g2000cwd.googlegroups.com>
ContentType: text/plain; charset="iso88591"
EpiTomboy@gmail.com wrote:
> Hi everyone. Some of my raw data has been entered in character format
> representing a range of numbers: 12 or 16 or 313 etc. I want to use
> SAS to compare multiple variables within an observation and keep only
> the records that have overlapping values for those variables. That is,
> if within one observation v1=15 and v2=38, then I would need to keep
> it because the values of the two variables have 3, 4 and 5 in common.
> I was thinking I could recode "15" to be "1, 2, 3, 4, 5" and "38" to
> be "3, 4, 5, 6, 7, 8" (which would be ugly enough) and ultimately use
> something like a simple where statement, i.e.
If you know how many range before hand, then the problem may be a lot
of easier if the number is very small, say 5. Otherwise you may need
to consider to use macro to deal with a general case.
Here is a solution to the general case.
The idea follows,
given ranges [a,b] and [c, d], if c or d is in the range [a, b], then
it is overlapped. Similar for given ranger [c, d] then check for a and
b.
HTH
%macro compolap(var1, var2);
_varb1=Input( Scan( &var1 , 1 , '' ) , best. ) ;
_vare1=Input( Scan( &var1 , 2 , '' ) , best. ) ;
_varb2=Input( Scan( &var2 , 1 , '' ) , best. ) ;
_vare2=Input( Scan( &var2 , 2 , '' ) , best. ) ;
if _varb1<= _varb2 <=_vare1 then olap&var1.&var2=1;
else if _varb1<= _vare2 <=_vare1 then olap&var1.&var2=1;
if _varb2<= _varb1 <=_vare2 then olap&var1.&var2=1;
else if _varb2<= _vare1 <=_vare2 then olap&var1.&var2=1;
else olap&var1.&var2=0;
drop _var:;
%mend;
Data t1;
Input X1 $ X2 $ X3 $ ;
%compolap(x1, x2)
%compolap(x1, x3)
%compolap(x2, x3)
Cards ;
34 15 12
14 56 28
1030 46 58
1925 2050 1100
;
Run ;
proc print; run;
*****output*****;
X1 X2 X3 olapx1x2 olapx1x3 olapx2x3
34 15 12 1 0 1
14 56 28 0 1 1
1030 46 58 0 0 1
1925 2050 1100 1 1 1
>
> where v1 contains v2;
>
> but "contains" won't quite cut it because I need to keep the records
> with any overlap at all, not just where one is completely contained in
> the other.
>
> Can anyone help?
>
> Thanks.
>
> Anna
