【问题标题】:Read the return value from VBScript into SAS将 VBScript 的返回值读入 SAS
【发布时间】:2017-03-10 08:59:15
【问题描述】:

我有一个 SAS 程序,它需要许多名称每个月都不同的输入文件。与其将 Windows 资源管理器中的单个文件名更新为 SAS 中的硬编码别名,或者将新文件名复制并粘贴到宏变量中,我希望 SAS 启动一个文件提示,我可以在其中选择输入。我在 Windows 7 上使用 SAS 9.4。

为此,我编写了一个 VBScript,它打开一个对话框,如果对话框被取消或关闭,则返回所选文件的路径或空白字符串。

' GetFilePath.vbs

Option Explicit

Function GetFilePath()

    Dim objExec, strMSHTA, wshShell

    GetFilePath = ""

    strMSHTA = "mshta.exe ""about:<input type=file id=FILE>" _
          & "<script>FILE.click();new ActiveXObject('Scripting.FileSystemObject')" _
          & ".GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);</script>"""

    Set wshShell = CreateObject( "WScript.Shell" )
    Set objExec = wshShell.Exec( strMSHTA )

    GetFilePath = objExec.StdOut.ReadLine( )

    Set objExec = Nothing
    Set wshShell = Nothing

End Function

'WScript.Echo GetFilePath()

GetFilePath()

理想情况下,我想将GetFilePath.vbs 的返回值分配给宏变量。但是,我不清楚该怎么做。我发现的唯一相关信息涉及使用unnamed pipe 将 I/O 流读入数据集。我想如果我能做到这一点,那么我至少可以使用CALL SYMPUT 将返回值分配给宏变量。但是,我无法让 SAS 读取返回值。

我一直在尝试的一个例子是,

filename getfile pipe "C:\temp\GetFilePath.vbs";

data test;
  infile getfile;
  input path $;
run;  

提示打开,但返回值未分配给path

【问题讨论】:

    标签: vbscript macros sas


    【解决方案1】:

    只需将 vbscript 文件的最后一行替换如下:

    WScript.StdOut.Write GetFilePath()
    

    并像这样调整您的 sas 代码(无需声明文件名):

    data test;
      infile "cscript.exe C:\temp\GetFilePath.vbs" pipe;
      input path:$100.; /* adjust length as appropriate */ 
    run; 
    

    “诀窍”是将输出写入 STDOUT,而不是回显。此外,根据answer,调用 .vbs 时需要 cscript.exe(对我而言,在 Windows 2012 中),以将输出保留在控制台中 - 而不是将其作为窗口弹回。

    顺便说一句,对一种简洁的方法表示敬意。我不明白如何使用 SAS 以这种方式启动(和读取)图形应用程序!

    这是我用于测试的最小工作示例:

    ' GetFilePath.vbs
    Option Explicit
    Function GetFilePath()
      GetFilePath  =  InputBox("Provide a value for SAS")
    End Function
    WScript.StdOut.Write GetFilePath()
    

    在 SAS 方面:

    data _null_;
      infile "cscript.exe C:\Temp\aaa.vbs" pipe;
      input; putlog _infile_;
    run;  
    

    【讨论】:

    • 好东西!如果您创建一个数据集,您会注意到引入了两个观察值;第一个是 Microsoft 免责声明,第二个是文件路径。似乎//NoLogo 是关闭免责声明的选项。如果我在 shell 中执行cscript.exe //NoLogo C:\temp\GetFilePath.vbs,我确实只得到了文件路径。但是,当我在 SAS 中尝试相同的语句时,没有将观察结果写入数据集。有什么想法吗?
    • 不确定 //NoLogo 选项,但要忽略 SAS 中的第一行,只需将以下行添加到您的数据步中:if _n_&gt;=4 then output;(适当调整数字)
    • 我发现这也行得通:data test; length path $ 260. ; infile "cscript.exe C:\temp\GetFilePath.vbs" pipe firstobs = 3; input path $ 260.; run; 路径长度为 260,因为这是 Windows 7 中的最大路径长度。
    猜你喜欢
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-06
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    相关资源
    最近更新 更多