data temp; 
input id group age; 
cards; 
1 1 50 
2 1 27 
3 1 26 
4 0 49 
5 1 49 
6 0 48 
7 0 25 
; 
run; 
proc sql; 
create table comp as 
  select a.id as ida, a.group as groupa, a.age as agea, 
         b.id as idb, b.group as groupb, b.age as ageb, 
         abs(b.age-a.age) as gap_age 
  from (select * from temp where group=1) as a,  
       (select * from temp where group=0) as b 
  where (ageb-agea) between -5 and 5 
; 
quit; 
proc sort data=comp; 
by  idb; 
run; 
data comp; 
set comp; 
by idb; 
n=first.idb; 
run; 
proc sort data=comp; 
by  ida; 
run; 
data comp; 
set comp; 
by ida; 
m=first.ida; 
run; 
data comp(keep=ida agea idb ageb gap_age); 
set comp; 
if n=m; 
run; 
/*A :试验组  B:对照组   gap_age:年龄差*/ |