Ah, so you want a by variable. I would suggest something like this: %macro Pairwise_Chisq(data=, group=, outcome=, count=,by=);
%local byclause bySQL prxid i w;
%if %length(&by) %then %do;
%let by=%sysfunc(compbl(&by));
%let bySQL=%sysfunc(tranwrd(&by,%str( ),%str(,a.))),;
%let prxid=%sysfunc(prxparse(s/(\S+)/ and a.$1=b.$1/));
%let ByClause=%sysfunc(prxchange(&prxid,-1,&by));
%syscall Prxfree(prxid);
%put _local_;
%end;
proc sql;
create table groups as select distinct
a.&bySQL a.&group as group1,b.&group as group2
from &data a, &data b
where a.&group<b.&group
order by a.&bySQL a.&group,b.&group;
;
quit;
data ChiTest;
%if %length(&by) %then
set &data(keep=&by);
;
length Pair_compared $100 Chi_stat DF P_value 8;
stop;
run;
filename tempsas temp;
data _null_;
set groups;
file tempsas;
put
"proc freq data=&data noprint;" /
" output out=CTout PCHI;" /
"tables &group*&outcome /chisq cellchi2 nopercent;"
;
length wherecls $1000 byvalues $200;
if vtype(group1)='N' then
wherecls=cats("&group in(",group1,',',group2,')');
else
wherecls=cats("&group in ('",group1,"','",group2,"')");
%do i=1 %to %sysfunc(countw(%str( )&by));
%let w=%scan(&by,&i);
if vtype(&w)='N' then do;
call catt(wherecls," and &w=", &w);
call cats(byvalues,&w,',');
end;
else do;
call catt(wherecls," and &w='", &w,"'");
call cats(byvalues,"'",&w,"',");
end;
%end;
put
'where ' WhereCls ';';
if "&count" ne " " then
put " weight &count;";
put
'run;' /
'proc sql;' /
" insert into ChiTest(&bySQL Chi_stat,df,P_value,Pair_compared)" /
' select ' byvalues '_PCHI_,DF_PCHI,P_PCHI,"' group1 'vs ' group2 '"' /
' from CTout a' /
' ;' /
'quit;' /
;
run;
%include tempsas;
%mend Pairwise_Chisq; It is not really by variable processing, as it is all done in WHERE clauses, so your data does not need to be sorted.
... View more