1

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

FinDev
  • 429
  • 5
  • 16
  • It seems like you're missing the str= portion for the table name? `sasdata2dataframe(table: str = '2020_01_SETTLEMENTS'` .... but that table name is also not a valid SAS data set name so are you sure that's correct? – Reeza Aug 10 '20 at 20:16
  • @Reeza thank you for the quick reply. I just added str = to the table name portion and received the same error message. Do you see anything wrong with the ```LIBREF``` section. Do I need to specify SASApp or Libraries? – FinDev Aug 10 '20 at 20:19
  • It's also possible that when connecting via python your libraries are not defined automatically as they are in EG/Viya. – Reeza Aug 10 '20 at 20:19
  • 1
    @Reeza I'm using SAS EG. This is my first time using SASPy so it seems entirely possible that they may not automatically be defined. Do you know how I could check that? – FinDev Aug 10 '20 at 20:21

2 Answers2

1

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')
FinDev
  • 429
  • 5
  • 16
  • 1
    All you did was use `=` instead of `:` in the call. So is that the normal difference between how Python documentation is written compared to how the actual function/method calls are written? or is the saspy documentation not following normal Python standards for documentation? – Tom Aug 10 '20 at 21:00
  • 2
    @Tom I would lean towards the latter, the SASPy documentation does not seem to follow Python standards. I just changed ```table: str = '2020_01_SETTLEMENTS'``` to ```table='2020_01_SETTLEMENTS'``` – FinDev Aug 10 '20 at 21:43
  • @FinDev This is great feedback for them. You should let them know. – Stu Sztukowski Aug 11 '20 at 14:32
  • 1
    @StuSztukowski I will let them know! – FinDev Aug 11 '20 at 15:07
1

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.

Tom
  • 68
  • 6