|
EpiTomboy@gmail.com wrote:
> Hi everyone. Some of my raw data has been entered in character format
> representing a range of numbers: 1-2 or 1-6 or 3-13 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=1-5 and v2=3-8, 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 "1-5" to be "1, 2, 3, 4, 5" and "3-8" 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 ;
3-4 1-5 1-2
1-4 5-6 2-8
10-30 4-6 5-8
19-25 20-50 1-100
;
Run ;
proc print; run;
*****output*****;
X1 X2 X3 olapx1x2 olapx1x3 olapx2x3
3-4 1-5 1-2 1 0 1
1-4 5-6 2-8 0 1 1
10-30 4-6 5-8 0 0 1
19-25 20-50 1-100 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
|