Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

NESUG 2008 Pharmaceuticals, Health Care, and Life Sciences

Develop A Demographic Data Analysis Report System


By SAS® and Visual Basic
Richard Zhou, Johnson & Johnson Pharmaceutical Research and Development, L.L.C

ABSTRACT
This paper describes how to use SAS plus Visual Basic software to develop an analysis report system, using clinical
demographic data as the analysis data. The system consists of general statistical analysis processes, output reports
which include three parts: (1) Statistical Summary (a text RTF file), (2) Analysis Graphs (a HTM file with drill-down
information) and (3) Data Listings (an Excel file with the option of multiple worksheets). All process functions are
integrated and located in one working window. The system tool is simple, efficient and powerful. Moreover, because
this tool can be built into an executable file (.exe), it is user-friendly even to those who do not have SAS experience.

INTRODUCTION
As we know, SAS is a powerful tool that can perform many statistical analysis processes and generate reports
(including text or graphs). Likewise, the Visual Basic (VB) software is also a useful tool in that it builds a user-friendly
window environment, in which a user can organize, manage and operate a variety of tasks easily. To take advantage
of the benefits of each software, we can combine SAS and Visual Basic to develop a tool for a data analysis report
system. It will be very practical in processing regular analysis for data in a standard format.

This paper describes an attempt at using this system for the above purposes. We will use VB to design a working
window with the following functions:
• Access and view the specified input data.
• Submit RunSAS1.sas to do the expected statistical analyses and generate an output text report file for
selected variables; view the report file.
• Submit RunSAS2.sas to do graphic analysis and generate a graph file for selected variables; view the graph
file.
• Submit RunSAS3.sas to generate an Excel file to list all subject data by a selected sorting variable, view
those work sheets.

Clinical demographic data structures and analysis procedures are mostly very similar. To demonstrate, we will use
some simple examples to introduce the working method of this tool.

DESIGN OF THE WORKING WINDOW


The desired working window is named “ShowDemog”. It contains eight main sections. The function for each section is
listed in the following table:

Section Label Function


1 Data Folder To select the input data folder (path).
2 Data Name To select the input (demographic) data set
3 View Data To use SAS Viewer to look at the input data
4 Output Folder To specify the folder for output report files
5 Statistic To submit the program RunSAS1 to get statistical analysis
Summary summary report for selected variables
6 Graphic Analysis To submit the program RunSAS2 to get graphic analysis for
selected variables
7 Data Listing To submit the program RunSAS3 to get an Excel file for
subject listings by selected sorting variable
8 Exit To exit the working window

Section 5 contains: (1) list of character variable(s) to be analyzed; (2) list of numerical variable(s) to be analyzed; (3)
submit RunSAS1.sas; (4) view output results.
Section 6 contains: (1) specify the variable to be analyzed; (2) specify the grouping variable; (3) specify graph type; (4)
submit RunSAS2.sas; (5) view the graph file.
Section 7 contains: (1) specify the group variable to be sorted; (2) submit RunSAS3.sas; (3) view generated Excel data
file.

1
NESUG 2008 Pharmaceuticals, Health Care, and Life Sciences

The working window is shown below:

1 2
3

7
6
8

Figure 1: The working window

Detail in each section, such as box names, properties and functions in Visual Basic, VB and SAS code are described
in the following:

SECTION 1: DATA FOLDER


Name of the Box: Dir1
Property: DriveListBox
Function: To select the input data folder (path).

SECTION 2: DATA NAME


Name of the Box: File1
Property: FileListBox
Function: To select the input (demographic) data set.

SECTION 3: VIEW DATA


Name of the Box: SASView
Property: CommandButton
Function: To use SAS Viewer to look at the input data.
VB Code:
Private Sub SASview_Click()
Dim SASview As Object
Dim folder As String
Dim dsn As String
Dim fddsn As String

folder = File1.Path
dsn = File1.FileName
fddsn = folder + "\" + dsn

Set SASview = CreateObject("SAS.Application")


