Tuesday, July 1, 2014

Combinations


/*****************************************************************************/

/***************************** Overlap Code **********************************/

/*****************************************************************************/

 

libname sri "E:\Srikanth";

 

option nomprint nomlogic;

options compress=yes;

%let data_set_name=sri.data_set;

%let var=T1 T2 T3 T4 T5 T6 T7 T8;

%let r_value=5;

 

*This macro, called combo(r), when execute will create a combination of n taken at a time r.;

%macro combo(r);

       data combo;

            keep v1-v&r.;

            array word $&maxl.  w1-w&n. (&things.);

            array rr (*) r1-r&r.;

            array v $&maxl.  v1-v&r.;

 

            %do i=1 %to &r.;

                %if &i.=1 %then %do;

               do r&i.=1 to &n.-(&r.-&i.);

               %end;

             %else %do;

               do r&i.=r%eval(&i.-1)+1 to &n.-(&r.-&i.);

               %end;

             %end;

               do k=1 to &r.;

               v(k)=word (rr(k));

               end;

               output;

           %do i=1 %to &r.;

             end;

             %end;

            run;

%mend combo;

 

 

 

%macro mrun(sri);

      %let i=1;

      %let thing=;

 

      %do %while (%scan(&var.,&i.) ne );

        %let p&i.="%scan(&var.,&i.)";

       %if &i.=1 %then %let thing=&&p&i..;

        %else %let thing=&thing.,&&p&i..;

        %let i=%eval(&i.+1);

      %end;

              %let things=&thing.;

      %let n=%eval(&i.-1);

 

* Calculation the max length of independent variables;

             %do m=1 %to &n.;

                 %let _length&m. = %length(%scan(&var.,&m.));

             %end;

 

             %do m=1 %to &n.;

                %if &m.=1 %then %let copy=&&_length&m.;

%else

                %let copy=&copy., &&_length&m.;

             %end;

 

             %let maxl = %sysfunc(max(&copy.));

*end of calculation;

 

*executing the macro combo(r);

                   %combo(&sri.);

*end of execution;

 

 

%mend mrun;

 

%mrun(&r_value.);

 

 

%put &r_value.;

 

 

%macro vars_creation;

 

data _null_;

set combo;

call symput("tot_com",compress(_N_));

run;

 

%put &tot_com.;

 

data _null_;

set combo;

%do i=1 %to &r_value.;

call symput(compress("v"||&i.||"_"||_n_),v&i.);

%end;

run;

 

data test_data_final;

keep p_1-P_&tot_com.;

set &data_set_name.;

    %do i=1 %to &tot_com.;

 

/*if (&&v1_&i. and &&v2_&i. and &&v3_&i. ) then P_&i.=1;else P_&i.=0;*/

 

if (

            %do j=1 %to %eval(&r_value.-1);

                &&&&v&j._&i. and

                %end;  &&&&v&r_value._&i.

        )then P_&i.=1;else P_&i.=0;

 

    %end;

;run;

 

proc means data=test_data_final noprint;

output out=result(drop=_Freq_ _type_) sum=;

run;

 

proc transpose data=result out=trans_result;run;

 

data final_result;

merge trans_result combo;

run;

 

%mend;

 

%vars_creation;

 

No comments:

Post a Comment