sassoftware / saspy

A Python interface module to the SAS System. It works with Linux, Windows, and Mainframe SAS as well as with SAS in Viya.
https://sassoftware.github.io/saspy
Other
367 stars 149 forks source link

Connecting to table in My library using saspy #522

Closed kshank5 closed 1 year ago

kshank5 commented 1 year ago

@tomweber-sas Hi Tom - I need help with a similar issue but with SaS Academic OnDemand version. I am connecting to the ondemand version and the session is created. However, it will not recognize any of my libraries. I am able to run sample codes to test my session with the SASHELP.CARS data. I tried qualifying the my library (Mylib) with the full pathname etc. Nothing has worked so far. print(sas.dirlist('/home/kshank0/EPG1V2/1_ Stats Programming/Final Project/')) is able to list the files in this directory. But the following will only list the SAS standard libraries.

for libref in sas.assigned_librefs(): print(libref) WORK SASDATA STPSAMP SASHELP MAPS MAPSSAS MAPSGFK SASUSER SAS

Finally, even if I have a table under WORK, it gives the error "Table WORK.stroke does not exist. This SASdata object will not be useful until the data set is created."

I read through this thread but dont understand what I need to do to fix it in the ondemand version. Please help. Thank you! K

tomweber-sas commented 1 year ago

Awesome, thanks! So, your session has a directory with your data sets in it, is that what you're describing about /home/kshank0/EPG1V2/1_ Stats Programming/Final Project/?

Since I don't see any code you're running or the output, it's not as clear as if I see what you're seeing. Like the thing about a table in WORK. Show me the code being run and the output it generates (and the SAS LOG if that's not already part of the output).

For your library, have you just tried creating a libref for it so you can then reference it? Issue a libname statement so then you can access it.

sas.saslib('x', path='/home/kshank0/EPG1V2/1_ Stats Programming/Final Project/')
sas.datasets('x')

What libraries are there that you are trying to access, beyond that one? Can you show the code and the results (errors) for any other case so I can help.

Thanks! Tom

kshank5 commented 1 year ago

Thank you, Tom. Here is the code:

import saspy
# import pandas as pd
sas = saspy.SASsession()
sas
for libref in sas.assigned_librefs():
    print(libref)
myTable = sas.sasdata('demo_I', libref='tstsaspy')
print(myTable.head(5))

Here is the list of libs and the error in accessing one of my libs:

C:\Users\kavit\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\kavit\PycharmProjects\pythonProject\SasPySessionSetup.py Using SAS Config named: oda SAS Connection established. Subprocess id is 37988

WORK SASDATA STPSAMP SASHELP MAPS MAPSSAS MAPSGFK SASUSER Table tstsaspy.demo_I does not exist. This SASdata object will not be useful until the data set is created. C:\Users\kavit\PycharmProjects\pythonProject\venv\lib\site-packages\saspy\sasioiom.py:1033: UserWarning: Noticed 'ERROR:' in LOG, you ought to take a look and see if there was a problem warnings.warn("Noticed 'ERROR:' in LOG, you ought to take a look and see if there was a problem") Traceback (most recent call last): File "C:\Users\kavit\PycharmProjects\pythonProject\SasPySessionSetup.py", line 23, in print(myTable.head(5)) File "C:\Users\kavit\PycharmProjects\pythonProject\venv\lib\site-packages\saspy\sasdata.py", line 240, in head df = self._returnPD(code, '_head') File "C:\Users\kavit\PycharmProjects\pythonProject\venv\lib\site-packages\saspy\sasdata.py", line 176, in _returnPD raise ValueError("Internal code execution failed: " + errorMsg) ValueError: Internal code execution failed: ERROR: Libref TSTSASPY is not assigned. SAS Connection terminated. Subprocess id was 37988

Process finished with exit code 1

Here is what I have in SAS (libraries folder)

image
tomweber-sas commented 1 year ago

So, you don't have a libref named tstsaspy assigned in the session you're connected to with saspy. What's that picture of librefs from? Have you tried assigning that libref so you can access the data?

kshank5 commented 1 year ago

YES! The libref did the trick. THAT is what I was missing. THANK YOU! But can you explain why we need to do this for our libs when Saspy recognises the standard libs from SAS in SASHELP?

For closure, here is what fixed it.

Steps: Right click on your lib in SAS and go to prpperties, copy the library path and set the path in python (saspy) for the libref in the below statement in python for the sas session you are creating from Saspy

sas.saslib('tstsaspy', path='/home/kshank5/EPG194/')
sas.datasets('tstsaspy')

PS: The picture was from my SAS environment (ODA)

Closing this issue; have another question and will open another issue for that, Not related to this thread!

tomweber-sas commented 1 year ago

Is the picture from SAS/Studio? SASPy doesn't make pictures like that. If so, Studio is just assigning that for you when you start it (you probably did something to tell it to do that). W/ saspy, you can have that pre-assigned for you too, so you don't have to submit that saslib method yourself each time you connect. In your Configuration Definition (in your sascfg_personal.py file), just add the following and then the libref will be assigned each time you connect:

       'autoexec' : "libname tstsaspy '/home/kshank5/EPG194';",
kshank5 commented 1 year ago

Yes, pic is from SAS Studio, not SasPy. Right, I programmatically created the lib using this statement in SAS Studio. libname tstsaspy '/home/kshank5/EPG194/'

Thanks for the additional tip on how to add to the sascfg_personal file. I think that will be more convenient for repetitive use. Thank you!