1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
data base_f;
input ind $ generation gpe gen_conjoint gpe_conjoint count;
datalines;
1 1960 1 1913 3 0
2 1960 2 1917 2 0
3 1960 3 1917 1 0
4 1960 3 1923 3 0
5 1960 3 1923 2 0
6 1960 1 1919 1 0
7 1960 2 1925 2 0
8 1960 1 1925 1 0
9 1960 1 1923 2 0
10 1960 3 1925 2 0
;
data base_h;
input ind_cj $ generation_cj gpe_cj gen_conjointe gpe_conjointe;
datalines;
1 1913 3 1960 3
2 1914 2 1960 3
3 1916 1 1960 1
4 1917 1 1960 1
5 1919 1 1960 -1
6 1921 1 1960 1
7 1921 1 1960 0
8 1922 3 1960 3
9 1922 1 1960 0
10 1923 2 1960 3
11 1923 1 1960 0
12 1923 1 1960 0
13 1923 1 1960 -1
14 1923 1 1960 -1
15 1924 1 1960 -1
16 1924 1 1960 1
17 1924 1 1960 0
18 1925 2 1960 1
19 1925 1 1960 1
20 1925 3 1960 3
;
data _null_;
if _n_=1 then do;
set base_f(obs=1); *make the variables in Cases data set available in the PDV;
*put the cases in a hash table;
declare hash cases(dataset:'base_f',hashexp:8,ordered:'a');
cases.definekey("ind");
cases.definedata("ind","count","generation", "gpe","gpe_conjoint");
cases.definedone();
declare hiter hi_cases('cases'); * declare a hash table iterator object;
declare hash matches(); *declare a hash table for matched cases and controls;
matches.definekey("ind","ind_cj");
matches.definedata("ind","ind_cj","generation", "gpe","gen_conjointe", "gpe_conjointe","gpe_cj","gpe_conjoint");
matches.definedone();
*declare a hash table for recording matched controls;
ident_cj_hash=ind;
declare hash m_control();
m_control.definekey("ident_cj_hash");
m_control.definedone();
m_control.clear();
end;
set base_h end=eof;
ident_cj_hash=ind_cj;
if (m_control.find()ne 0) then do;
rc=hi_cases.first();
do while(rc=0);
if (count<1 ) then do;
if (abs(generation-gen_conjointe)+ abs(gpe_conjoint-gpe_cj))=0 then do;
count+1;
cases.replace();
matches.add();
m_control.add();
leave;
end;
end;
rc=hi_cases.next();
end;
end;
done=1;
rc=hi_cases.first();
do while(rc=0);
if count<1 then do;
done=0;
leave;
end;
rc=hi_cases.next();
end;
*if all the cases are matched or run out of controls, output the resulting data sets;
if (done or eof) then do;
matches.output(dataset:"matches");
cases.output(dataset:"matched_cases");
m_control.output(dataset:"matched_controls");
stop;
end;
run;
proc sql;
create table diff_f as select a.* from base_f as a left join matches as b
on a.ind=b.ind
where b.ind is null;
quit;
proc sql;
create table diff_h as select a.* from base_h as a left join matches as b
on a.ind_cj=b.ind_cj
where b.ind is null;
quit;
data _null_;
if _n_=1 then do;
set diff(obs=1); *make the variables in Cases data set available in the PDV;
*put the cases in a hash table;
declare hash cases(dataset:'diff',hashexp:8,ordered:'a');
cases.definekey("ind");
cases.definedata("ind","count","generation", "gpe","gpe_conjoint");
cases.definedone();
declare hiter hi_cases('cases'); * declare a hash table iterator object;
declare hash matches(); *declare a hash table for matched cases and controls;
matches.definekey("ind","ind_cj");
matches.definedata("ind","ind_cj","generation", "gpe","gen_conjointe", "gpe_conjointe","gpe_cj","gpe_conjoint");
matches.definedone();
*declare a hash table for recording matched controls;
ident_cj_hash=ind;
declare hash m_control();
m_control.definekey("ident_cj_hash");
m_control.definedone();
m_control.clear();
end;
set diff_h end=eof;
ident_cj_hash=ind_cj;
if (m_control.find()ne 0) then do;
rc=hi_cases.first();
do while(rc=0);
if (count<1 ) then do;
if (abs(generation-gen_conjointe)+ abs(gpe_conjoint-gpe_cj))=1 then do;
count+1;
cases.replace();
matches.add();
m_control.add();
leave;
end;
end;
rc=hi_cases.next();
end;
end;
done=1;
rc=hi_cases.first();
do while(rc=0);
if count<1 then do;
done=0;
leave;
end;
rc=hi_cases.next();
end;
*if all the cases are matched or run out of controls, output the resulting data sets;
if (done or eof) then do;
matches.output(dataset:"matches1");
cases.output(dataset:"matched_cases");
m_control.output(dataset:"matched_controls");
stop;
end;
run; |
Partager