SAS - SQL


SAS offers extensive support to most of the popular relational databases by using SQL queries inside SAS programs. Most of the ANSI SQL syntax is supported. The procedure PROC SQL is used to process the SQL statements. This procedure can not only give back the result of an SQL query, it can also create SAS tables & variables. The example of all these scenarios is described below.

Syntax

The basic syntax for using PROC SQL in SAS is −

PROC SQL;
SELECT Columns
FROM TABLE
WHERE Columns
GROUP BY Columns
;
QUIT;

Following is the description of the parameters used −

  • The SQL query is written below the PROC SQL statement followed by the QUIT statement.

Below we will see how this SAS procedure can be used for the CRUD (Create, Read, Update and Delete)operations in SQL.

SQL Create Operation

Using SQL we can create new data set form raw data. In the below example, first we declare a data set named TEMP containing the raw data. Then we write a SQL query to create a table from the variables of this data set.

DATA TEMP;
INPUT ID $ NAME $ SALARY DEPARTMENT $;
DATALINES;
1 Rick 623.3 IT
2 Dan 515.2 Operations
3 Michelle 611 IT
4 Ryan 729 HR
5 Gary 843.25 Finance
6 Nina 578 IT
7 Simon 632.8 Operations
8 Guru 722.5 Finance
;
RUN;

PROC SQL;
CREATE TABLE EMPLOYEES AS
SELECT * FROM TEMP;
QUIT;

PROC PRINT data = EMPLOYEES;
RUN;

When the above code is executed we get the following result −

SQL Create Table

SQL Read Operation

The Read operation in SQL involves writing SQL SELECT queries to read the data from the tables. In The below program queries the SAS data set named CARS available in the library SASHELP. The query fetches some of the columns of the data set.

PROC SQL;
SELECT make,model,type,invoice,horsepower
FROM 
SASHELP.CARS
;
QUIT;

When the above code is executed we get the following result −

SQL1

SQL SELECT with WHERE Clause

The below program queries the CARS data set with a where clause. In the result we get only the observation which have make as 'Audi' and type as 'Sports'.

PROC SQL;
SELECT make,model,type,invoice,horsepower
FROM 
SASHELP.CARS
Where make = 'Audi'
and Type = 'Sports'
;
QUIT;

When the above code is executed we get the following result −

SQL Where Clause

SQL UPDATE Operation

We can update the SAS table using the SQL Update statement. Below we first create a new table named EMPLOYEES2 and then update it using the SQL UPDATE statement.

DATA TEMP;
INPUT ID $ NAME $ SALARY DEPARTMENT $;
DATALINES;
1 Rick 623.3 IT
2 Dan 515.2 Operations
3 Michelle 611 IT
4 Ryan 729 HR
5 Gary 843.25 Finance
6 Nina 578 IT
7 Simon 632.8 Operations
8 Guru 722.5 Finance
;
RUN;

PROC SQL;
CREATE TABLE EMPLOYEES2 AS
SELECT ID as EMPID,
Name as EMPNAME ,
SALARY as SALARY,
DEPARTMENT as DEPT,
SALARY*0.23 as COMMISION
FROM TEMP;
QUIT;

PROC SQL;
UPDATE EMPLOYEES2
      SET SALARY = SALARY*1.25;
   QUIT;
      PROC PRINT data = EMPLOYEES2;
RUN;

When the above code is executed we get the following result −

Proc sql Update

SQL DELETE Operation

The delete operation in SQL involves removing certain values from the table using the SQL DELETE statement. We continue to use the data from the above example and delete the rows from the table in which the salary of the employees is greater than 900.

PROC SQL;
DELETE FROM EMPLOYEES2
   WHERE SALARY > 900;
QUIT;
   PROC PRINT data = EMPLOYEES2;
RUN;

When the above code is executed we get the following result −

Proc SQL Delete
Advertisements