Skip to content
AgileLogics
6 min read

Importing and Exporting Files

Real data arrives as files. In SAS OnDemand for Academics, upload files to your home folder first (right-click a folder in Server Files and Folders → Upload files).

Suppose sales.csv in your home folder contains:

date,region,product,amount
2024-01-05,North,Widget,120.50
2024-01-09,South,Gadget,80.00
2024-02-02,North,Gadget,99.90

PROC IMPORT

PROC IMPORT reads the header row and guesses each column's type:

/* illustrative: use your own home folder path */
proc import datafile='/home/u12345678/sales.csv'
            out=work.sales
            dbms=csv
            replace;
   getnames=yes;
   guessingrows=max;
run;

proc print data=work.sales;
run;
Obs          date    region    product    amount

  1    2024-01-05    North     Widget      120.5
  2    2024-01-09    South     Gadget       80.0
  3    2024-02-02    North     Gadget       99.9
  • dbms=csv names the file type; dbms=xlsx reads Excel (add sheet='Sheet1'; to pick a sheet).
  • replace overwrites work.sales if it exists.
  • guessingrows=max makes SAS scan every row before choosing types and lengths, instead of only the first rows — slower, but it avoids truncated text.
  • The ISO dates were recognized and read as SAS dates (displayed with a YYMMDD10. format).

PROC IMPORT writes a DATA step behind the scenes — you'll find it in the log. When you need full control, write that step yourself.

INFILE and INPUT

/* illustrative: use your own home folder path */
data work.sales2;
   infile '/home/u12345678/sales.csv' dsd firstobs=2;
   input date :yymmdd10. region :$10. product :$10. amount;
   format date date9. amount 8.2;
run;

proc print data=work.sales2;
run;
Obs         date    region    product      amount

  1    05JAN2024    North     Widget       120.50
  2    09JAN2024    South     Gadget        80.00
  3    02FEB2024    North     Gadget        99.90

Log (abridged):

NOTE: 3 records were read from the infile '/home/u12345678/sales.csv'.
NOTE: The data set WORK.SALES2 has 3 observations and 4 variables.
  • dsd — comma-delimited, with quoted values and empty fields handled correctly. Use dlm='09'x for tab-separated files, dlm=';' for semicolons.
  • firstobs=2 skips the header row.
  • :$10. reads a character value up to the delimiter, with length 10.
  • Check the "records were read" note against the file's line count.

A FILENAME statement can give the file a short name (a fileref): filename salesf '/home/u12345678/sales.csv'; then infile salesf dsd.

Exporting

/* illustrative */
proc export data=work.sales2
            outfile='/home/u12345678/sales_out.xlsx'
            dbms=xlsx
            replace;
run;

dbms=csv writes CSV. To convert a JSON export to a spreadsheet outside SAS, the JSON to Excel tool is handy.

Common mistakes

  • Letting PROC IMPORT guess from a few rows, then finding a long value truncated later — use guessingrows=max.
  • Forgetting firstobs=2, so the header row is read as data and NOTE: Invalid data appears for every numeric column.