Develop VB client with SAS interaction

Source: Internet
Author: User
Tags arrays execution ldap connect object model ole reference
Interactive | Client Profile
Programmers often ask which language can access SAS, that is, using SAS's it mechanism, which allows development customers to access SAS, programmers can quickly build strong applications with SAS in different languages, this article mainly introduces how to use VB to interact with SAS
Need to know before reading
This paper assumes that the reader has a certain understanding of Vb/com/sas knowledge.
SAS It's components
SAS It is a middleware that provides users with the interface to access SAS and render data, which includes the following features
1. LDAP (Lightweight Directory Access Protocol) directory integration
LDAP is an industrial standard for distributed data storage, and programmers can access the LDAP directory using Microsoft ADSI (Dynamic Directory Service Interface), and you can think of LDAP as a database that can be accessed over TCP/IP, usually with a single LDAP server. and is shared by all the machines of the organization.
2, publish/Subscribe
This positive messaging mechanism enables you to create SAS outputs (publishers) to those who are interested in those outputs (subscribers)
This mechanism is composed of the IOM (Consolidated object model) interface, the SAS language invocation syntax, and the LDAP object and work together
3, Message Queuing
SAS can output output to Message Queuing so that the client does not need to wait for SAS execution to complete
Detailed usage reference for Message Queuing:
Http://www.sas.com/rnd/itech/doc/messageq/index.htm
4. IOM (Integrated object model)
The IOM is a collection of COM objects, most of which are commonly used by VB
The advantages of SAS it offers OLE interfaces over previous versions
It is an extension of the previous OLE, and here is a comparison of the three major differences
1, it provides a multi-level interface, and OLE only provides a single interface
2, it is Cross-platform (SAS can support the platform, such as: Win/unix/os, etc.), VB application can be remote call it objects, and OLE can only run on the WIN platform
3. The It object allows SAS programs to run asynchronously, while OLE cannot
Note: The root object of the It object is "Sas.workspace", which corresponds to the OLE "Sas.application" Object
VB Programmer's IT Tool
Includes IOM, SAS workspace Manager, IOM data provider, IOM bridge for COM, Scripto
IOM
Clients can connect to the IOM server through multiple connections, such as CORBA/JDBC/COM/OMG, without the need to attach code to the client
The IOM is an object model because it exposes a set of objects for users to use
These objects can be used to achieve 2 goals: submitting code to SAS and obtaining SAS output
In the IOM object, the array is widely used, so all requests are returned at the same time.
Note that, without authorization from SAS IT products, the IOM interface can only be used by local COM and, if authorized, access the remote IOM interface via the network
At the IOM object level, the root object is workspace, and each of these objects has its own work library.
The Workspace object provides the following available objects
DataService object: Returns an interface to operate the SAS library, which also provides an interface to read and write SAS data, but VB programmers cannot use it directly, only indirectly via SAS data providers
Fileservice object: Returns an interface to read and write SAS server file and file references
Getapplication object: Returns a custom interface that has SAS/AF established
Languageservice object: Returns an interface to submit SAS code and get output
IOM use examples
Suppose the following code has been executed
Dim Obwsmgr as New _
Sasworkspacemanager.workspacemanager
Dim Obsas as SAS. Workspace
Dim Xmlinfo as String
' This is creates a SAS Server running on the
' Local Machine
Set Obsas = ObWsMgr.Workspaces.CreateWorkspaceByServer ("", Visibilitynone, Nothing, "", "", Xmlinfo)
Example 1: Submitting code
There are 2 ways to submit code to SAS SERVER
Mode 1: Using Languageservice
ObSAS.LanguageService.Submit _
"Data A; Do x= 1 to 10; Y=x*x*x; "& _
"Output;end;run;"
MsgBox ObSAS.LanguageService.FlushLog (100000)
Mode 2: Using Storedprocessservice
This way the SAS file is requested to be stored in a known directory, and can pass parameters to the file through macros, and in the SAS file to receive parameter information, you need to use this syntax:
*processbody;
It automatically converts incoming arguments to macros.

