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=csvnames the file type;dbms=xlsxreads Excel (addsheet='Sheet1';to pick a sheet).replaceoverwriteswork.salesif it exists.guessingrows=maxmakes 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. Usedlm='09'xfor tab-separated files,dlm=';'for semicolons.firstobs=2skips 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 IMPORTguess from a few rows, then finding a long value truncated later — useguessingrows=max. - Forgetting
firstobs=2, so the header row is read as data andNOTE: Invalid dataappears for every numeric column.