Title: IML Workshop 2.0
1IML Workshop 2.0
May 21, 2003 Charlie Hallahan
2IML Workshop Programming Environment
- Program editor, output window, error log window
- Program editor color-codes IML syntax
- Editable output window
- Multiple independent program environments
- Multithreaded
- these notes are based on a tutorial at SUGI 28
presented by - Simon Smith and Rick Wicklin of SAS Institute
Demo
3The IML Workshop Environment
Plot window
Program editor
Data table window
Output window
4Programming with IMLPlus
- Use same syntax as for PROC IML
- Execute data/procedure steps from PROC IML
- Create high-level statistical graphics
- Call modules without explicitly loading them
- Call external functions written in C/Fortran/Java
5Graphics Programming with IMLPlus
- Create and manipulate graphics using objects
- Use declare statement to define variables that
refer to objects - declare
ScatterPlot plot - Call Create method to construct new object
- plot
ScatterPlot.Create(Example, x, y) - Use dot syntax to invoke methods
- plot.ShowAxisReferenceLine
s(YAXIS)
Demo
6Creating a Scatter Plot
7Creating Graphs Using IMLPlus, Part 1
/ BasicGraphs2.iml / x 1200 e 30
normal( x ) y x e declare ScatterPlot
splot splot ScatterPlot.Create( "Example", x,
y ) declare Histogram hist hist
Histogram.Create( "Error Distribution", e ) ?
These graphs are not dynamically linked.
Demo
8Unlinked Scatter Plot and Histogram
9Dynamic Linking Infrastructure
- DataObject and DataView classes are the backbone
- DataObject contains the data being displayed
- DataView renders the data in a specific way
ScatterPlot Histogram
10The DataObject Class
- Methods for adding data from matrices
- Methods for loading SAS data sets
- Methods for accessing data/metadata
- Every graph is based on a DataObject
11The DataView Class Hierarchy
DataView DataTable
Plot Plot2D
BarChart
BoxPlot
ContourPlot
Histogram
LinePlot
MosaicPlot
PolygonPlot
ScatterPlot Plot3D
RotatingPlot
12Creating Graphs Using IMLPlus, Part 2
/ BasicGraphs3.iml / x 1200 e 30
normal( x ) y x e declare DataObject
dobj dobj DataObject.Create( "Data", "X",
"Y", "E", xye ) declare ScatterPlot
splot splot ScatterPlot.Create( dobj, "X", "Y"
) declare Histogram hist hist
Histogram.Create( dobj, "E" )
? These graphs are dynamically linked.
Demo
13Linked Scatter Plot and Histogram
14Color-Coding Observations by Interval Data
colormatrix 255 0 0, 255 255
0, 255 0 0 run
ColorCode(dobj,"E",colormatrix,9) splot.SetMarker
Size(7)
15Color-Coding Observations by Interval Data
16IMLPlus Module Library
- Collection of prewritten modules
- Similar to IMLMLIB modules
- DrawLegend module adds legends to a graph
- DoDialog and DoMessageBox modules enable
- interaction with the user
17Graphing Data Stored in SAS Data Sets
declare DataObject dobj dobj
DataObject.CreateFromFile("Business.sas7bdat") de
clare ScatterPlot plot plot ScatterPlot.Create(
dobj,"Sales","Profits") plot.SetObsLabelVar("Comp
any")
Demo
18Graphing Data Stored in SAS Data Sets
19Creating Graphs Using the GUI
- Use File menu to open SAS data set (data table
appears) - Use Plot menu to create graphs
- Can use graphs GUI to customize appearance
- Can copy and paste graphs into other Windows
apps
20Creating Graphs Using the GUI
21Modifying Graph Properties Using GUI
- Graph window divided into Plot Area and Graph
Area - Right-click area to change properties
- Right-click axis to modify axis properties
- Right-click axis title to change text/font
22Modifying Graph Properties Using GUI
23Local Selection
- Technique for using graphs to filter data
- Terminology Selector view, Observer view
- Each graph is either Selector view or Observer
view - Selector views enable user to select
observations - Observer views display either union or
intersection - of selections
24Local Selection Basics
Selector view ?
? Observer view (intersection)
Selector view ?
25Executing Procedures from IMLPlus
- Execute any SAS statements from IMLPlus
- Use submit and endsubmit statements to
delineate - SAS code block
- Use data sets to pass data
- Write modules that use procedures
26IMLPlus Module to Compute Skewness
/ CalcSkewness.iml / start CalcSkewness( x
) create InputDataSet var x append
close InputDataSet submit proc
univariate dataInputDataSet noprint var
x output outOutputDataSet
skewnessskewness quit endsubmit
use OutputDataSet read var skewness
close OutputDataSet return
(skewness) finish v 2, 4, 6, 3, 1 y
CalcSkewness( v ) print y
Output Y 0.5901287
Demo
27Putting the Pieces Together
- Filtering Data Using Graphics
- Interacting with Programs
- Integrating Statistical Results and Graphics
28Interacting with Programs
? Choose aspects of analysis at run-time ?
Data sets, parameters, PROC options, ?
Display dialog boxes to prompt for input ?
Present menu of choices for analysis
29Interacting with Programs
/ SummaryStatsCalculator.iml This program
allows you to choose a data set to select a
numeric variable of interest, and to see
summary statistics for that variable. The
statistics are MIN, MAX, N, NMISS MEDIAN, MEAN,
STD, and SUM / run GetPersonalFilesDirectory(
path ) dir path "Data Sets" ok
DoDialogGetOpenDataSetFileName( dataset, "Choose
a Data Set", dir ) if okfalse then do reset
log print "Abort. No data set chosen."
abort end declare DataObject dobj dobj
DataObject.CreateFromFile( dataset )
30Interacting with Programs
/ get list of numerical variables / numVars
dobj.GetNumVar() varNames j(numVars,1,"
")/ length 32 / do i
1 to numVars name dobj.GetVarName(i) if
dobj.IsNumeric(name) then varNamesi
name end numericVarIndex loc( varNames " "
) numNumericVars ncol( numericVarIndex ) if
numNumericVars0 then do reset log print
"Abort. The data set does not contain",
"any numeric variables. Abort" abort end
varNames varNamesnumericVarIndex
31Interacting with Programs
/ display list in a dialog / ok
DoDialogGetListItem( selection, "Numeric
Variables", "Choose a variable to display
summary statistics", varNames ) varName
varNamesselection dobj.GetVarData( varName, x
) statNames 'MIN', 'MAX', 'N',
'NMISS', 'MEDIAN', 'MEAN', 'STD',
'SUM' stats j(8,1,.) stats1 min( x
) stats2 max( x ) stats3 nrow( x
) stats4 ncol( loc(x.) ) stats5
median( x ) stats6 x
32Interacting with Programs
nonMissing stats3-stats4 / N - NMISS
/ if nonMissing gt 1 then do xCenter x -
stats6 stats7 sqrt( xCenter /
(nonMissing-1) ) end stats8 sum( x
) print statsrownamestatNames
colnamevarName if dobj.IsNominal(varName)
then BarChart.Create( dobj, varName ) else
Histogram.Create( dobj, varName )
Demo
33Interacting with Programs
34Interacting with Programs
35Interacting with Programs
36Integrating Statistical Results and Graphics
- Call procedures in other SAS products without
losing - state of IML program.
- Read results into IML Workshop
- Use results in program or to augment graph
- Example Add nonparametric smoother with
confidence - limits to ScatterPlot
37Add Kernel Density Estimation to Histogram
/ KDE.iml create a histogram / declare
DataObject dobj dobj DataObject.CreateFromFile(
"Colleges.sas7bdat" ) declare Histogram
hist hist Histogram.Create( dobj, "RoomBd"
) hist.ShowDensity() / default is frequency
/ / set up libref to data set directory / run
GetPersonalFilesDirectory( dir ) dir dir
"Data Sets" dir "d\charlie\iml
Workshop" libname dataDir (dir)
38Add Kernel Density Estimation to Histogram
/ compute kernel density estimation / / This
KDE syntax is for SAS 8.2 / submit ods
exclude all / turn off printing / proc kde
datadataDir.Colleges outKDEOut var RoomBd
quit endsubmit / read the output from the
KDE procedure / use KDEOut read all varRoomBd
density close KDEOut / overlay on existing
Histogram of RoomBd / hist.DrawUseDataCoordinates
() hist.DrawLine( RoomBd, density )
Demo
39Add Kernel Density Estimation to Histogram
40Add Nonparametric Smoother to ScatterPlot
/ TPSpline.iml / / set up libref to data
set directory / run GetPersonalFilesDirectory(
dir ) dir dir "Data Sets" dir
"d\Charlie\IML Workshop" libname dataDir
(dir) / pred thin plate spline and ask for
95 confidence limits for the predicted mean.
/ submit ods exclude DataSummary FitSummary
FitStatistics proc tpspline
datadataDir.Colleges(where(PubPriv1))
model PctPhD(SAT) output outpred predicted
lclm uclm quit endsubmit / read work.pred
/ declare DataObject dobj dobj
DataObject.CreateFromServerDataSet( "work.pred" )
41Add Nonparametric Smoother to ScatterPlot
/ manufacture names output by proc / xVar
"SAT" yVar "PctPhD" predName "p_"
yVar lclName "lclm_" yVar uclName "uclm_"
yVar / create scatter plot of Y versus X
/ declare ScatterPlot plot plot
ScatterPlot.Create( dobj, xVar, yVar
) plot.SetAxisViewRange(YAXIS, 0, 100) / get
predicted values and confidence limits
/ dobj.Sort( xVar ) dobj.GetVarData( xVar, x
) dobj.GetVarData( predName, pred
) dobj.GetVarData( lclName, lower95
) dobj.GetVarData( uclName, upper95 )
42Add Nonparametric Smoother to ScatterPlot
/ plot the predicted pred in red
/ plot.DrawUseDataCoordinates() plot.DrawSetPenC
olor( RED ) plot.DrawLine( x, pred ) / plot
the confidence limits in blue / plot.DrawSetPenCo
lor( BLUE ) plot.DrawLine( x, lower95
) plot.DrawLine( x, upper95 ) / Add legend at
Inside Right Bottom position / labels
"Predicted" "95 Lower CLM" "95 Upper CLM"
colors RED BLUE BLUE run
DrawLegend( plot, labels, 14, colors,
SOLID, NULL, -1, "IRB" )
Demo
43Add Nonparametric Smoother to ScatterPlot