SAS dataset to CSV

Hi Guys,

Is there a way to export a sas file i.e .sas7bdat file to .csv file with header and data using unix. I dont want to use SAS program instead using unix tool or unix scripting is it possible ?

That's highly probable.

Hi Rudic,

Can you let me know how it can be done, because i would require to read the sasdataset file and export. How it can be done in unix

I would REALLY like to let you "know how it can be done" - IF you gave e.g. a sample representing your "sasdataset" and a target output file structure. With sloppy generic request formulations like above, what else than sloppy generic answers do you expect?

Hi rudic,

For eg my file is sample.sas7bdat which has below columns

id name country
01 xyz AUS
02 yyy NZ

My sample.csv will have the same data

I don't understand your problem. If you want a character separated values (i.e. CSV) file (with an unspecified character as the separator), then your sample SAS dataset is a CSV file in the format you want. If you're saying that you want a copy of your SAS dataset file with the name sample.csv , try:

cp sample.sas7bdat sample.csv

If you're saying that you don't want to keep two copies of your file and just need to rename your file, try:

mv sample.sas7bdat sample.csv

If there is something else that you want done, please specify details about what you want done instead of giving generalities that could be solved hundreds of different ways that might or might not do what you really want.

Hi Don,

My sample.sas7bdat is a sas dataset file which hold these columns. Its not csv file. Just for sample data i mentioned. Basically i would require to read this sas dataset file and convert into csv file so that i can read the data along with columns

Your sample SAS dataset file sample.sas7bdat is a CSV file with <space> as the separator character. If that is not the format you want, you must tell us how the file needs to be transformed to produce a CSV file in the format you want. Repeatedly telling us that a CSV file is not a CSV file is not going to help us help you get what you want.

Hi Don,

Actually i am unable to view the sas7dbat file in unix , because its generated using sas. If view it using sas tool then i can able to see the columns and data accordingly which i showed you as an example how the data looks while viewing from SaS 9.4 tool.

Now my query was is there a way to read this sasdataset file and convert into readable format which is comma seprated. It can be exported as .csv or .txt whichever but in accesable format so that i can open the file and access the data in unix.

Your problem is becoming less and less evident to me.
What does "i am unable to view the sas7dbat file in unix , because its generated using sas" mean? Is that file available on your *nix host? Or, how would those output data be provided, and to where?
What does "read this sasdataset file" mean? Does it exist or not?

Please take a step back, carefully rephrase your request (considering my post#4 and Don Cragun's post#6), and draw the whole picture.

Hi Rudic/Don,

Let me clarify once again. I have a sas7bdat file which is present in unix box.
sample.sas7bdat.

I need to read the content of this file. if i do cat on this file it wouldnt be in proper readable format. Hence i would require how i can read this file to a read able format where i would require to export as .csv file. So that i can able to view the data

tr ' ' ',' <sample.sas7bdat >sample.csv

Hi,

Special characters are getting printed when i use the tr command

What special characters???

Do you mean the tr command is failing and printing a diagnostic message? If so, show us the exact output the tr command produces (in CODE tags).

Do you mean that sample.csv contains special characters after running the command:

tr ' ' ',' <sample.sas7bdat >sample.csv

? If so, show us the output produced by the command:

od -bc sample.csv

but, understand that tr didn't insert those "special" characters; they were already present in sample.sas7bdat .

Hi ,

The thing is i am unable to view cat the .sas7dbat file which is holding no many special characters

cat sample.sas7bdat
^]AA0^_BAAAx#DA_;CAkAo%KjAF5>oAT|lAo
                                                                                 @)PACWWAMALAYSIA INCL DSRNCCPL_IL[XXXX]$ xtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtemxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxtermxterm^C

od -bc sample.csv

0501720 346 202 123 107 104 345 205 101 123 055 123 107 104 343 202 123
        346 202   S   G   D 345 205   A   S   -   S   G   D 343 202   S
0501740 107 104 341 206 125 055 120 122 114 055 111 351 202 123 107 104
          G   D 341 206   U   -   P   R   L   -   I 351 202   S   G   D
0501760 345 210 123 151 156 147 141 160 157 162 145 341 202 123 107 104
        345 210   S   i   n   g   a   p   o   r   e 341 202   S   G   D
0502000

So, are we supposed to assume that this post was a lie? You didn't say anything about any special characters in sample.sas7bdat when you first told us you wanted to convert sample.sas7bdat to sample.csv .

What did you use to produce the above shown output from sample.sas7bdat ?

Hi,

This file is generated using SaS 9.4 script. I just did cat on this file and can able to see many special characters.It may be bacause i am unable to view the content of sas7bdat file like normal flat file

See my post#10, last line.

Please don't assume people to be familiar with Sas, and provide the (ALL!) details necessary to understand, analyse, and solve the situation and your problem.

Please show us the exact SAS command (or sequence of SAS commands) you used to produce the file sample.sas7bdat that you showed us (which did not contain any special characters) in post #5 in this thread.

Happened to use SAS for a while long time ago. It seems to me there is an option to do the conversion within SAS platform, which even has the GUI interface now. Something like:

proc export data=sample.sas7bdat
    outfile='sample.csv' 
    dbms=csv 
    replace; 
run; 

HTH.

2 Likes