I wanted to read a csv file which is saved in sas 9.4. Just wanted to confirm i dont want to import sas data in python first then read it.
I want to read the csv file directly.
@Srigyan wrote:
I wanted to read a csv file which is saved in sas 9.4. Just wanted to confirm i dont want to import sas data in python first then read it.
I want to read the csv file directly.
The blue marked sentence does not make much sense.
In SAS csv-files are read by data-step (or by proc import, but this i can't recommend).
I would think you would want to write out your SAS dataset as a csv, then read it in using python. Not sure if I'm missing something.
Register today and join us virtually on June 16!
sasglobalforum.com | #SASGF
View now: on-demand content for SAS users
Yes, you are correct
SAS doesn't really store CSVs. It would be on a network drive or server somewhere instead and you would have the same acesss.
It could be on the SAS server, but that can be treated as a drive for CSV files.
@Srigyan wrote:
I wanted to read a csv file which is saved in sas 9.4. Just wanted to confirm i dont want to import sas data in python first then read it.
I want to read the csv file directly.
To communicate with sas 9.4 python have saspy but unfortunately it can't read csv without uploading this in python. So is there any way... Just to reiterate sas 9.4 have it's own server which is not window, in my case Linux but python runs on Windows, hope now you understand the complexity of this question
It he following what you are trying to do? If not which parts are not right?
1) you have python, and saspy, on your local windows.
2) SAS is remote on Linux.
3) you have a local .csv file on your windows machine
4) you want SAS on the remote Linux to actually read in the .csv file
The above can be accomplished with the current version of saspy (2.4.4) like so...
import saspy
sas = saspy.SASsession()
sas
res = sas.upload('c:\\local_csv_file.csv', sas.workpath) # you should have access to write to work
# or to any directory res = sas.upload('c:\local_csv_file.csv', '/whatever/directory/you/can/write/to')
sascsv = sas.read_csv(sas.workpath+'local_csv_file.csv')
# or sascsv = sas.read_csv(/whatever/directory/you/can/write/to/local_csv_file.csv')
sascsv.head() # sascsv is the SASdata opbject for the new SAS data set
...
Here's the signature, so you can specify the libref and table name and other things too
read_csv
(file: str, table: str = '_csv', libref: str = '', results: str = '', opts: dict = None) → saspy.sasdata.SASdata
You can name the file something different on the remote system too, just specify the full path and file name on upload:
# res = sas.upload('c:\local_csv_file.csv', '/whatever/directory/you/can/write/to/remote.csv')
# sascsv = sas.read_csv(/whatever/directory/you/can/write/to/remote.csv')
Is this what you are looking to do? (I hand typed that in, so forgive me if there's a typo)
Tom
Was this what you were trying to do? Were you able to accomplish it? Or was there something different with your scenario?
Thanks,
Tom
@Srigyan I wanted to read a csv file which is saved in sas 9.4. Just wanted to confirm i dont want to import sas data in python first then read it.
I want to read the csv file directly.
I think what you are says is
I want to read a csv file that is created through a SAS 9.4 process. I just want to confirm if I can read the csv file into Python or can I read into SAS a csv file created by Python.
Yes CSV is not SAS nor Python required to read. You can even import a csv file into Excel.
When I say sas9.4 it means data are stored on sas server which is actually Linux environment but you are running python on window environment. Saspy can upload this csv in python then read it. But I am not aware of it can read it without uploading in python.
@Srigyan wrote:
When I say sas9.4 it means data are stored on sas server which is actually Linux environment but you are running python on window environment. Saspy can upload this csv in python then read it. But I am not aware of it can read it without uploading in python.
To express what you write a bit differently:
1. You have a .csv saved on a file system that is accessible via a SAS Server you can connect to
2. You have Python available to you on your local Windows PC
3. You can't access the file system where the .csv is stored directly from your Windows PC
4. You don't really want to use SAS. You just want to process the .csv using your local instance of Python
If above is correct then this has nothing to do with SAS as such. What you need is a way to either map the remote file system to your local PC or you just need to download the .csv to your local environment.
If this is a one off task then for downloading: If you have something like WinSCP installed locally then use this client to download the .csv. If you don't know how this works then contact your IT department/SAS Admin to help you with this.
Python can read a CSV file.
But note that a CSV file does not contain ANY metadata about the columns, other than the optional header row.
So your program in Python will have to know how to treat the columns in the CSV file or else you can get stupid things like character variables being interpreted as numeric just because the values in this particular file happen to look like numbers written as strings. So if you have a character variable with leading zeros then they might be lost if treated as a number.
A SAS dataset does contain information about the columns, like whether it is text of a number, a longer SAS label in addition to the variable name. Also the SAS metadata on format and informat.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.