Published on :

Using SAS Engines to Process External Data

This code is also available in: Deutsch Español Français
Awaiting validation
Detailed functional analysis of different approaches for importing and exporting external data. It covers the use of PROC IMPORT for CSV and Excel files, creating tables in a DBMS via a DATA step, integrating LIBNAME statements into PROC SQL views, leveraging the SQL Pass-Through function, as well as importing XML and JSON data using the XMLV2 and JSON engines, respectively. Each example is designed to be self-contained, providing the means to acquire or create the necessary data.
Data Analysis

Type : MIXED


Examples use external data (CSV, Excel, XML, JSON) whose acquisition is explained, or internally created data (datalines) for DBMS demonstrations.

1 Code Block
PROC HTTP / PROC IMPORT
Explanation :
This example uses the FILENAME statement to assign the 'chol' fileref to a temporary file. The HTTP procedure is then used to download a CSV file from a specified URL and save it to the 'chol' fileref. The IMPORT procedure reads the CSV data and creates the SAS dataset 'work.mycholesterol'. Finally, PROC PRINT displays the content of the dataset to verify the import.
Copied!
1filename chol temp;
2PROC HTTP
3 url="http://support.sas.com/documentation/onlinedoc/viya/exampledatasets/cholesterol.csv"
4 out=chol;
5QUIT;
6PROC IMPORT datafile=chol
7 out=work.mycholesterol
8 dbms=csv
9 replace;
10RUN;
11PROC PRINT DATA=work.mycholesterol;
12RUN;
2 Code Block
PROC IMPORT
Explanation :
This example imports data from a Microsoft Excel (.xlsx) file using the IMPORT procedure and the XLSX engine. For this example to be self-contained, you must first create an Excel file named 'cholesterol.xlsx' (e.g., by saving the 'cholesterol.csv' file from the previous example in .xlsx format). The 'VALIDVARNAME=V7' option is used to convert Excel column names into valid SAS variable names (replacing spaces with underscores). The XLSX engine requires the SAS/ACCESS to PC Files interface to be configured. The REPLACE option allows replacing an existing SAS dataset.
Copied!
1options validvarname=v7;
2PROC IMPORT datafile='cholesterol.xlsx'
3 dbms=xlsx
4 out=work.mycholesterol
5 replace;
6RUN;
3 Code Block
LIBNAME / DATA STEP Data
Explanation :
This example shows how to create a table directly in an external database management system (DBMS), here Teradata. The LIBNAME statement 'mytddata' is defined to establish a connection to the Teradata server, including authentication information. A DATA step is then used to create a table named 'grades' in this DBMS using 'datalines' to provide the data. The DATASETS procedure is used to display information about the 'mytddata' library, confirming that the table has been created in the DBMS.
Copied!
1LIBNAME mytddata teradata server=mytera user=myid password=mypw;
2DATA mytddata.grades;
3 INPUT student $ test1 test2 final;
4 DATALINES;
5Fred 66 80 70
6Wilma 97 91 98
7;
8PROC DATASETS library=mytddata;
9RUN;
10QUIT;
4 Code Block
PROC SQL
Explanation :
This example illustrates the creation of a SAS view (mygrades in the viewlib library) that embeds a LIBNAME statement for a connection to a Teradata database. The libref 'viewlib' is assigned to the location where the SAS view will be stored, using the V9 engine. The 'USING' clause of the 'CREATE VIEW' statement allows embedding the DBMS connection details, making the view self-sufficient for accessing the 'mytddata.grades' table. Finally, PROC PRINT executes this view and displays its content.
Copied!
1LIBNAME viewlib v9 'library-path';
2PROC SQL;
3 create view viewlib.mygrades as
4 select *
5 from mytddata.grades
6 using LIBNAME mytddata teradata
7 server=mytera
8 user=myid password=mypw;
9QUIT;
10PROC PRINT DATA=viewlib.mygrades noobs;
11RUN;
5 Code Block
PROC SQL
Explanation :
This example uses the SQL Pass-Through facility within PROC SQL to send SQL queries directly to an external database (Teradata). The 'CONNECT TO' statement establishes a named connection 'myconn' to the Teradata server with the specified credentials. Then, a 'SELECT * FROM CONNECTION TO myconn (...)' clause allows executing a native SQL query (filtering 'grades' with 'final gt 90') directly on the DBMS. The 'DISCONNECT FROM' statement is used to terminate the database connection after the query execution.
Copied!
1PROC SQL;
2 connect to teradata as myconn (server=mytera
3 user=myid password=mypw);
4 select *
5 from connection to myconn
6 (select *
7 from grades
8 where final gt 90);
9 disconnect from myconn;
10QUIT;
6 Code Block
LIBNAME / PROC PRINT
Explanation :
This example imports data from an XML file using the XMLV2 engine. To make this example self-contained, create a file named 'nhl.xml' with the following XML content, and replace 'file-path' with the actual path:
```xml
<?xml version="1.0" encoding="iso-8859-1" ?>
<NHL>
<CONFERENCE> Eastern
<DIVISION> Southeast
<TEAM name="Thrashers" abbrev="ATL" />
<TEAM name="Hurricanes" abbrev="CAR" />
<TEAM name="Panthers" abbrev="FLA" />
<TEAM name="Lightning" abbrev="TB" />
<TEAM name="Capitals" abbrev="WSH" />
</DIVISION>
</CONFERENCE>

<CONFERENCE> Western
<DIVISION> Pacific
<TEAM name="Stars" abbrev="DAL" />
<TEAM name="Kings" abbrev="LA" />
<TEAM name="Ducks" abbrev="ANA" />
<TEAM name="Coyotes" abbrev="PHX" />
<TEAM name="Sharks" abbrev="SJ" />
</DIVISION>
</CONFERENCE>
</NHL>
```
The FILENAME statement assigns the 'nhl' fileref to the XML file. A second FILENAME statement assigns the 'map' fileref to a file that will store the generated XMLMap. The LIBNAME statement, with the XMLV2 engine and options 'AUTOMAP=REPLACE' and 'XMLMAP=map', reads the XML file and generates an XMLMap. PROC PRINT then displays the 'TEAM_name' and 'TEAM_abbrev' variables from the 'nhl.team' dataset created in memory.
Copied!
1filename nhl 'file-path/nhl.xml';
2filename map 'file-path/nhlgenerate.map';
3LIBNAME nhl xmlv2 automap=replace xmlmap=map;
4PROC PRINT DATA=nhl.team noobs;
5 var TEAM_name TEAM_abbrev;
6RUN;
7 Code Block
LIBNAME / PROC DATASETS
Explanation :
This example imports JSON data using the JSON engine. To make this example self-contained, create a file named 'example.json' with valid JSON content (e.g., a simple JSON object with a few fields and values) and replace 'file-path' with the actual path. The LIBNAME statement 'mydata' is assigned to the JSON engine and points to the 'example.json' file. The DATASETS procedure is then used to list the SAS datasets created in memory from this JSON file, thereby verifying the import.
Copied!
1LIBNAME mydata json '/file-path/example.json';
2PROC DATASETS lib=mydata;
3RUN;
4QUIT;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Copyright © SAS Institute Inc. All Rights Reserved. Last updated: December 16, 2025


Related Documentation : Data Access

Sujet / Mot-cléLien vers la ressource
DOC FedSQL en/sampleCode/FEDSQL9D66