SASview.Submit ("x 'cd C:\Program Files\SAS 9.1.3\SAS System Viewer\9.1 '; ")
SASview.Submit ("x 'sv.exe " & fddsn & " '; ")
End Sub

2
NESUG 2008 Pharmaceuticals, Health Care, and Life Sciences

Example:
(1) Select “C:\Project1\SAS\Data” for input data folder.
(2) Select “demog.sas7bdat” for input data set name.
(3) Click the box “View Data” then the screen will appear as below

Figure 2: View input data

SECTION 4: OUTPUT FOLDER


Name of the Box: TxtOutdir
Property: TextBox
Function: To specify the folder for output report files.

SECTION 5: STATISTICAL SUMMARY


Section 5.1 : Character Variables
Name of the Box: TxtCharVar
Property: TextBox
Function: To input the character variable(s) to be analyzed

Section 5.2 : Numerical Variables


Name of the Box: TxtNumVar
Property: TextBox
Function: To input the numerical variable(s) to be analyzed

Section 5.3 : RunSAS1


Name of the Box: RunSAS1
Property: CommandButton
Function: To run RunSAS1.sas to get a statistical summary
VB Code:
Private Sub RunSAS1_Click()
Dim RunSAS1 As Object
Dim folder As String
Dim dsn As String
Dim outdir As String
Dim charvar As String
Dim numvar As String

outdir = TxtOutdir.Text
charvar = TxtCharVar.Text
numvar = TxtNumVar.Text
dsn = File1.FileName
folder = File1.Path

If Len(dsn) > 0 Then


pos = InStr(dsn, ".")
dsn = Left(dsn, pos - 1)

Set RunSAS1 = CreateObject("SAS.Application")

RunSAS1.Submit (" %let dir=" & folder & " ;")


RunSAS1.Submit (" %let dsn=" & dsn & "; ")
RunSAS1.Submit (" %let outdir=" & outdir & " ; ")
RunSAS1.Submit (" %let charvar=" & charvar & " ; ")
RunSAS1.Submit (" %let numvar=" & numvar & " ; ")

3
NESUG 2008 Pharmaceuticals, Health Care, and Life Sciences

RunSAS1.Submit (" %inc 'C:\Project1\sas\RunSAS1.sas'; ")


RunSAS1.Visible = False
RunSAS1.quit
MsgBox "RunSAS1 Completed"

Else
MsgBox "No Data file selected"
End If

End Sub

SAS Code in RunSAS1.sas:


Upon clicking the box RunSAS1, Visual Basic will call RunSAS1.sas to run.
The function of RunSAS1 is to do general statistic analysis for input character and numerical variables, and to
generate a statistical summary report file: RunSAS1.rtf. Statistical methods in the analysis, and the output contents
and format are dependent on how the user preset them. The SAS code in this demo example is simple:

%macro runsas1;
libname dirname "&dir";
ods rtf file="&outdir.\RunSAS1.rtf";

proc sort data=dirname.&dsn out=testds;


by TREAT;
run;

title1 "Summary of Demographic Data Analysis";


%if "&charvar"^=" " %then %do;
proc freq data=testds;
tables &charvar /list;
run;
%end;
%if "&numvar"^=" " %then %do;
proc means data=testds n mean max min range std maxdec=1;
var &numvar;
by TREAT;
run;
%end;
ods rtf close;
%mend runsas1;
%runsas1;

Section 5.4 : View Summary


Name of the Box: ViewSummary
Property: CommandButton
Function: To view the statistical summary report (RunSAS1.rtf)
VB Code:
Private Sub ViewSummary_Click()
Dim outdir As String
outdir = TxtOutdir.Text

Dim OpenRTF As Object


Set OpenRTF = CreateObject("Word.Application")
OpenRTF.Visible = True
OpenRTF.Documents.open " " & outdir & "\RunSAS1.rtf"
End Sub

Example:
(1) Select “C:\Project1\SAS\Data” for input data folder.
(2) Select “demog.sas7bdat” for input data set name.
(3) Type “C:\Test” for input data folder.
(4) Type “TREAT SEX RACE REGION” for analysis character variables
(5) Type “AGE WEIGHTKG” for analysis numerical variables
(6) Click the box “RunSAS1”. After a while, you will see this window:

Figure 3: The message after


submitting “RunSAS1”

4
NESUG 2008 Pharmaceuticals, Health Care, and Life Sciences

(7) Click “OK”, then click the box “View Summary”. You will see “Summary of Demographic Data Analysis” as
below:

Figure 4: Click button “View Summary” to look the summary reports

SECTION 6: GRAPHIC ANALYSIS


Section 6.1 : Analysis Variable
Name of the Box: TxtGvar
Property: TextBox
Function: To specify the variable to be analyzed

Section 6.2 : Group Variable


Name of the Box: TxtGby
Property: TextBox
Function: To specify the grouping variable

Section 6.3 : Graph Type


Name of the Box: ListGtype
Property: ListtBox
Function: To specify the type of graph

Section 6.4 : RunSAS2


Name of the Box: RunSAS2
Property: CommandButton
Function: To run RunSAS2.sas to get graphic analysis
VB Code:
Private Sub RunSAS2_Click()
Dim RunSAS2 As Object
Dim folder As String
Dim dsn As String
Dim outdir As String
Dim gvar As String
Dim gby As String
Dim gtype As String

5
NESUG 2008 Pharmaceuticals, Health Care, and Life Sciences

outdir = TxtOutdir.Text
gvar = TxtGvar.Text
gby = TxtGby.Text
gtype = ListGtype
dsn = File1.FileName
folder = File1.Path

If Len(dsn) > 0 Then


pos = InStr(dsn, ".")
dsn = Left(dsn, pos - 1)

Set RunSAS2 = CreateObject("SAS.Application")

RunSAS2.Submit (" %let dir=" & folder & " ;")


RnSAS2.Submit (" %let dsn=" & dsn & "; ")
RunSAS2.Submit (" %let outdir=" & outdir & " ; ")
RunSAS2.Submit (" %let gvar=" & gvar & " ; ")
RunSAS2.Submit (" %let gby =" & gby & " ; ")
RunSAS2.Submit (" %let gtype=" & gtype & " ; ")
RunSAS2.Submit ("options noxwait;")
RunSAS2.Submit ("x 'del " & outdir & "\*.gif';")
RunSAS2.Submit (" %inc 'C:\Project1\sas\RunSAS2.sas'; ")
RunSAS2.Visible = False
MsgBox "RunSAS2 Completed"

Else
MsgBox "No Data file selected"
End If

End Sub

SAS Code in RunSAS2.sas:


On clicking the RunSAS2 button, Visual Basic will call RunSAS2.sas to run.
The function of RunSAS2 is to do graphic analysis for input variables and generate a graph file: RunSAS2.htm.
There are many choices of graphic styles and output fie formats in SAS Graph. The following SAS code is an
example in which we designed two options for “Group Variable”: entering a variable or leaving it blank; and two
options for “Graph Type”: BAR or PIE; the output file is in html format:

%macro mplot;

*****************************************************;
** Part 1: Load Input Data, Assign Macro Variables **;
*****************************************************;
libname dirname "&dir";
libname outdir "&outdir";

** 1.1 Load analysis data **;


data testds;
set dirname.&dsn;
run;

proc contents data=testds out=lb(keep=name label) noprint;


run;
data _null_;
set lb;
if name="&gvar" then call symput ('gvarlb',trim(left(label)));
run;

** 1.2 Find cases for selected analysis variable (&gvar.) **;


** 1.3 Find cases for selected group variable (&gby.) **;
proc freq data=testds noprint;
table &gvar./out=out1;
%if &gby^= %then %do;
table &gby./out=out2;
%end;
run;

** 1.4 Assign macro variable values for such cases **;


data _null_;
set out1;

6
NESUG 2008 Pharmaceuticals, Health Care, and Life Sciences

call symput('GVAL'||trim(left(_n_)),&gvar.);
call symput('TOTAL',trim(left(_n_)));
run;

