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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Discussion stats
  • 1 reply
  • 2415 views
  • 0 likes
  • 1 in conversation