Introduction

SAS Micro Analytic Service 2.2 supports modules that are written in the Python programming language. A Python module represents a collection of Python functions, and each of the module's methods represents one Python function. Python modules can be published to SAS Micro Analytic Service and called from DS2. (See DS2 Interface to Python.) If your SAS solution supports it, Python modules can be published and called directly.
Here is an example of Python code that can be used by SAS Micro Analytic Service:
def func0():
    print('func1')

def func1(arg1, arg2):
    "Output: arg3, arg4"
    func0()
    arg3=arg1 + arg2
    arg4 = arg3 + 1
    return arg3, arg4

def func2(arg1, arg2):
    "Output: arg3"
    func0()
    arg3=arg1 + arg2
    return arg3
The parameters listed with the function definition are considered the input arguments for the function. The first line after the function declaration must be a quoted string containing the word "Output:" followed by all of the outputs for the function.
This example has no output. Input arguments are given in the function's argument list. This example has input variables a and b. Outputs of the function must be listed after "Output:" in the quoted string that follows the function definition. The output variables should match the variables listed in the return statement.
def calcATimesB(a, b):
  "Output: "
  print ("Function with no output variables.")
  c = a * b
  print ("Result is: ", c, ", but is not returned")
  return None
Note: Input and output argument names live in a single namespace. Therefore, they cannot be the same. This means that in_out arguments are not supported. This is true for all module types in SAS Micro Analytic Service. This is not an issue in Python, as a new variable can be assigned the value of an input argument and then safely added to the output list. If the "Output:" line is missing, the function is not exposed as a callable function through SAS Micro Analytic Service. However, that function can be called internally. As a result, any function without the "Output:" line is a private function. The func0() function is an example of such a private function. SAS Micro Analytic Service parses the code to create a dictionary of the methods and their signatures.
The following data types are supported between SAS Micro Analytic Service and Python.
SAS Micro Analytic Service Type
SAS Micro Analytic Service Functions
Python Type
Python Example
String
  • sfSymGetString()
  • sfSymGetStringArray()
  • sfSymSetString()
  • sfSymSetStringArray()
Unicode string
outStr = unicode('abcdef')
BigInt
  • sfSymGetBigInt()
  • sfSymGetBigIntArray()
  • sfSymSetBigInt()
  • sfSymSetBigIntArray()
Long
outLong = 10
Double
  • sfSymGetDouble()
  • sfSymGetDoubleArray()
  • sfSymSetDouble()
  • sfSymSetDoubleArray()
Float
outFloat = 10.10
Boolean
  • sfSymGetBool()
  • sfSymGetBoolArray()
  • sfSymSetBool()
  • sfSymSetBoolArray()
Boolean
outBool = True
Datetime
  • sfSymGetDatetime()
  • sfSymGetDatetimeArray()
  • sfSymSetDatetime()
  • sfSymSetDatetimeArray()
Datetime
outDatetime = datetime.datetime(2013, 12, 22, 11, 30, 59)
Date
  • sfSymGetDate()
  • sfSymGetDateArray()
  • sfSymSetDate()
  • sfSymSetDateArray()
Date
outDate = datetime.date(2013, 12, 22)
Time
  • sfSymGetTime()
  • sfSymGetTimeArray()
  • sfSymSetTime()
  • sfSymSetTimeArray()
Time
outTime = datetime.time(11, 30, 59)