Report? Make It Easy - An Example of Creating Dynamic Reports Into Excel
Report? Make It Easy - An Example of Creating Dynamic Reports Into Excel
Report? Make It Easy - An Example of Creating Dynamic Reports Into Excel
Paper 101-28
Liping Huang
Visiting Nurse Service of New York
1
SUGI 28 Coders' Corner
data _null_; CHISQ test and T-Test. It organizes results as a SAS data
call symput('cnt', put('if region = ' ||
"&borough", $20.));
set to be output to a pre-defined Excel template.
run;
%put &cnt &borough &beg &end; /*list variables for running frequencies or
means*/
As one can see from the log, if B is entered in the field of
%let advfreq=adv001_1 adv001_2 adv001_3
the region, the value of &cnt showed as: if borough = adv001_4 adv002_1 adv003_1
“B”. adv004_1 adv004_2 adv005_1
adv006_1 adv006_2 adv006_3
adv007_1;
Section II. Creating data sets %let varmean=los age total_visit;
The data is from the agency Oracle database. Using SQL As mentioned before, simply add and drop variables in
Pass-Through facility and the global macro variables the above statements, one will be able to meet needs from
generated from %WINDOW REPORT, a data set that the management. In addition, the following process shows
matches user-selected criteria is dynamically retrived how the program refers to the variables listed above and
from the Oracle database, named “data_all”. perform analyses by calling macros %DOFREQ,
Furthermore, a flag indicating if a patient was from a %DOMEAN, %DOCHISQ, and %DOTTEST, and
report region is also added. Data sets for a reporting organizes results into a desired order.
region only and non-reporting regions, named “pt_region”
and “pt_other” were created, respectively. /*create data sets in order to convert listed variables to macro
variables*/
proc sql;
connect to oracle (user=&user data advfreq;
password=&password path=rcprod1); input &advfreq;
create table datain as cards;
select * from connection to oracle run;
(select case_num, borough,
discharge_date, adv_item from data varmean;
rc_owner.case_facts input &varmean;
where discharge_date between &beg and cards;
&end); run;
disconnect from oracle;
quit; The following %INPUTVAR converts variables into
data data_all pt_region pt_other; macro variables and calls other macros to perform various
set datain; analyses, such as frequency, mean, CHISQ test and T-
&cnt then report=1; Test, and then it organizes results as a SAS data set to be
else report=0; output. The parameter &datain asks the data set for
if report>=0 then output data_all ;
if report=1 then output pt_region; analysis. It could be either data for the reporting region,
if report=0 then output pt_other; comparison regions, or all. The parameter &varin refers to
run; the group of variables listed in the above %let statements,
and the parameter &in is used to distinguish types of
Section III. Running Analyses Based on Selected analyses. It ranges from 0 to 3 that represents performing
Variables frequency, mean, t-test, and Chisq tests, respectively.
As we stated before, the purpose of this report is to
%macro INPUTVAR(datain, varin, in);
analyze adverse events that occurred during care in order
/*convert a user defined variable list from
to help the agency monitoring the quality of patient care a %let statement to macro variables*/
and making plans for improvement. Therefore, the most proc transpose data=&varin
reporting items involving this report is related to various out=var2;
run;
adverse events. In other words, in addition to overall proc sql noprint;
patient information, the report will present the frequencies select count(*) into :count
of adverse events within a region or across regions. Since from var2;
the strategies of the agency could vary along with the quit;
time, the emphasis of individual adverse categories may data _null_;
vary as well. Therefore, how to minimize the efforts on call symput('newcount',
modifications is the concern of programmers. By using compress('new') ||compress(&count));
the macro %INPUTVAR, as showed bellow, one will be run;
proc sql noprint;
able to perform various analyses by simply adding or select _name_ into :new1 thru
dropping variables listed in %let statements. The :&newcount from var2;
%INPUTVAR is a user-defined macro. It converts quit;
variables into macro variables and calls other macros to
perform various analyses, such as frequency, mean, %let count=&count;
2
SUGI 28 Coders' Corner
data var3(rename=(_name_=label)); get four data sets that represent final descriptive and
set var2;
obs=_n_;
statistics results for a reporting region and its comparison.
run;
proc sort data=var3; by label; run; Section VI. Loading Excel and output results into the
/*calling macros to run frequencies, template and save as a specific report
means, chisq and t-test*/
%if &in=0 %then %do;
%DOFREQ(&count); After results are calculated and organized as a SAS data
%end; set, it is time to output. There are a few different ways to
%if &in=1 %then %do; load Excel template. In the example, we use ‘X”
%DOMEAN(&count);
%end;
command to activate an Excel template and write outputs
%if &in=2 %then %do; into its “data” sheet. In addition, the codes below also
%DOTTEST(&count); write a regional title and a time frame into the data table,
%end; which will be dynamically reflected in a report title.
%if &in=3 %then %do;
%DOCHISQ(&count);
%end; /*Load an Excel template*/
%mend INPUTVAR; options noxwait noxsync;
x "c:\progra~1\micros~1\office\excel
c:\reports\template.xls";
Section III. Calculating Frequencies and Means
data _null_;
In a real world, information is usually presented as x = sleep(10);
run;
frequencies and means. The macros %DOFREQ and
%DOMEAN meet this purpose. Based on the variables /*count the total observations from the
listed from %let statements: &DOFREQ and &DOMEAN final data set in order to determine the
will give results as count, percentages, and means across number of rows needed to be filled in a
data sheet of the template*/
different months, and append results together for
outputting. Please note that the macro %EXIST proc sql;
determines whether a base data set exists while doing select count(*) into :count
appending. It deletes an existing base data set if it is the from finalout;
quit;
first run of a loop. /*output the final data set*/
filename data1 dde "excel|data!r3c1:
The detail codes for the %EXIST, %DOFREQ, AND r%eval(&count+3)c5" notab;
%DOMEAN are listed in Appendix II. data _null_;
set finalout;
file data1;
Section IV. Do CHISQ test and T-TEST if _n_ = 1 then put 'label' '09'x
'c_region' '09'x 'p_region' '09'x
'c_other' '09'x 'p_other' '09'x;
Although it is simple enough to obtain CHISQ results as a put label $25. '09'x
SAS data set through the features of the PROC FREQ, it c_region 8. '09'x
is a little tricky to have the results from a t-test organized p_region 8.3 '09'x
c_other 8. '09'x
as a SAS data set. The Base SAS version 8 has not had p_other 8.3 '09'x
the capability to create a SAS data set directly from the sign $8. '09'x;
procedure of PROC TTEST. Nevertheless, the macro run;
%DOTTEST adopts the features from SAS Output
/*Save the file as a specific report*/
Delivery System (ODS) and examine the variance filename cmds dde 'excel|system';
between two samples and select a proper probability, and data _null_;
subsequently export statistically significant results to the archive="c:\report_®ion"||".xls";;
file cmds;
Excel template (see Appendix II for detail information of put '[QUIT()]';
%DOCHISQ and %DOTTEST. run;
3
SUGI 28 Coders' Corner
References APPENDIX I
Vyverman, K. “Using Dynamic Data Exchange to Export SAS AF WINDOW: Generated from %REPORT
your SAS Data to MS Excel”. Proceedings of the twenty-
seventh Annual SAS Users Group International
Conference, paper 5, 2002.
Trademarks
APPENDIX II
SAS is a registered trademark of SAS Institute Inc. in the %macro EXIST(dsn);
USA and other countries. ® Indicates USA registration. %if %sysfunc(exist(&dsn)) %then %do;
proc datasets library=work;
Other brand name and product names are registered delete &dsn; quit;
%end;
trademarks or trademarks of their respective companies. %mend EXIST;
%macro DOFREQ(count);
%do i=1 %to &count;
Contact Information %let n=&i;
proc freq data=&datain noprint ;
Liping Huang tables month*&&new&n /list out=out;
Center for Home Care Policy and Research run; /*&&new&n was created from
%inputvar*/
Visiting Nurse Service of NY data out;
5 Penn Plaza, 11th Floor set out;
New York, NY 10001 if &&new&n >0;/*Only keep real
value*/
(212) 290 – 0520 proc transpose data=out
E-mail: liping.huang@vnsny.org (keep=count month)
out=outa(drop=_label_ _name_)
prefix=c_m;
id month;
proc transpose data=out(keep=percent
month)
out=outb(drop=_label_ _name_)
prefix=p_m;
id month;
4
SUGI 28 Coders' Corner
5
SUGI 28 Coders' Corner
run;
%if &n=1 %then %do;
%exist(test);
proc append data=out2 base=test force;
run;
%end;
%else %do;
proc append data=out2 base=test force;
run;
%end;
%end;
%mend DOTTEST;