ODS PDF NOTOC; title 'Contents'; proc contents data=temp.fghm113 varnum; run; title; /* test the basic code */ title '/* test the basic code */'; proc freq data=temp.fghm113; tables sex*diabetes / chisq; run; title; /* very simple macro - substitute things that might change */ %macro twobytwo(mydat, pred, outcome); proc freq data=&mydat; tables &pred*&outcome / chisq; run; %mend; /*running the first macro */ title '/*running the first macro */'; %twobytwo(temp.fghm113,sex,diabetes) %twobytwo(temp.fghm113,bpmeds,diabetes) title; /* loop through sex cursmoke bpmeds prevchd prevap prevmi prevstrk prevhyp */ %macro twobytwoloop(mydat, outcome, n, predlist); %do i = 1 %to &n; proc freq data=&mydat; tables %scan(&predlist,&i)*&outcome / chisq; run; %end; %mend; /*running the second macro */ title '/*running the second macro */'; %twobytwoloop(temp.fghm113,diabetes, 8, sex cursmoke bpmeds prevchd prevap prevmi prevstrk prevhyp) title; /* ODS OUTPUT */ title '/*ODS OUTPUT - Using ODS TRACE and LOG file to find name */'; ods trace on; proc freq data=temp.fghm113; tables sex*diabetes / chisq; run; ods trace off; title; /* Use ODS OUTPUT to store two-way table and chi-square results to datasets called CT and CHI */ title '/* Use ODS OUTPUT to store two-way table and chi-square results to datasets called CT and CHI */'; proc freq data=temp.fghm113; tables sex*diabetes / chisq; ods output crosstabfreqs=ct chisq=chi; run; title; /* Select only paricular output using ODS SELECT */ title '/* Select only paricular output using ODS SELECT */'; proc freq data=temp.fghm113; tables sex*diabetes / chisq; ods select crosstabfreqs; run; title; /* Test how to clean the resulting datasets */ title '/* Test how to clean the resulting datasets */'; proc print data=ct; where diabetes=1 and SEX ne .; run; proc print data=chi; where statistic="Continuity Adj. Chi-Square"; run; title; /* final basic code including cleaning CT and CHI datasets */ title '/* final basic code including cleaning CT and CHI datasets */'; proc freq data=temp.fghm113; tables sex*diabetes / chisq; ods output crosstabfreqs=ct chisq=chi; run; data ct; set ct; if diabetes = 1 and SEX ne .; keep table rowpercent; run; data chi; set chi; if statistic="Continuity Adj. Chi-Square"; Keep table prob; /* basic transpose - swaps rows and columns */ title '/* basic transpose - swaps rows and columns */'; proc transpose data=ct out=cttrans; run; proc print data=cttrans; run; proc print data=chi; run; /* Merge two datasets together */ data out; retain table col1 col2 prob; merge cttrans chi; keep table col1 col2 prob; run; /* Final Simplified Result */ title '/* Final Simplified Result */'; proc print data=out; run; title; /* macro with pretty output from class*/ %macro twobytwoloopb(mydat, outcome, n, predlist); %do i = 1 %to &n; proc freq data=&mydat; tables %scan(&predlist,&i)*&outcome / chisq; ods output crosstabfreqs=ct chisq=chi; run; data ct; set ct; if &outcome = 1; keep table rowpercent; run; data chi; set chi; if statistic="Continuity Adj. Chi-Square"; Keep table prob; proc transpose data=ct out=cttrans; run; data out&i; retain table col1 col2 prob; merge cttrans chi; keep table col1 col2 prob; run; proc print data=out&i; run; %end; %mend; title '/* running macro with pretty output from class*/'; %twobytwoloopb(temp.fghm113,diabetes, 8, sex cursmoke bpmeds prevchd prevap prevmi prevstrk prevhyp) title; /* macro with pretty output updated - one output table!! */ %macro twobytwoloopc(mydat, outcome, n, predlist); %do i = 1 %to &n; proc freq data=&mydat; tables %scan(&predlist,&i)*&outcome / chisq; ods select crosstabfreqs chisq; ods output crosstabfreqs=ct chisq=chi; run; data ct; set ct; if diabetes = 1; keep table rowpercent; run; data chi; set chi; if statistic="Continuity Adj. Chi-Square"; Keep table prob; proc transpose data=ct out=cttrans; run; data out&i; retain table col1 col2 prob; merge cttrans chi; keep table col1 col2 prob; run; %end; data out; set %do j = 1 %to &n; out&j %end; ; run; data out2; set out; table = substr(table,7); rename col1=Row1PctDiabetes; rename col2=Row2PctDiabetes; rename prob=PValue; rename table=Variables; run; proc print data=out2; run; %mend; title '/* macro with pretty output updated - one output table!! */'; %twobytwoloopc(temp.fghm113,diabetes, 8, sex cursmoke bpmeds prevchd prevap prevmi prevstrk prevhyp) title; ods pdf close; ods pdf notoc; title 'Final Result of Macro Creation'; proc print data=out2; run; ods pdf close;