1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
'Begin Description
'This script standardizes variables within cases. 
'A dialog box is presented that allows the user to select (at least three)  
'variables which are standard within cases. For example, if there are 4 variables 
'which correspond to responses on a survey, this script will standardize a case's 
'score for each variable based on the mean and standard deviation for that case across 
'the four variables (e.g., score1 = (score1 - mean1234)/st.dev1234))
'End Description
Option Explicit

'Script level constant declarations
Const cDLGTITLE As String = "Standardize Variables Within Case"
Const cOK As String = "OK"
Const cCANCEL As String = "Cancel"
Const cROOTNAME As String = "Root name for new variables:"
Const cSELVARS As String = "Selected Variables"
Const cVARROOT As String = "VAR"
Const cROOTMSG As String = "Please provide a root name for new variables."
Const cSCRIPTNAME As String = "Standardize Variables Within Case"
Const cSELMSG As String = "Please select at least three variables."


Public strNotSelVar() As String
Public strSelVar() As String
Public strListOfVars() As String
Public bolSelected() As Boolean
Public intArrayIndex() As Integer
Public strFilePath As String

Sub Main

	BuildDialog

End Sub

Sub RunJob
'Purpose: Runs the Syntax that will standardize scores within cases
'Assumptions: None
'Effects: Creates new standardized variables
'Inputs: None
'Return Values: None

Dim strCmd1 As String
Dim strCmd2 As String
Dim strCmd3 As String
Dim strCmd4 As String
Dim intSelVarIndex As Integer

	strCmd1 = "COMPUTE #MEAN_ = MEAN("
	strCmd2  = "COMPUTE #SD_ = SD("
	For intSelVarIndex = 0 To UBound(strSelVar) 
    	If intSelVarIndex < UBound(strSelVar)  Then
        	strCmd1 = strCmd1 & strSelVar(intSelVarIndex) & ","
        	strCmd2  = strCmd2  & strSelVar(intSelVarIndex) & ","
    	ElseIf intSelVarIndex = UBound(strSelVar)  Then
        	strCmd1 = strCmd1 & strSelVar(intSelVarIndex) & ")."
        	strCmd2  = strCmd2  & strSelVar(intSelVarIndex) & ")."
    	End If
	Next
	
	objSpssApp.ExecuteCommands strCmd1, False
	objSpssApp.ExecuteCommands strCmd2, False
	For intSelVarIndex = 0 To UBound(strSelVar) 
 		strCmd3 = "COMPUTE " & DlgText("txtRootName") & intSelVarIndex+1 & " = (" & strSelVar(intSelVarIndex) & "- #MEAN_) / #SD_."
 		objSpssApp.ExecuteCommands strCmd3, False
	Next

	strCmd4 = "EXECUTE."
	objSpssApp.ExecuteCommands strCmd4, False
    
End Sub

Sub BuildDialog
'Purpose: Creates and presents the dialog that allows user to select variables to standardize
'Assumptions: None
'Effects: None
'Inputs: None
'Return Values: None

	ReDim strNotSelVar(0) As String
	ReDim strSelVar(0) As String

	Begin Dialog UserDialog 540,203,cDLGTITLE,.DialogMonitor
		ListBox 30,28,140,119,strNotSelVar(),.lstVarInFile
		ListBox 240,28,150,119,strSelVar(),.lstSelVar
		PushButton 430,14,90,21, cOK,.cmdRun
		PushButton 430,42,90,21, cCANCEL,.cmdCancel
		PushButton 190,77,30,21,">",.cmdMoveIt
		TextBox 290,161,100,21,.txtRootName
		Text 100,165,190,14, cROOTNAME,.Field7
		Text 240,14,140,14, cSELVARS,.lbl1
	End Dialog

	Dim dlg As UserDialog

	Dialog dlg
	
End Sub

Function DialogMonitor(strDialogItem As String, intAction As Integer, intSuppValue As Integer) As Boolean
'Purpose: Monitors the dialog for events taking place
'Assumptions: None
'Effects: None, monitors dialog events and calls procedures to handle them
'Inputs: the control that was selected(strDialogItem), the action that took place(intAction),
'		 and a supplemental value used by some control's events(intSuppValue)
'Return Values: TRUE if keep dialog visible, FALSE if dialog should be closed

    Select Case intAction
	    Case 1 ' Dialog box initialization
	    	DlgEnable "cmdCancel", True
	    	DlgEnable "cmdRun", True
	    	DlgText "txtRootName",  cVARROOT
	    	GetVarsFromFile		'Sub that gets the variables from file and puts in list box
	    Case 2 ' Value changing or button pressed
	        Select Case strDialogItem
	        	Case "cmdRun"
	        		If DlgText("txtRootName") = "" Then
						MsgBox cROOTMSG, 48, cSCRIPTNAME
						DialogMonitor = True
					ElseIf UBound(strSelVar) < 2 Then
						MsgBox cSELMSG, 48, cSCRIPTNAME
						DialogMonitor = True
					Else
	        			Call RunJob
	        			DialogMonitor = False
	        		End If
	        	Case "cmdCancel"
	        		DialogMonitor = False
	        	Case "lstVarInFile"
	        		DlgText "cmdMoveIt", ">"
	        		DlgEnable "cmdMoveIt", True
	        		DialogMonitor = True
	        	Case "lstSelVar"
	        		DlgText "cmdMoveIt", "<"
	        		DialogMonitor = True
	        	Case "cmdMoveIt"
	        		If DlgText("cmdMoveIt") = ">" Then 	'add variable to selected variables list
	        			Call AddToSelList
	        		Else								'remove variable from selected variables list
	        			Call RemoveFromSelList
	        		End If
	        		DialogMonitor = True
	        End Select
	End Select