%if &gby^= %then %do;


data _null_;
set out2;
call symput('GBY'||trim(left(_n_)),&gby.);
call symput('TOTAL2',trim(left(_n_)));
run;
%end;

*****************************************************************;
** Part 2: Create A RTF Link File for Drill Down Information **;
** (Listing N, Mean, Min, Max, Range and Std Dev. of **;
** Age, Weight and Study Days) **;
** Options: With or without group variable (&gby) **;
*****************************************************************;

** 2.1 Prepare data **;


data testds2;
set testds;

%if &gby^= %then %do;


GraphVar=&gvar||', '||&gby;
%do i=1 %to &total;
if &gvar ="&&gval&i." then do;
%do j=1 %to &total2;
if &gby ="&&gby&j." then do;
%let k=%eval((&i-1)*&total2+&j);
rpt="href='DrillDowns.rtf#g&k'";
end;
%end;
end;
%end;
label GraphVar="&gvarlb. Group";
%end;

%else %do;
GraphVar=&gvar;
%do i=1 %to &total;
if &gvar ="&&gval&i." then do;
rpt="href='DrillDowns.rtf#g&i'";
end;
%end;
label GraphVar="&gvarlb";
%end;

run;

** 2.2 Create RTF file **;


ods rtf body="&outdir\DrillDowns.rtf" ANCHOR='g1';

title "Subject's Age, Weight and StudyDays in Each Treatment Group";

proc sort data=testds2;


by GraphVar;
run;

proc means data=testds2 n mean max min range std maxdec=0;


var age weightkg studyday ;
by GraphVar;
run;

quit;
ods rtf close;

*****************************************************;
** Part 3: Count frequencies for analysis variable **;
** and create graph data (testds3) **;
*****************************************************;

7
NESUG 2008 Pharmaceuticals, Health Care, and Life Sciences

proc freq data=testds2 noprint;


tables GraphVar*rpt/out=testds3;
run;

*************************************;
** Part 4: Create A HTM Graph File **;
*************************************;

ods html body="&outdir\RunSAS2.htm" gpath="&outdir.";

** 4.1 IF GraphType=PIE **;

%if %upcase(&gtype)=PIE %then %do;

GOPTIONS DEVICE=GIF GSFMODE=REPLACE XPIXELS=480 YPIXELS=360 FTEXT=SWISS HTEXT=1.2;

proc gchart data= testds3;


pie GraphVar / sumvar=count
percent=arrow
slice=arrow
value=none
HTML=rpt
noheading ;

pattern1 v=s c=blue;


pattern2 v=s c=cyan;
pattern3 v=s c=green;
pattern4 v=s c=magenta;
pattern5 v=s c=red;
pattern6 v=s c=yellow;
pattern7 v=s c=olive;
pattern8 v=s c=brown;

title h=6 pct 'Pie Chart: Population of Subjects';


run;

%end;

** 4.2 IF GraphType=BAR **;

%if %upcase(&gtype)=BAR %then %do;

GOPTIONS DEVICE=ACTIVEX GSFMODE=REPLACE XPIXELS=560 YPIXELS=480 FTEXT=SWISS


HTEXT=1.2;

proc gchart data= testds3;


vbar GraphVar / sumvar=count
HTML=rpt;
pattern c=blue;
title 'Bar Chart: Population of Subjects';
run;

%end;

quit;
ods HTML close;
%mend mplot;

%mplot;

Section 6.4 : View Graph


Name of the Box: ViewGraph
Property: CommandButton
Function: To view graphic analysis (RunSAS2.htm)
VB Code:
Private Sub ViewGraph_Click()
Dim cmd As String
Dim outdir As String
outdir = TxtOutdir.Text
cmd = "C:\Program Files\Internet Explorer\IEXPLORE.EXE " & outdir &

8
NESUG 2008 Pharmaceuticals, Health Care, and Life Sciences

"\RunSAS2.htm"
Call Shell(cmd, 1)
End Sub

