Created
February 19, 2014 05:02
-
-
Save FriedEgg/9086328 to your computer and use it in GitHub Desktop.
Example of how to use SAS Workspace APIs to upload a file to a remote SAS session. Uses SAS LanguageService, FileService and BinarySteam APIs, part of IOM.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example of how to use Python on Windows to | |
# transfer a file to a SAS Integration Technologies client | |
# You can connect to a remote SAS Workspace | |
# and write a file through a binary stream | |
# run a program and collect the SAS log | |
__author__ = 'FriedEgg' | |
import win32com.client,os,binascii | |
# create a local file containing a random string of characters | |
# this is what we will transfer to the SAS client | |
filename = 'output_file' | |
with open( filename , 'wb' ) as fout: | |
fout.write(binascii.b2a_hex(os.urandom(1024))) | |
# create the Integration Technologies objects | |
objFactory = win32com.client.Dispatch("SASObjectManager.ObjectFactoryMulti2") | |
# these xml files can be created using the SAS Integration Technologies Configutation Utility | |
objFactory.SetMetadataFile( "C:/path/to/serverinfo.xml" , "C:/path/to/userinfo.xml" , False ) | |
# create a connection to the SAS Workspace Server | |
objSAS = objFactory.CreateObjectByLogicalName( "SASApp - Logical Workspace Server" , "" ) | |
# create a file reference | |
assignedName = "" | |
objFile = objSAS.FileService.AssignFileref( "" , "TEMP" , "" , "" , assignedName ) | |
# open a binary stream for writing to the created fileref | |
StreamOpenModeForWriting = 2 | |
objStream = objFile[0].OpenBinaryStream( StreamOpenModeForWriting ) | |
# open local file and read by chunk, transfer chunk to remote SAS workspace through binary steam | |
with open( filename , "rb" ) as f: | |
chunk = f.read(512) | |
if chunk: | |
objStream.Write(chunk) | |
# close the stream | |
objStream.Close() | |
# a program to read the transfered file | |
program = "data _null_; infile " + objFile[1] + " lrecl=32767 length=len; input; put _infile_; run;" | |
# run the defined program | |
rc = objSAS.LanguageService.Submit(program) | |
# collect the SAS log | |
log = "foo" | |
while log != "": | |
log = objSAS.LanguageService.FlushLog(1000) | |
print(log) | |
objSAS.Close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment