使用SAS Office Add in将数据从SAS附加到Excel中

时间:2011-05-12 15:04:59

标签: excel sas

我有一个希望每月运行的SAS项目。生成的数据需要加载到Excel工作簿中。有没有办法将新月数据附加到前几个月的末尾,而无需刷新所有数据?

2 个答案:

答案 0 :(得分:1)

您可以具体说明粘贴数据的位置,因此,如果您跟踪下一次更新的位置,可以使用以下内容将其粘贴到那里:

data _null_;
  set try;
  file dde "EXCEL|sheet!R10C1:R150C20" notab lrecl=2000; #sheet & cell refs;
  put var1 var2 varn;
run;

我之前没有使用过这个,所以我不能再发表评论了。

我建议的方法是将每月更新添加到SAS中的滚动历史数据集,然后将所有数据导出到Excel。您可以更好地控制格式化以及将来可能希望在SAS内部而不是Excel中进行的任何分析。

答案 1 :(得分:0)

当然 - 使用VBA&国际移民组织!这应该可以解决问题:

Dim obSAS As SAS.Workspace
Dim obWorkspaceManager As New SASWorkspaceManager.WorkspaceManager
Dim obConnection As New ADODB.Connection

Sub Connect_to_SAS()
   Dim obServerDef As New SASWorkspaceManager.ServerDef
   Dim xmlString As String
   Dim errorXML As String
   Dim myUserid As String
   Dim myPwrd As String
   Dim myPort As String
   Dim myServer As String

  ' Enter these params
   myPort = 8561
   myServer = "blah.companyname.com"
   myUserid = "you@saspw"
   mytargetsheet = "Sheet1" ' where the data is going
   mytargetrow = 2 ' where the data gets pasted

   ' connect to sas
   obServerDef.Port = myPort
   obServerDef.Protocol = ProtocolBridge
   obServerDef.MachineDNSName = myServer
   myPwrd = InputBox("User = " & myUserid & vbCrLf & vbCrLf & _
    "Please enter SAS password below", "Login Prompt", "Password")
   If myPwrd = "" Then End
   Set obSAS = obWorkspaceManager.Workspaces.CreateWorkspaceByServer( _
        "My Ref", VisibilityProcess, obServerDef, myUserid, myPwrd, xmlString)
   If (Len(errorXML) > 0) Then MsgBox errorXML
    'submit your sas code
   obSAS.LanguageService.Submit "data x; x=1; run;"
   ' retrieve data (cols not needed as we are doing an append)
    Dim obRecordSet As New ADODB.Recordset
    obConnection.Open "provider=sas.iomprovider.1; SAS Workspace ID=" _
        + obSAS.UniqueIdentifier
    obRecordSet.Open "work.x", obConnection, adOpenStatic, adLockReadOnly _
        , adCmdTableDirect
    Sheets(mytargetsheet).Cells(mytargetrow, 1).CopyFromRecordset obRecordSet
   ' close session
    obWorkspaceManager.Workspaces.RemoveWorkspace obSAS
    obSAS.Close
End Sub