how to import an excel file into sas
TRANSCRIPT
-
8/12/2019 How to Import an Excel File Into SAS
1/8
How to Import an Excel File into SAS
Here are the steps to import an Excel file.
1. Examine the content of your Excel file.
A few guidelines:
o It is ideal for the first row to contain the column header because by
default this will be used as the variable names...
o ... and that the data values begin in the second row.
o As much as possible, the variable names (column header) must
conform to the variable naming rules. The rules are:
Variable names must begin with a letter or underscore. The rest can be any
letter, number or underscore. They can also be up to 32 characters long.
Please note, however, that SAS will automatically convert invalid characters in
the variable names (column header) into an underscore when SAS imports the
Excel file into a SAS data set.
o As much as possible, the values of a variable must be of the same
type. SAS uses the first 8 observations to determine the TYPEto
assign to the imported column(s). In case of mixed-types, the type
that is the most dominant in the first 8 observations will be applied.
In case of a tie i.e., four (4) character and four (4) numeric
variables, the numeric type takes precedence. Once imported, values
that do not conform to the assigned type will be converted to missing
values.
o Make sure that rows immediately following the last observation and
columns immediately following the last variable are not activated bydeleting them so that SAS will not mistake them for additional
observations or additional columns, respectively.
-
8/12/2019 How to Import an Excel File Into SAS
2/8
In Example 1:
The first row of data is the column
header. (recommended!)
The second row of data is the start of the data
values. (recommended!)
The column names AGE OF RESPONDENT andSEX:CHAR FORMAT do not conform to the variable
naming rules because of the presence of invalidcharacters - spaces in the former, and colon and space
in the latter. (Not recommended!) You can rename thismanually or let SAS do the renaming automatically. Ifyou let SAS rename the variables automatically, theformer variable will be renamed in the resulting SASdataset as AGE_OF_RESPONDENT and the latter as
SEX_CHAR_FORMAT.
-
8/12/2019 How to Import an Excel File Into SAS
3/8
The data values in each column are consistent in
type. (recommended!)
Rows immediately following the last observation, and
columns immediately following the last variable are notactivated. (recommended!)
Example 2
Example 2 is similar to Example 1 except that thesecond and fourth columns have mixed-type
values. (Not recommended!)
Remember that SAS uses the first 8 observations todetermine the TYPEto assign to the importedcolumn(s). In case of mixed-types, the type that is the
most dominant in the first 8 observations will beapplied. In case of a tie i.e., four (4) character and
four (4) numeric variables, the numeric type takesprecedence.
-
8/12/2019 How to Import an Excel File Into SAS
4/8
Once imported, values that do not conform to the
assigned type will be converted to missing values.
So..
When SAS imports column 2, because there is a tie in
the first 8 observations i.e, four character values andfour numeric values, SAS assigns a numeric type forthis column. The character values will be converted tomissing values.
When SAS imports column 4, because there are morecharacter than numeric values in the first 8
observations, SAS assigns a character type for thiscolumn. The numeric values will be converted to
missing values.
2. Close the Excel File you want to import to avoid a file sharing violation!
3. Open SAS, then clickFile > Import Data(this opens the Import Wizard).
4. The default type is Microsoft Excel, accept the default by clickingNext. This
opens the Connect to MS Exceldialog box.
-
8/12/2019 How to Import an Excel File Into SAS
5/8
5. Click Browse, then locate the Excel fileyou want to import, then
click Open. Suppose we want to import the excel file DEMO1.XLSlocated in
the C:\SASWORKSHOPfolder.
6. Click OK(This opens the Select Table dialog box).
7. Select the table you want to import.
Note: If your Excel file has multiple tables (or worksheets), click the pull-
down (see red arrow above) menu to see the list of worksheets in that Excel
file.
8. Click Options(this opens the Spreadsheet Options dialog box).
-
8/12/2019 How to Import an Excel File Into SAS
6/8
9. Check/Uncheckappropriate boxesin the Spreadsheet Options dialog
box , or accept the default, then click OK.
10.Click Next(this opens the Select Library and Member dialog box).
11.Click the Librarypull-down menu(see red arrow) to select the library
where you want to store the SAS data set to be created. Then enter
the nameof the SAS data set to be created (eg., demo1) in theMember
box(see pink arrow).
-
8/12/2019 How to Import an Excel File Into SAS
7/8
12.Click Next(this opens the Create SAS Statements dialog box).
Note 1: PROC IMPORT is the SAS procedure used to
import your Excel file into SAS. Click Browseto create aSAS program file that contains the PROC IMPORTstatements used to import your Excel file. When you clickBrowse, you will be asked to specifythe nameand locationof the SAS program file. (Bydefault, SAS program files have a .SAS extension.) Thenclick Save.
Note 2: If you want to import multiple tables from the
same Excel file, saving the program file maybe the best
-
8/12/2019 How to Import an Excel File Into SAS
8/8
option for you. Because you can open this program file inthe Enhanced Editor, copy and paste the PROC IMPORTstatement and then simply replace the name of the tablein the SHEET =statement one by one with the names of
the tables you want to import.
13.Click Finish.