If you have multiple parameters, separate them with "space" when they are passed in
For example:
' Run the SAS program at C:\temp\looper. Sas
Dim Obstoredprocessservice As _
Sas. Storedprocessservice
Set Obstoredprocessservice = _
ObSAS.LanguageService.StoredProcessService
Obstoredprocessservice.repository = _
"File:c:\temp"
Obstoredprocessservice.execute "Looper", _
"Looptimes=10"
MsgBox ObSAS.LanguageService.FlushLog (100000)

The contents of the Looper.sas file are as follows:
%let looptimes=3;
*processbody;
Data A;
Do x= 1 to &loopTimes;
Y=x*x*x;
Output
End
Run
Example 2: Code execution
Async property: If False, execute synchronously, no, for asynchronous execution, you can get whether the execution completed successfully through the event
The following defines an error occurrence event:
Public WithEvents Obsaslanguage as _ SAS. Languageservice
' To enable events, you must associate the
' Obsaslanguage
' interface with the same languageservice
' interface used to make calls.
Set oblanguage = Obsas.languageservice
Oblanguage.submit "This is a error;run;"
Private Sub Oblanguage_steperror ()
' An error has occurred. Dump the Log
Debug.Print Oblanguage.flushlog (100000)
End Sub
Example 3: Getting output
SAS outputs multiple types of information for users to use, as follows
IOM data Provider can provide binary access to users
Languageservice's Flushlist Flushlistlines method can obtain the SAS window output
If you want to get ODS output, you can do this by using a file reference, SAS provides fileservice services
The following shows how to obtain output in a generic fileservice way
Dim Obfileref as SAS. FileRef
Dim Obtextstream as SAS. TextStream
Dim Obfilesystem as New Scripting.FileSystemObject
Dim Obfile as Scripting.TextStream
Set obfile = Obfilesystem.createtextfile ("c:\temp\sasOutput.htm", True)
ObSAS.LanguageService.Submit "filename fref TEMP;" & "ODS HTML Body=fref;" & "proc Corr Data=sashelp.class;" & "Run;" & "ODS HTML close;"
Set obfileref = ObSAS.FileService.UseFileref ("Fref")
Set Obtextstream = Obfileref.opentextstream (streamopenmodeforreading, 10000)
Sodsoutput = Obtextstream.read (100000)
while (Len (sodsoutput) > 0)
' Do something and the read text here
Obfile.write Sodsoutput
Sodsoutput = Obtextstream.read (100000)
Wend
WebBrowser1.Navigate "C:\temp\sasOutput.htm"
In fact, in a stand-alone environment there is no need to do so:)

