BookmarkSubscribeRSS Feed
dnye
Calcite | Level 5

If a previous session of SAS is open, when I use CreateObject("SAS.Application"), it connects to the previous session of SAS instead of opening a new session. If multiple sessions are open, each run of the CreateObject code runs in the successive instance until it has been run in all instances, at which time the next run will create a new instance of SAS, e.g. if I have 3 open sessions, the first time I run the CreateObject code, it runs in session 1, the next time in session 2, the next time in session 3 and the next time it opens a new SAS instance.

Does anyone know how I can modify the code to open and use a new session each time it is run?

VBA:

Sub run_SAS()

    Dim SAS As Object

    Set SAS = CreateObject("SAS.Application")

    SAS.Visible = True

    SAS.Submit ("%let gVar=Something;")

    '...

    Set SAS = Nothing

End Sub

1 REPLY 1
dnye
Calcite | Level 5

Here is my solution: Count the number of SAS sessions before attempting to create a new one. Loop through the CreateObject until a new one is created. If there are no previous sessions or the previously sessions have already been looped through by a previous calls, it immediately creates one.

Function get_num_sas_sessions() As Integer

    Dim objWMIService As Object

    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

    Dim colItems

    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE name = 'SAS.EXE'")

    Dim intSASCount As Integer

    intSASCount = 0

    For Each itProcess In colItems

        intSASCount = intSASCount + 1

    Next

    Set objWMIService = Nothing

    Set colItems = Nothing

    get_num_sas_sessions = intSASCount

End Function

Sub run_SAS()

    Dim SAS As Object

    Dim intNumSAS As Integer

    intNumSAS = get_num_sas_sessions()

    Do Until get_num_sas_sessions() > intNumSAS

        Set SAS = CreateObject("SAS.Application")

    Loop

    SAS.Visible = True

    SAS.Submit ("%let gVar=Something;")

    '...

    Set SAS = Nothing

End Sub

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Discussion stats
  • 1 reply
  • 2358 views
  • 0 likes
  • 1 in conversation