Search code examples
vbaspssbasicwinwrap

WWBasic + SPSS, script to rename value labels



before I start I want to point out that I tagged this question as VBA because I can't actually make a new tag for Winwrap and I've been told that Winwrap is pretty much the same as VBA.

I'm working on SPSS V19.0 and I'm trying to make a code that will help me identify and assign value labels to all values that don't have a label in the specified variable (or all variables).
The pseudo code below is for the version where it's a single variable (perhaps inputted by a text box or maybe sent via a custom dialogue in the SPSS Stats program (call the .sbs file from the syntax giving it the variable name).

Here is the Pseudo Code:

Sub Main(variable As String)

On Error GoTo bye

'Variable Declaration:
Dim i As Integer, intCount As Integer
Dim strValName As String, strVar As String, strCom As String
Dim varLabels As Variant 'This should be an array of all the value labels in the selected record
Dim objSpssApp As 'No idea what to put here, but I want to select the spss main window.

'Original Idea was to use two loops
'The first loop would fill an array with the value lables and use the index as the value and 
'The second loop would check to see which values already had labels and then
'Would ask the user for a value label to apply to each value that didn't.
'loop 1
'For i = 0 To -1 
'current = GetObject(variable.valuelist(i)) 'would use this to get the value
'Set varLabels(i) = current
'Next

'Loop for each number in the Value list.
strValName = InputBox("Please specify the variable.")
'Loop for each number in the Value list.
 For i = 0 To varLabels-1
 If IsEmpty (varLabels(i)) Then
 'Find value and ask for the current value label
 strVar = InputBox("Please insert Label for value "; varLabels(i);" :","Insert Value Label")
 'Apply the response to the required number
 strCom = "ADD VALUE LABELS " & strVar & Chr$(39) & intCount & Chr$(39) & Chr$(39) & strValName & Chr$(39) &" ."
 'Then the piece of code to execute the Syntax
 objSpssApp.ExecuteCommands(strCom, False)

 End If
 'intCount = intCount + 1 'increase the count so that it shows the correct number
 'it's out of the loop so that even filled value labels are counted
 'Perhaps this method would be better?
 Next

Bye:
End Sub

This is in no way functioning code, it's just basically pseudo code for the process that I want to achieve I'm just looking for some help on it, if you could that would be magic.

Many thanks in advance
Mav


Solution

  • Winwrap and VBA are almost identical with differences that you can find in this post: http://www.winwrap.com/web/basic/reference/?p=doc_tn0143_technote.htm I haven't used winwrap, but I'll try to answer with my knowledge from VBA.

    • Dim varLabels As Variant

    You can make an array out of this by saying for example dim varLabels() as variant 'Dynamically declared array dim varLabels(10) as variant 'Statically declared array dim varLabels(1 to 10) as variant 'Array starting from 1 - which I mostly use dim varLabels(1 to 10, 1 to 3) 'Multidimensional array

    • Dim objSpssApp As ?

    "In theory", you can leave this as a variant type or even do

    Dim objSpssApp 
    

    Without further declaration, which is basically the same - and it will work because a variant can be anything and will not generate an error. It is good custom though to declare you objects according to an explicit datatype in because the variant type is expensive in terms of memory. You should actually find out about the objects class name, but I cannot give you this. I guess that you should do something like:

    set objSpssApp = new <Spss Window> 
    set objSpssApp = nothing 'In the end to release the object 
    
    • Code:

      'loop 1
      For i = 0 To -1
      current = GetObject(variable.valuelist(i)) 'would use this to get the value
      Set varLabels(i) = current
      Next

    I don't exactly know why you want to count from 0 to -1 but perhaps it is irrelevant. To fill an array, you can just do: varLabels(i) = i The SET statement is used to set objects and you don't need to create an object to create an array. Also note that you did not declare half of the variables used here.

    • Code:

      strVar = InputBox("Please insert Label for value "; varLabels(i);" :","Insert Value Label")

    Note that the concatenation operator syntax is &. This appears to be the same in WinWrap: http://www.winwrap.com/web/basic/language/?p=doc_operators_oper.htm But you know this, since you use it in your code.

    • Code:

      'intCount = intCount + 1 'increase the count so that it shows the correct number
      'it's out of the loop so that even filled value labels are counted
      'Perhaps this method would be better?

    I'm not sure if I understand this question, but in theory all loops are valid in any situation, it depends on your preference. For ... Next, Do ... Loop, While ... Wend, in the end they all do basically the same thing. intCount = intCount + 1 seems valid when using it in a loop.

    • Using Next (for ... next)

    When using a counter, always use Next iCounter because it increments the counter.

    I hope this reply may be of some use to you!