End Function

Sub AddToSelList()
'Purpose: Changes the status of a variable from unselected to selected
'Assumptions: None
'Effects: Changes the value of bolSelected from FALSE to TRUE
'Inputs: None
'Return Values: None

	Dim intSelIndex As Integer
	Dim i As Integer
	
	intSelIndex = DlgValue("lstVarInFile")
	'loop through intArrayIndex and if find the variable that was selected
	'change its value in bolSelected to TRUE
	For i = 0 To UBound(intArrayIndex)
		If (intArrayIndex(i) = intSelIndex) And (bolSelected(i) = False) Then
			bolSelected(i) = True
			Exit For
		End If
	Next i
	Call PopulateLists	'Populates the list boxes of available and selected variables

End Sub

Sub RemoveFromSelList()
'Purpose: Changes the status of a variable from selected to unselected
'Assumptions: None
'Effects: Changes the value of bolSelected from TRUE to FALSE
'Inputs: None
'Return Values: None

	Dim intSelIndex As Integer
	Dim i As Integer
	
	intSelIndex = DlgValue("lstSelVar")
	'loop through intArrayIndex and if find the variable that was removed from selected list
	'and change its value in bolSelected to FALSE
	For i = 0 To UBound(intArrayIndex)
		If (intArrayIndex(i) = intSelIndex) And (bolSelected(i) = True) Then
			bolSelected(i) = False
			Exit For
		End If
	Next i
	Call PopulateLists

End Sub

Sub PopulateLists()
'Purpose: Goes through all variables and puts selected ones in strSelVar array
'		  unselected variables go into strNotSelVar array.
'Assumptions: None
'Effects: Variables in strSelVar appear in selected variable listbox, those in strNotSelVar appear
'		  in available variables listbox
'Inputs: None
'Return Values: None

	Dim i As Integer
	Dim intNumNotSel As Integer
	Dim intNumSel As Integer
	
	intNumSel = 0
	intNumNotSel = 0
	ReDim strNotSelVar(intNumNotSel) As String
	ReDim strSelVar(intNumSel) As String
	
	'loop through variables and put in appropriate array based on whether
	'they are selected or not
	For i = 0 To UBound(bolSelected)
		If bolSelected(i) = False Then
			ReDim Preserve strNotSelVar(intNumNotSel) As String
			strNotSelVar(intNumNotSel) = strListOfVars(i)
			intArrayIndex(i) = intNumNotSel
			intNumNotSel = intNumNotSel + 1
		Else	'Variable is selected for analysis
			ReDim Preserve strSelVar(intNumSel) As String
			strSelVar(intNumSel) = strListOfVars(i)
			intArrayIndex(i) = intNumSel
			intNumSel = intNumSel + 1
		End If
	Next i
	
	'assign the arrays to their respective listboxes.
	DlgListBoxArray "lstVarInFile", strNotSelVar()
	DlgListBoxArray "lstSelVar", strSelVar()

End Sub

Sub GetVarsFromFile()
'Purpose: Gets the variables from the currently open SPSS data file
'Assumptions: There is a data file open
'Effects: puts the variables into the available variables list box
'Inputs: None
'Return Values: None

	Dim objSPSSInfo As ISpssInfo
	Dim i As Long
	
	Set objSPSSInfo = objSpssApp.SpssInfo

	ReDim strListOfVars(objSPSSInfo.NumVariables - 1) As String
	ReDim bolSelected(objSPSSInfo.NumVariables - 1) As Boolean
	ReDim intArrayIndex(objSPSSInfo.NumVariables - 1) As Integer

	
	For i = 0 To UBound(bolSelected)
		strListOfVars(i) = objSPSSInfo.VariableAt(i)	
		bolSelected(i) = False		'Value = false, variable is not currently selected
		intArrayIndex(i) = i		'stores where will go in either list box
	Next i
	
	DlgEnable "lstVarInFile", True
	DlgEnable "lstSelVar", True
	Call PopulateLists

End Sub