BookmarkSubscribeRSS Feed
Srigyan
Quartz | Level 8

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.

13 REPLIES 13
andreas_lds
Jade | Level 19

@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).

noling
SAS Employee

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

Srigyan
Quartz | Level 8

Yes, you are correct

Reeza
Super User

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.


 

Srigyan
Quartz | Level 8

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

Reeza
Super User
You may want to contact tech support. I don't understand your question and SASpy and integration with python is pretty new and it seems like it's going to depend a lot on your set up.

If python doesn't have access to the server, that's not a SAS or Python issue, it's a set up issue.
Reeza
Super User
Reading your responses to others, this is sounding more and more like a set up of your network type issue, not a programming issue. If your python can't see the server that's something that needs to get fixed. Within SAS there are multiple methods of moving files, so you could move the file if that was allowed, but it wouldn't really be any different than reading it directly from the server. And all of these depend on set up so you need to talk to your IT team.
sastpw
SAS Employee

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: strtable: 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

sastpw
SAS Employee

Was this what you were trying to do? Were you able to accomplish it? Or was there something different with your scenario?

Thanks,

Tom

VDD
Ammonite | Level 13 VDD
Ammonite | Level 13
@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.

Srigyan
Quartz | Level 8

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.

Patrick
Opal | Level 21

@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.


@Srigyan 

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.

Tom
Super User Tom
Super User

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.

 

SAS Innovate 2025: Call for Content

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!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 13 replies
  • 4131 views
  • 4 likes
  • 8 in conversation