Your Visual Basic program's
interaction with SAS begins by creating a SAS.Workspace object. This
object represents a session with SAS and is functionally equivalent
to a SAS Display Manager session or the execution of Base SAS software
in a batch job. Using a workspace from Visual Basic requires that
you declare an object variable in order to reference it. Here is an
example declaration:
Dim obSAS as SAS.Workspace
This statement only
declares a variable to reference a workspace. It does not create a
workspace or assign a workspace to the variable. For creating workspaces,
the SAS Integration Technologies client provides the object manager.
The object manager is an ActiveX component that can be used to create
SAS workspaces, either locally or on any server that runs the SAS
Integration Technologies product.
If the server is remote,
then you can create a workspace by using either the machine name of
the server (such as unx03.abccorp.com) or a logical name (such as
FinanceDept) that is given to you by your system administrator. For
more information about creating workspaces on remote servers, see
Using the SAS Object Manager .
If SAS is installed
on your local PC and you want to create a workspace that runs locally,
then it is very easy to use the object manager to create the workspace
and assign it to your object variable. The following example shows
how to create a workspace and assign it to your object variable:
Dim obObjectFactory As New SASObjectManager.ObjectFactory
Dim obSAS As SAS.Workspace
Set obSAS = obObjectFactory.CreateObjectByServer(
"", True, Nothing, "", "")
By using the keyword
new
, you instruct COM to create a SAS Object Factory
and assign a reference to it when the object variable (obObjectFactory
in this case) is first used.
After you create the
instance of the object factory, you can use it to create a workspace
(SAS session) on the local machine. The object factory has a CreateObjectByServer
method for creating a new workspace.
The first parameter
to this method is the name of your choice. The name can be useful
when your program is creating several objects and must later distinguish
among them. The next parameter indicates whether to create the workspace
synchronously. If this parameter equals true, then CreateObjectByServer
does not return until a connection is made. If this parameter equals
false, then the caller must get the created workspace from the ObjectKeeper.
The next parameter is
an object to identify the desired server for the workspace. Because
we are creating a workspace locally, we pass the Nothing
(a Visual Basic keyword) to indicate that there is no server object.
The next two strings are for a user ID and password. Again, we do
not need these to create a local workspace. This method returns a
reference to the newly created workspace.
To get error information
about the connection, you can use the following code:
Dim obSAS As SAS.Workspace
Dim obObjectFactory As New SASObjectManager.ObjectFactory
obObjectFactory.LogEnabled = True
Set obSAS = obObjectFactory.CreateObjectByServer("", True, Nothing, "", "")
Debug.Print obSAS.Utilities.HostSystem.DNSName
Debug.Print "Log:" obObjectFactory.GetCreationLog(True, True)
Note: Whenever you assign a reference
to an object variable, the assignment uses the keyword
Set
. If you do not use
Set
,
then Visual Basic attempts to find a default property of the object
variable and set that default property.
You can prove that the
previous example program code works by instructing the server to print
its Internet Domain Name System (DNS) name. The following program
creates a SAS workspace on the local computer, prints the DNS name,
and then closes the newly created workspace.
Dim obObjectFactory As New SASObjectManager.ObjectFactory
Dim obSAS As SAS.Workspace
Set obSAS= obObjectFactory.CreateObjectByServer(
"", True, Nothing, "", "")
Debug.print obSAS.Utilities.HostSystem.DNSName
ObSAS.Close
As you use other features
of SAS, you will need additional object variables to hold references
to objects created within your SAS workspace. These are declared similarly.
For example, if you want to assign SAS FILEREFs, then you might want
an object variable to reference your workspace's FileService. Here
is the declaration for this example:
Dim obFS as SAS.FileService
Such declarations never
use the keyword
new
because doing so asks
COM to create the object. Object variables for objects within the
workspace always reference objects that are created by the workspace,
not COM. In fact, when you use
new
in a declaration,
Visual Basic IntelliSense provides a much shorter list of possibilities
because it lists only those classes of objects that COM knows how
to create.
The following statement
uses the obFS variable to hold a reference to the FileService for
the workspace that is referenced by obSAS:
Set obFS = obSAS.FileService
The assignment of an
object variable must use the
Set
keyword.