Macro (SVB) Programs Example - Customizing a Simple Descriptive Statistics Macro
This example summarizes the Statistica Dialog Box for Selecting Data Files, Using Arrays of Values to Set the .Variables Property and Statistica Dialog Boxes for Selecting Variables. We begin by recording two simple macro (SVB) programs for computing descriptive statistics via the Basic Statistics module, and via the Nonparametrics module. We then put them together into a single SVB program, and delete most of the dialog properties, to shorten the program for this illustration:
Sub Main ' We organized the two macro programs by creating ' two separate subroutines. ComputeBasicStatistics ComputeNonparametrics End Sub ' This is the portion copied from the ' Basic Statistics macro program. Sub ComputeBasicStatistics Set newanalysis = Analysis (scBasicStatistics) With newanalysis.Dialog .Statistics = scBasDescriptives End With newanalysis.Run With newanalysis.Dialog .Variables = "5 6 7 8" End With newanalysis.Dialog.Summary.Visible = True End Sub ' This is the portion copied from the ' Nonparametrics macro program. Sub ComputeNonparametrics Set newanalysis = Analysis (scNonparametrics) With newanalysis.Dialog .NonparametricStatistics = _ scNonOrdinalDescriptiveStatistics End With newanalysis.Run With newanalysis.Dialog .Variables = "5 6 7 8" End With newanalysis.Dialog.Summary.Visible = True End Sub
Let us now add the code for selecting a separate data file and for selecting the variable list for the analysis. Here is the complete program:
' The following variables are global, and can be ' "seen" or accessed by all subroutines and functions. ' FileName will contain the name of the user-selected ' input data file (Spreadsheet). Dim FileName As String ' Variable InputFile will contain the input SpreadSheet. Dim InputFile As Spreadsheet ' VarList1 will contain the user-selected variables. Dim VarList1 () As Long Dim InList1 As Long Sub Main ' Function RetrieveVariables retrieves the input data ' file and variable list. If RetrieveVariables <> 0 Then ' We organized the two macro programs by creating ' two separate subroutines. ComputeBasicStatistics ComputeNonparametrics End If Finish: End Sub Function RetrieveVariables As Integer Dim Nvars As Long FileName=GetFilePath(, "sta", , "Open Statistica data file", ) ' If the user cancels out of the GetFilePath function, the ' length of the FileName that is returned will be 0; thus ' we can check here whether the user clicked Cancel; if ' so, we go to label Finish at the end of the program. If Len(FileName)<1 Then GoTo Finish Set InputFile=Spreadsheets.Open(FileName) InputFile.Visible=True InputFile.Activate InList1=0 Nvars = ActiveDataSet.NumberOfVariables If Nvars<1 Then GoTo Finish ' Next the array of Long values VarList1 are ' redimensioned; we will allow up to the total ' number of variables in the input Spreadsheet to ' be selected into the VarList. ReDim VarList1(1 To Nvars) ' Retrieve user-selection of variable list. ret = SelectVariables1 ( _ ActiveDataSet, _ "Variables for Analysis", _ 1, Nvars, _ VarList1, _ InList1, _ "Variables:") If ret=0 Then GoTo Finish RetrieveVariables=1 Exit Function Finish: RetrieveVariables=0 Exit Function End Function ' This is the portion copied from the ' Basic Statistics macro. Sub ComputeBasicStatistics Set newanalysis = Analysis (scBasicStatistics, InputFile) With newanalysis.Dialog .Statistics = scBasDescriptives End With newanalysis.Run With newanalysis.Dialog .Variables = VarList1 End With newanalysis.Dialog.Summary.Visible = True End Sub ' This is the portion copied from the ' Nonparametrics macro. Sub ComputeNonparametrics Set newanalysis = Analysis (scNonparametrics,InputFile) With newanalysis.Dialog .NonparametricStatistics = _ scNonOrdinalDescriptiveStatistics End With newanalysis.Run With newanalysis.Dialog .Variables = VarList1 End With newanalysis.Dialog.Summary.Visible = True End Sub
This program first requests the user to select an input data file, and a list of variables for the analyses from that input data file. Descriptive statistics is then computed via the Basic Statistics module and the Nonparametrics module. Refer also to Dialogs, User Interfaces for information on how to build more elaborate custom dialogs.
Instead of assigning the Long array VarList1 to the .Variables property, you can assign individual integer values; for example, replace the ComputeBasicStatistics subroutine with the following version:
' This is the portion copied from the ' Basic Statistics macro Sub ComputeBasicStatistics Dim i As Integer Set newanalysis = Analysis (scBasicStatistics, InputFile) With newanalysis.Dialog .Statistics = scBasDescriptives End With newanalysis.Run For i=1 To InList1 With newanalysis.Dialog .Variables = VarList1(i) End With newanalysis.Dialog.Summary.Visible = True Next i End Sub
In this version of the subroutine, the program will cycle through the elements in VarList1, and assign them one by one to the .Variables property. Now a single spreadsheet will be created for each variable in VarList1.