This post was kindly contributed by The SAS Dummy - go there to comment and to read the full post. |
When you weren’t watching, SAS did it again. We smuggled Yet Another Excel Engine into a SAS release.
SAS 9.2 Maintenance 2 added the XLSX engine, which allows you to read and write Microsoft Excel files as if they were data sets in a library. The big advantage of using this engine is that it accesses the XLSX file directly, and doesn’t use the Microsoft data APIs as a go-between. (LIBNAME EXCEL and LIBNAME PCFILES rely on those Microsoft components.) That means that you can use this engine on Windows or Unix systems without having to worry about bitness (32-bit versus 64-bit) or setting up a separate PC Files Server process.
The XLSX engine does require a license for SAS/ACCESS to PC Files. Are you a SAS University Edition user? The SAS/ACCESS product is part of that package, so this technique works there. It’s an easy way to get well-formed Excel data into your SAS process.
/* because Excel field names often have spaces */ options validvarname=any; libname xl XLSX '/folders/myfolders/sas_tech_talks_15.xlsx'; /* discover member (DATA) names */ proc datasets lib=xl; quit; libname xl CLEAR; |
Example output:
Once the library is assigned, I can read the contents of a spreadsheet into a new SAS data set:
/* because Excel field names often have spaces */ options validvarname=any; libname xl XLSX '/folders/myfolders/sas_tech_talks_15.xlsx'; /* read in one of the tables */ data confirmed; set xl.confirmed; run; libname xl CLEAR; |
And here’s the result in my SAS University Edition:
Sometimes you need just one value from a spreadsheet. That’s a common use case for dynamic data exchange (DDE), which isn’t as feasible as it once was. You can use FIRSTOBS and OBS options to control how much data you retain:
/* read in just one value */ data _null_; set xl.confirmed (firstobs=6 obs=6 keep='Job title'n); call symput('VALUE','Job Title'n); run; %put &value; |
Output:
76 %put &value; Testing Manager, Quality-driven User Experience Testing
You can also use the XLSX engine to create and update XLSX files.
libname xlout XLSX '/folders/myfolders/samples.xlsx'; data xlout.cars; set sashelp.cars; run; data xlout.classfit; set sashelp.classfit; run; data xlout.baseball; set sashelp.baseball; run; data xlout.air; set sashelp.air; run; libname xlout clear; |
Here is my output in Microsoft Excel with all of these data sets now as sheets:
Remember, you can also create Microsoft Excel files with Base SAS by using ODS EXCEL — experimental in 9.4 Maintenance 2 but production in Maintenance 3 (coming soon).
The XLSX libname is different from the EXCEL and PCFILES engines in other ways. For example, the XLSX engine does not support Excel named ranges (which can surface a portion of a spreadsheet as a discrete table). Also, you won’t see the familiar “$” decoration around the spreadsheet names when they are surfaced in the library within SAS. If you need that sort of flexibility, you can use PROC IMPORT to provide more control over exactly what Excel content is brought into SAS and how.
One other IMPORTANT caution: The XLSX engine is a sequential access engine in that it processes data one record after the other. The engine starts at the beginning of the file and continues in sequence to the end of the file. Some techniques to MODIFY the data in-place will not work. Also, some SAS data viewers cannot render the data from the XLSX engine. SAS VIEWTABLE and SAS Enterprise Guide and even SAS Studio can’t open these tables directly in the data grid view. VIEWTABLE gives you a nice message, but SAS Enterprise Guide simply “hangs” in the attempt. For that reason, I recommend using DATA step to copy the Excel content that you want to another SAS library, then CLEAR the XLSX library to avoid accidentally opening a table in a viewer that won’t support it. (This is currently a bug in SAS Enterprise Guide that should be fixed in a future release.)
I have found LIBNAME XLSX to be a quick, convenient method to bring in Excel data on any SAS platform. If you have SAS 9.4 Maintenance 2 or later, try it out! Let me know how it works for you by sharing a comment here.
The post Using LIBNAME XLSX to read and write Excel files appeared first on The SAS Dummy.
This post was kindly contributed by The SAS Dummy - go there to comment and to read the full post. |