Hello,
I want to count the number of records that meet each pair of (variable and value) specified in a seperate file.
So for the 1st combination of a=4 and b=5, there are 2 record out of total 5 (with either a=4 or b=5).
I write a code that get it done but that approach is defintely not efficient. It take too long.
I guess there should be a different way.
Could you please help me with that?
Thanks a lot,
HHC
data have;
input date a b c d;
datalines;
1 4 4 9 5
2 4 4 5 9
3 4 5 5 0
4 3 6 8 9
5 3 5 9 0
6 4 5 9 2
7 6 0 9 1
;run;
data have_var;
input a_name $ a_value;
datalines;
a 4
b 5
c 9
d 9
;run;
*a sample for 1 combination of condition a=4 AND b=5 ;
data BOTH; set have;
if a=4 AND b=5 THEN DO;
BOTH=1;OUTPUT;END;
ELSE
if a=4 OR b=5 THEN DO;
BOTH=0;OUTPUT; END;
run;
proc MEANS data=BOTH noprint;
var both;
output out=N_same sum=N_same;
run;
data N_same; set N_same;
drop _TYPE_;
a_name="&a_name";
b_name="&b_name";
a_value=&a_value;
b_value=&b_value;
run;
*----This MAcro works but take too much time ------------------------------------------------------------------------;
data WANT; set xx; run; *the marco below will build all final result into the WANT_CORR;
%MACRO correlation(a_name=,a_value=,b_name=,b_value=);
data BOTH; set have;
if &a_name=&a_value AND &b_name=&b_value THEN DO;
BOTH=1;OUTPUT;END;
ELSE
if &a_name=&a_value OR &b_name=&b_value THEN DO;
BOTH=0;OUTPUT; END;
run;
proc MEANS data=BOTH noprint;
var both;
output out=N_same sum=N_same;
run;
data N_same; set N_same;
drop _TYPE_;
a_name="&a_name";
b_name="&b_name";
a_value=&a_value;
b_value=&b_value;
run;
data WANT; set WANT N_same; run;
%MEND;
*Create combination of any 2 condition;
data combination;
set have_var nobs=totalobs;
drop i ;
i+1;
do j=i+1 to totalobs ;
set have_var(keep = a_name a_value rename=(a_name=b_name a_value=b_value)) point=j;
output;
end;
run;
*Write code to temp file named "code";
filename code temp;
data _null_;
set combination end=last_cond;
file code;
put '%correlation (a_name=' a_NAME ',a_value=' a_VALUE ', b_name=' b_NAME ',b_value=' b_VALUE ');'
;run;
*Run the temp file;
%include code /source2;
... View more