Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run SAS from VBA with full access

I hope someone can help. I'm trying to build a subroutine to launch SAS from VBA. I've been doing so with SAS Workspace Manager. As I'm strolling pretty far from my comfort zone I've followed mostly what I found in these two sources. Here's what I came up with so far in VBA:

    Public Sub SubmitSasProg(usrid As String, passid As String, path As String, sasprog As String, varinput As String, Optional logreturn)
    Dim obWsMgr As New SASWorkspaceManager.WorkspaceManager

Dim obSAS As SAS.Workspace
Dim xmlInfo As String

Set obSAS = obWsMgr.Workspaces.CreateWorkspaceByServer("Local", VisibilityProcess, Nothing, usrid, passid, xmlInfo)

Dim obStoredProcessService As SAS.StoredProcessService
Set obStoredProcessService = obSAS.LanguageService.StoredProcessService

obStoredProcessService.Repository = "file:" & path
obStoredProcessService.Execute sasprog, varinput

If IsMissing(logreturn) Then logreturn = 100000
MsgBox obSAS.LanguageService.FlushLog(logreturn)

End Sub

And I have myself a little SAS program, let's call it "Test.sas":

 %let loopTimes=3;
*ProcessBody;
data a;
do x= 1 to &loopTimes;
y=x*x*x;
output;
end;
run;

Now this line will work juste fine:

Call SubmitSasProg("myuserid", "mypassword", "somepath", "Test", "loopTimes=10")

But whenever I try to execute a SAS proceedure that modifies files/libnames etc. I'll get a either a "Invalid operation for this SAS session" or "User does not have access". Please note that I'm using SAS locally, not on a server. So I'm guessing that I'm not correctly logged in with my SAS worksession and lack permission. I thought that the userId and password parameters in CreateWorkspaceByServer are supposed to log me in.

So my question would be how to succesfully start the SAS session with my credentials on my local computer and have all the normal access I'd have by opening the windowed environement.

Just to clarify, this SAS process would work fine in the windowed environement:

   Data _NULL_;
      *x del C:\WINDOWS;
      x mkdir C:\Users\Myname\Desktop\NewFolder;
   run;

But it would fail with an "Invalid operation for this SAS session" code if started from VBA. Something similar happens if I try to write SAS datasets.

I've been looking for some time now but most threads are about SAS server sessions. Any help would be appreciated.

like image 887
Pane Avatar asked Jul 18 '13 19:07

Pane


People also ask

How do I run VBA code in SAS?

Example 1 : Create a new workbook and type in a cell The following SAS program opens a sample excel workbook and type 'Welcome to Excel' in cell A1. The translated VB Script would be stored in the temp location by the name 'vbscript'. Type %put vba_loc; to see the location of the code.

How do I run a SAS code in Excel?

Detailed steps included below: 1) With Excel open press ALT+F11, this launches the VBA Editor 2) Double click on “ThisWorkbook” under the workbook you have open, in the example this is “PhuseButton. xlsx” 3) Copy and paste the code from appendix 1 to the text field where it says “Option Explicit” in Figure 1.

Can VBA interact with Internet Explorer?

VBA macro drives internet explorer using its DOM properties and methods. The macro allows to interact with web page controls (text fields and buttons).

What is excel VBA used for?

What Is VBA Used for? VBA is used to further expand what some programs are able to accomplish. VBA is often used to create macros, automate processes, generate custom forms, or perform repetitive tasks that may need minimal human intervention.


1 Answers

Well, it looks like most of my problems stem from the fact that I'm not an admin on my system. I have managed to write datasets with the above technique given that I have write privileges on the directory with my account (duuh). Allas, the x command is stubborn and won't work lest I get myself an admin account. So, two choices to «solve» this problem: 1. Get administrator privileges. or 2. Forget the workspace manager. Use OleObjects.

While less elegant and perhaps more time consuming upon execution (not tested) OleObject will let me use SAS to its full extent. Here is the VBA code:

Dim OleSAS As Object
Set OleSAS = CreateObject("SAS.Application")
OleSAS.Visible = True
OleSAS.Top = 1
OleSAS.Title = "Automation Server"
OleSAS.Wait = True
OleSAS.Submit(yourSAScode)
OleSAS.Quit
Set OleSAS = Nothing

If you want to run a specific process and change some macro variables as with *ProcessBody; just do OleSAS.Submit("%let "& variable_name & "=" & "yourValue") and OleSAS.Submit("%include" & your_program).

Anyway, I'm pretty sad of loosing the Log report return that I had with the Worspace Manager, it was really great for fast debugging. Hope this was usefull.

like image 180
Pane Avatar answered Oct 02 '22 04:10

Pane