Example 1:
(1) Select “C:\Project1\SAS\Data” for input data folder.
(2) Select “demog.sas7bdat” for input data set name.
(3) Type “C:\Test” for output file folder.
(4) In the frame “Graph Analysis” (see figure 5),
(a) Type “TREAT” for analysis character variables
(b) Type “SEX” for group variables
(c) Select PIE as Graph Type
(d) Click the box “RunSAS2”. After a while, you will get the message “RunSAS2 Completed”, then click “OK”.
(e) Click the box “View Graph” to see graph file RunSAS2.htm as shown in Figure 6.

Figure 5: Frame “Graph


Analysis”

Test: Move mouse to here


and left-click, you will get
the “Drill Down” Information
for “Treatment B, MALE”
(see Figure 7)

Figure 6: Example 1, Pie Chart

Figure 7: “Drill
Down” Information

9
NESUG 2008 Pharmaceuticals, Health Care, and Life Sciences

Example 2:
(1) Change the selection of Graph Type (see figure 5) from PIE in Example 1 to BAR.
(2) Click the box “RunSAS2”. After a while, you will get the message “RunSAS2 Completed”, then click “OK”.
(3) Click the box “View Graph” to see graph file RunSAS2.htm as shown in Figure 8.
(4) Move the mouse to any bar chart to get actual counts shown in a message box.

Test: Move mouse to here and


left-click, you will get the “Drill
Down” Information for “Treatment
B, MALE” (see Figure 7)

Figure 8: Example 2, Bar Chart

SECTION 7: DATA LISTINGS


Section 7.1 : Select “Sort By” Variable
Name of the Box: LstByvar
Property: ListBox
Function: To select the sort variable

Section 7.2 : Run RunSAS3


Name of the Box: RunSAS3
Property: CommandButton
Function: To run RunSAS3.sas to get data listings
VB Code:
Private Sub RunSAS3_Click()
Dim RunSAS3 As Object
Dim folder As String
Dim dsn As String
Dim outdir As String
Dim byvar As String

outdir = TxtOutdir.Text
byvar = LstByvar
dsn = File1.FileName
folder = File1.Path

If Len(dsn) > 0 Then

10
NESUG 2008 Pharmaceuticals, Health Care, and Life Sciences

pos = InStr(dsn, ".")


dsn = Left(dsn, pos - 1)

Set RunSAS3 = CreateObject("SAS.Application")

RunSAS3.Submit (" %let dir=" & folder & " ;")


RunSAS3.Submit (" %let dsn=" & dsn & "; ")
RunSAS3.Submit (" %let outdir=" & outdir & " ; ")
RunSAS3.Submit (" %let byvar=" & byvar & " ; ")
RunSAS3.Submit ("options noxwait;")
RunSAS3.Submit ("x 'del " & outdir & "\RunSAS3.xls';")
RunSAS3.Submit (" %inc 'C:\Project1\sas\RunSAS3.sas'; ")
RunSAS3.Visible = False
RunSAS3.quit

MsgBox "RunSAS3 Completed"

Else
MsgBox "No Data file selected"
End If

End Sub

SAS Code in RunSAS3.sas:


Upon clicking the RunSAS3 button, Visual Basic will call RunSAS3.sas to run. It will generate the Excel file
RunSAS3.xls with multiple sheets (if the selected sort character variable has at least two cases of values) to list
subject data.
RTF or LST are usually for subjects listings file formats. These two file formats are easily generated by SAS. Here
we use a different way – generating an Excel file with multiple data sheets to show subject listing data. It is helpful in
organizing data, making it easy and quick to look at the listing data information.
The SAS codes are:

libname dirname "&dir";

options noxwait replace;


%let xlsfile=RunSAS3.xls;

data testds;
set dirname.&dsn;
run;

proc sort data=testds out=varlst(keep=&byvar) nodupkey;


by &byvar;
run;

sdata varlst;
set varlst;
ds=compress(propcase(&byvar));
call symput('N',compress(left(_N_)));
call symput('ds'||LEFT(_N_), ds);
call symput('byvar'||LEFT(_N_), &byvar);
run;

