【问题标题】:WWBasic + SPSS, script to rename value labelsWBasic + SPSS,重命名值标签的脚本
【发布时间】:2012-06-28 09:07:56
【问题描述】:


在开始之前,我想指出我将这个问题标记为 VBA,因为我实际上无法为 Winwrap 创建新标签,而且有人告诉我 Winwrap 与 VBA 几乎相同。

我正在使用 SPSS V19.0,我正在尝试编写一个代码来帮助我识别值标签并将其分配给在指定变量(或所有变量)中没有标签的所有值。
下面的伪代码适用于它是单个变量的版本(可能由文本框输入,或者可能通过 SPSS Stats 程序中的自定义对话框发送(从给定变量名称的语法中调用 .sbs 文件)。

这是伪代码:

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

这绝不是功能代码,它基本上只是我想要实现的过程的伪代码我只是在寻找一些帮助,如果可以的话,那将是神奇的。

提前非常感谢
小牛

【问题讨论】:

    标签: vba spss basic winwrap


    【解决方案1】:

    Winwrap 和 VBA 几乎相同,但您可以在这篇文章中找到差异: http://www.winwrap.com/web/basic/reference/?p=doc_tn0143_technote.htm 我没有使用过winwrap,但我会尝试用我的VBA知识来回答。

    • 将 varLabels 调暗为变体

    你可以用这个来做一个数组,例如 dim varLabels() as variant '动态声明的数组 dim varLabels(10) as variant '静态声明的数组 dim varLabels(1 to 10) as variant 'Array 从 1 开始 - 我主要使用 dim varLabels(1 to 10, 1 to 3) '多维数组

    • 将 objSpssApp 调暗为?

    “理论上”,您可以将其保留为变体类型,甚至可以这样做

    Dim objSpssApp 
    

    无需进一步声明,这基本上是相同的 - 它会起作用,因为变体可以是任何东西并且不会产生错误。尽管根据显式数据类型声明对象是一个很好的习惯,因为变体类型在内存方面很昂贵。您实际上应该了解对象类名称,但我不能给您这个。我想你应该这样做:

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

      '循环 1
      对于 i = 0 到 -1
      current = GetObject(variable.valuelist(i)) '将使用它来获取值
      设置 varLabels(i) = 当前
      下一个

    我不完全知道您为什么要从 0 数到 -1,但也许这无关紧要。 要填充数组,您可以这样做: varLabels(i) = i SET 语句用于设置对象,您无需创建对象即可创建数组。另请注意,您没有声明此处使用的一半变量。

    • 代码:

      strVar = InputBox("请为值插入标签"; varLabels(i);" :","插入值标签")

    请注意,连接运算符的语法是 &。 这在 WinWrap 中似乎是相同的: http://www.winwrap.com/web/basic/language/?p=doc_operators_oper.htm 但是您知道这一点,因为您在代码中使用了它。

    • 代码:

      'intCount = intCount + 1 '增加计数,使其显示正确的数字
      '它不在循环中,所以即使填充的值标签也被计算在内
      '也许这种方法会更好?

    我不确定我是否理解这个问题,但理论上所有循环在任何情况下都是有效的,这取决于您的偏好。 For ... Next,Do ... Loop,While ... Wend,最后它们基本上都做同样的事情。 intCount = intCount + 1 在循环中使用时似乎有效。

    • 使用 Next (for ... next)

    使用计数器时,请始终使用 Next iCounter,因为它会递增计数器。

    希望这个回复对你有用!

    【讨论】:

    • 哦,以及 0 到 -1 的 For 循环它循环将运行 12 次,当变量具有 34 个值时,我第二次运行循环将运行 34 次,依此类推)。
    • 哦,是的,对于对象,您可以使用对象而不是变体。我的猜测是您需要引用一个库,然后执行类似 SPSS.() => 之类的操作,之后您可能会得到一个建议列表。但我不能肯定地说。
    • 我猜你最好从 1 数到 ubound(varLabels)。
    • 不知道目标值标签是什么,距离上次使用SPSS已经9年了。
    • 感谢 Kim,这个网站看起来很有用!
    猜你喜欢
    • 2014-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 2016-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多