Read External Data Into SAS: 1. The IMPORT Procedure
Read External Data Into SAS: 1. The IMPORT Procedure
Read 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.
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
Example2
Given the following SAS program:
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?
To read the Excel workbook file, SAS must receive the following information in the DATA step:
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.
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;
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;
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