Leaving SAS: the script

15 September 11. [link] PDF version

I typically have one question when somebody hands me a SAS data set: how can I get out of SAS as quickly as possible?

Here's my best answer to that question: a bash script that writes a SAS data set in plain text to stdout.

#Usage, assuming you saved this to {\tt leave_sas}: 
# ./leave_sas my_data_directory/sas_dataset.sas7bdat > outfile.csv

if [ ! -e $1 ] ; then
        echo "File $1 does not exist."
        # Use POSIX shell string handling to get the path and filename
        base=`basename $1 .sas7bdat`

        sas -noterminal -stdio <<-XXXXXX
            libname indata "$dir";

            PROC EXPORT
            DBMS=CSV REPLACE;

As noted, this writes to stdout, so you're going to get a lot of data on the screen unless you pipe to something else or write to a file. You know my fetish for the Apophenia library, so I'd do something like

./leave_sas a_data_file.sas7bdat | apop_text_to_db -d',' '-' data_tab usable.db

and then get to work via SQL. Or as per the example at the head of the script, you could just write output a text file, and use it as you would any other.

This was generated via consulation with several SAS experts (thanks, Dr CLE and Ms NGA of Washington, DC), but I couldn't arrive at much consensus that this is the Correct Way. Perhaps the ODS would be better, and perhaps the export procedure won't work for all data sets. If you have any additional recommendations or caveats, do leave them in the comments.

[Previous entry: "Scope in C is e-z"]
[Next entry: "Tip-a-day mode"]