Read External Data Into SAS: 1. The IMPORT Procedure

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Read External data into SAS

Two methods for reading external data into SAS

1. The IMPORT Procedure

The IMPORT procedure reads data from an external data source and writes it to a SAS data set. You can
import structured and unstructured data using PROC IMPORT. You can import delimited files (blank,
comma, or tab) along with Microsoft Excel files

By default, the IMPORT procedure expects the variable names to appear in the first row.
The procedure scans the first 20 rows to count the variables, and it attempts to determine the
correct informat and format for each variable.

In addition, you also have the following options:

indicate how many rows SAS scans for variables to determine the type and length
(GUESSINGROWS=)
modify whether SAS extracts the variable names from the first row of the data set (GETNAMES=)
indicate at which row SAS begins to read the data (DATAROW=)

1
1) Importing an Excel File with an XLSX Extension

proc import datafile=' /courses/d4c186d5ba27fe300/boots.xlsx'


dbms=xlsx
out=work.bootsales
replace;
sheet=boot;
getnames=yes;
run;

2) Importing a Comma-Delimited File with a CSV Extension

proc import datafile='/courses/d4c186d5ba27fe300/boot.csv'


dbms=csv
out=shoes
replace;
getnames=no;
run;

3) Importing a Space-Delimited File with a TXT Extension

filename stdata '/courses/d4c186d5ba27fe300/state_data.txt';


proc import datafile=stdata
dbms=dlm
out=states
replace;
delimiter=' ';
getnames=yes;
run;

Example2
Given the following SAS program:

proc import datafile= C:\workshop\ od c .da


dbms=tab
out=work.import;
run;

If work.import already exists, what will happen when the program is submitted?
A. The program will prompt the user for a new output data set name.
B. The program will overwrite the work.import data set.
C. The program will not complete and will not create a new data set.
D. The program will create a new data set named work.import2.

2
Example3
A programmer writes a SAS program using PROC IMPORT to read a delimited text file with the following
properties:
It has 100 rows and 10 variables.
One variable is named First_Name.
Within the first 20 rows of data, the longest value of First_Name is 10 characters.
Later in the delimited file, there are values of First_Name that are 30 characters long.

How should the programmer ensure that no values of First_Name are truncated in the new data set?

A. Add a format statement to the PROC IMPORT.


B. Add a guessingrows statement to the PROC IMPORT.
C. Set variable length prior to running the PROC IMPORT.
D. Add a length statement to the PROC IMPORT.

2. Reading Microsoft Excel Data with the XLSX Engine

To read the Excel workbook file, SAS must receive the following information in the DATA step:

a libref to reference the Excel workbook to be read


the name of the Excel worksheet that is to be read

To read in this workbook, create a libref to point to the workbook's location:

libname certxl XLSX '/courses/d4c186d5ba27fe300/exercise.xlsx';

The SAS/ACCESS LIBNAME statement creates the libref Certxl, which points to the Excel workbook
exercise.xlsx. The workbook contains 3 worksheets, which are now available in the new SAS library as
data sets.swer: B

Missing data
Numeric missing is presented as a period (.); character missing is presented as blank
Missing data is less than any other valid values
SAS will treat as missing numeric value if it encounters:

1. an invalid numeric data (for example, letters, see the following example)
2. division by zero (5/0 = missing)
3. performing an operation on missing value (1+missing = missing)

3
SAS date - a special numeric data
SAS date value is a value that represents the number of days between January 1, 1960, and a
specified date.

SAS date constant


“ddMMMyyyy”d
Example: “02Jan1 0”d 1, “01Jan1 1”d
Date related functions

Function Usage Result Remark

MDY MDY(2,5,2017) 20855 Results are all numeric, character can be used in
MDY(‘01’,’01’,1 0) 0 the input, but will be converted into numeric
Day Day(‘0 Feb201 ’d) 5 automatically. 05FEB2017 is 20855 and Sunday
Month Month(‘0 Feb201 ’d) 2
Year Year(20855) 2017
Weekday Weekday(20855) 1
Qtr Qtr(20855) 1
Time Time() Current # of seconds from 00:00:00
time
Date/today Date() or today() Current # of days from 1960/01/01
date
Datetime Datetime() Current # of seconds from 1960/01/01 00:00:00
date and
time

Example4
The following SAS program is submitted:

data revenue;
Var1 = mdy(1,15,1960);
run;

What is the value of the variable var1 ?

A. 15
B. 14
C. ‘1/1 /1 0’
D. 1151960
Answer: C

4
Example5
The following SAS program is submitted:
data newstaff;

set staff;

<insert WHERE statement here>

run;

Which one of the following WHERE statements complete the program and selects only observations
with a numeric variable Hire_date of February 23, 2000?

a. where hire_date='23feb2000'd;

b. where hire_date='23feb2000';

c. where hire_date='02/23/2000'd;

d. where hire_date='02/23/2000';

Answer: C

You might also like