You can also get output using Resultpackageservice, which can be referenced by Workspace.Utilities.ResultPackageService
You can use the interface provided by Resultpackageservice to build resultpackage, and of course, the call syntax in the SAS language
A resultpackage can be any SAS output, such as file, DataSet, ODS output, picture, etc.
The following example shows how to create a resultpackage
%macro CHECKRC (RC);
If RC ne 0 then do;
msg = Sysmsg ();
Put msg;
ABORT;
End
%mend;
Data _null_;
Call Package_begin (PID, DESC, Nameval, RC);
%CHECKRC (RC);
Call Insert_file (PID, ' Fileref:fref ',
"TEXT", "text/html", "Some ODS Output",
", RC);
%CHECKRC (RC);
/* Nothing in the package actually gets
* Written out until we call publish.
* So, if your modify any filerefs
* Calling insert but before calling
* This is then you'll get the
* Modified fileref.*/
Call Package_publish (PID, "To_archive", RC,
"Archive_path, Archive_name", "C:\Temp",
"Archive");
%CHECKRC (RC);
/* You could call Package_publish as many
* Times as your want for any given package,
* As long as you
* do so before calling Package_end. */
Call Package_end (PID, RC);
%CHECKRC (RC);
Run
The following shows how to read this Resultpackage
Dim props () as String
Dim Obresultpackage as SAS. Resultpackage
Dim Obfileentry as SAS. Resultpackagefileentry
Dim Obrps as SAS. Resultpackageservice
Set Obrps = ObSAS.Utilities.ResultPackageService
Set obresultpackage = obrps.browseresultpackage ("ARCHIVE", "C:\TEMP\ARCHIVE.SPK", props)
Set obfileentry = obresultpackage.getentry (0)
Set Obtextstream = Obfileentry.open (streamopenmodeforreading, 100000)
Sodsoutput = Obtextstream.read (100000)
while (Len (sodsoutput) > 0)
' Do something and the read text here
Obfile.write Sodsoutput
Sodsoutput = Obtextstream.read (100000)
Wend
WebBrowser1.Navigate "C:\temp\sasOutput.htm"
About SAS Workspace Manager
It is an ActiveX control that completes the following functions
1, it establishes the connection with the SAS, and returns the work space
2, it provides a pool to ensure that each workspace is safe in the Web environment
3, automatically maintain the connection with the SAS
You can use the following 2 ways to establish a connection with SAS
Non-managed mode (all connection information remains on the client)
Managed mode (so the connection information remains in the file or LDAP)
These connection information includes which machine to connect to, what protocol to use, what port or service name, user name, user ID, and so on, while DCOM or the IOM support encrypted session
Example 1: Establishing a connection, not managed mode
Dim Obwsmgr as New _
Sasworkspacemanager.workspacemanager
Dim Obsas as SAS. Workspace
Dim Xmlinfo as String
Dim ObServer as New _
Sasworkspacemanager.serverdef
Observer.machinednsname = "Remote.sas.com"
Set Obsas = _
ObWSMgr.Workspaces.CreateWorkspaceByServer _
("", Visibilitynone, ObServer, "", "", _
Xmlinfo)
The Xmlinfo parameter returns the value that the server actually uses
About IOM Data Provider
IOM data provider is an OLE DB provider who uses the IOM dataservice read and write data
You can use ADO to read and write data
For example:
Set Obsas = _
ObWSMgr.Workspaces.CreateWorkspaceByServer _
("", Visibilityprocess,observer, "", "", _
Xmlinfo)
Dim obconnection as New ADODB. Connection
Obconnection.open _
"Provider=sas.iomprovider.1;" & _
"SAS Workspace id=" & Obsas.uniqueidentifier
Dim Obrs as New ADODB. Recordset
Obrs.open "Sashelp.class", Obconnection, _
adOpenDynamic, adLockReadOnly, _
adCmdTableDirect
Set Mschart1.datasource = Obrs
About IOM COM bridge
When SAS runs Windows, you can use COM or DCOM, but when SAS run on UNIX or OS, you cannot use COM technology to invoke Sas,sas to provide the IOM COM bridge, which facilitates the rendering of COM interfaces to clients on the one hand, using tcp/ IP communicates with SAS

About Scripto
Scripto is a Acitvex object for scripting applications that solves several limitations of scripting interacting with SAS component objects, such as VBScript, which can only return a single parameter; VBScript requested all variables to be variant, so the number of ancestors was also processed as variant,scripto to solve these 2 problems, allow the return of multiple parameters, you can use the array for transmission
' Scripto'll do 2 things:
' 1 It'll convert the 3 returned arrays to arrays of variants instead of arrays of
' Longs so VBScript can handle them
' 2) It allows more than 1 return parameter
Set Obsas = CreateObject ("SAS". Workspace ")
Set Obscripto = _
CreateObject ("Sasscripto.scripto")
ObSAS.LanguageService.Submit _
"Proc Options;run;"
Obscripto.setinterface Obsas.languageservice
' ObSAS.LanguageService.FlushLogLines 1000, Carriagecontrols, Linetypes, Loglines
Dim Flushloglinesparams (3)
Flushloglinesparams (3) = 1000
Obscripto.invokemethod "Flushloglines", _
Flushloglinesparams
' Flushloglinesparams (0) now has Loglines
' Flushloglinesparams (1) has linetypes
' Flushloglinesparams (2) has Carriagectrls
' This prints the '
WScript.Echo Flushloglinesparams (0) (0)

About it clients
You need to install and register several files on the client
The necessary files for it clients are automatically installed, and by calling Inttech.exe, they install the necessary files through the network



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.