FinDev
FinDev

Reputation: 439

How to specify LIBREF in SASPy when turning a SAS table into a Python Dataframe

Please see the image below for my SAS Server Layout. I would like to pull the top table 2020_01_SETTLEMENTS and place it into a Python data frame. I have successfully established connection between Python and the SAS Server. I have the following code below but I believe where I am going wrong is in the LIBREF part of the sasdata2dataframe function. I have listed the error message below as well. Much appreciated to anyone who can tell me where I went wrong. I've also linked the reference material to this function here:

https://sassoftware.github.io/saspy/api.html

Code:

import saspy
import pandas as pd
from IPython.display import HTML

sas = saspy.SASsession(cfgfile=r'C:\Users\P1\Python_Code\sascfg_personal.py')

sasdata2dataframe(table: str = '2020_01_SETTLEMENTS', libref: str = 'BANKMKR', dsopts: dict = None, method: str = 'MEMORY')

Error Message:

 File "<ipython-input-18-41bb6a0902ac>", line 1
    sasdata2dataframe(table: '2020_01_SETTLEMENTS', libref: str = 'BANKMKR', dsopts: dict = None, method: str = 'MEMORY')
                           ^
SyntaxError: invalid syntax

SAS Server Layout

Upvotes: 1

Views: 1155

Answers (2)

Tom
Tom

Reputation: 68

I only saw this today, and I thought I’d shed some light on what I see in this track. And, I’m trying to help, not be a jerk, so please read it that way (I wrote it that way 😊 ). The issue here is simply familiarity with the language; python in this case. The API doc is an auto-generated section, based upon the code itself; method definitions and doc-strings. So, the methods shown are the definitions (signatures), not examples of how to call them. This may or may not help with that, but here’s the doc on function (method) definitions: https://docs.python.org/3.5/reference/compound_stmts.html#function-definitions

For instance, in C you might define a function as

int max(int num1, int num2) { … },

but that isn’t an example of how to call it from your source code; that’s a function definition that tells you everything you need to know about it to be able to call it. If you tried to code the definition as a call to it, you’d get a syntax error from the compiler, same as the syntax error python gave trying to code a : instead of an = in a function call.

value = int max(int 3, int 5);

is a syntax error. The right way to call that function is

value = max(3,5);

That’s the same thing with any language. The function definition tells you about the function; it’s not a sample of invoking it from your code. There’s a lot of information to be gleaned from a python function definition; what parms are required or not, which are positional, keyword, or both. if there are defaults, and what they are, or not. If the parm has a specific data type or not. All of this info is in the definition. But to make a call from your code, you use that info to code it how you want to call it, with the correct language syntax. Hope this makes sense. Again, just trying to help.

Upvotes: 1

FinDev
FinDev

Reputation: 439

Although as it is not specified in the manual, I ended up using the following code instead and it worked. I just changed table: str = '2020_01_SETTLEMENTS' to table='2020_01_SETTLEMENTS'

tempTable = sas.sasdata2dataframe(table='2020_01_SETTLEMENTS', libref='BANKMKR')

Upvotes: 1

Related Questions