%macro setds;
%do i=1 %to &N;
data &&ds&i;
informat SUBJID TREAT &byvar REGION RACE SEX;
set testds;
if &byvar="&&byvar&i";
run;

proc sort data=&&ds&i;


by subjid;
run;
%end;
%mend setds;
%setds;

** Create Excel File **;


libname WrkBk EXCEL "&outdir.\&xlsfile" ver=2002;

11
NESUG 2008 Pharmaceuticals, Health Care, and Life Sciences

%macro mdxls;
%do i=1 %to &n;
data WrkBk.&&ds&i;
set &&ds&i;
run;
%end;
libname WrkBk clear;
quit;
%mend mdxls;
%mdxls;

Section 7.3 : View Listing Sheets


Name of the Box: ViewListing
Property: CommandButton
Function: To view listing sheets (RunSAS3.xls)
VB Code:
Private Sub ViewListing_Click()
Dim outdir As String
outdir = TxtOutdir.Text
Dim OpenExcel As Object
Set OpenExcel = CreateObject("Excel.Application")
OpenExcel.Visible = True
OpenExcel.Workbooks.open outdir & "\RunSAS3.xls"
End Sub

Example :
(1) Select “C:\Project1\SAS\Data” for input data folder.
(2) Select “demog.sas7bdat” for input data set name.
(3) Type “C:\Test” for output file folder.
(4) In the frame “Data Listing” (see figure 9),
(a) Select “RACE” as “Sort By Variable”
(b) Click the box “RunSAS3”. After a while, you will get the message “RunSAS3 Completed”, then click “OK”
(c) Click the box “View Listing Sheets” to see file RunSAS3.xls as shown in Figure10.

Figure 9: Frame “Data


Listing”

Figure 10: Listing of Subjects Sorted By Race

SECTION 8: EXIT
Name of the Box: Exit
Property: CommandButton
Function: To Exit working window.
VB Code:
Private Sub Exit_Click()
Unload Me
End Sub

12
NESUG 2008 Pharmaceuticals, Health Care, and Life Sciences

RESULT OF TEST
The results of the tests were positive. All functions of the boxes in the working window have successfully met our
expectations. The user can execute any changes or settings in the listed field choices without modifying any SAS or
VB code. The tool handles the communication to SAS, MS Word, MS Excel and HTML files, and works very well. Also,
since this tool can be built into an executable (.exe) file, it is very easy and simple to use.

CONCLUSION
This is a successful attempt. The tool has combined advantages from both SAS and Visual Basic. It can load powerful
statistical analysis and reporting software (SAS), and can work with the window environment to perform all task
management and operations visually.
With the strategy of the tool, we can customize (add more or change) any options and functions upon it as needed. We
could also develop similar tools for regular analysis and reports for other standard format data, such as adverse
events, disposure, patient profile, etc..

REFERENCES
[1] Lauren E. Haworth ,“Output Delivery System”, page 250-258.
[2] SUGI 30, paper 044-30, “A Microsoft Access GUI for SAS Automation”, by Rubin Nan and David Mullins.
[3] 2005 Pharmaceutical users Software Exchange Conference, paper AS11, “Using VBA and Base SAS to Get Data
from SAS to Excel Without Data Integrity Issues”, by Timothy Adlington.
[4] SUGI 27, paper 164-27, “Using Visual Basic to Customize a Set of SAS reports”, by Kevin McGowan.
[5] “Quick Tip: Building Drill-down SAS Applications”, by Kirk Paul Lafler, http://www.sconsig.com/sastips/building_drill-
down_sas_applications.pdf.

CONTACT INFORMATION
Your comments and questions are valued and encouraged. Contact the author at:

Richard Zhou
Johnson & Johnson Pharmaceutical Research and Development, L.L.C.
920 Route 202
PO Box 300
Raritan, NJ 08869
Email: rzhou@prdus.jnj.com

SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS
Institute Inc. in the USA and other countries. ® indicates USA registration.

Other brand and product names are trademarks of their respective companies.

13

You might also like