- SAS - Dates & Times
- SAS - Macros
- SAS - Input Methods
- SAS - Functions
- SAS - Decision Making
- SAS - Loops
- SAS - Operators
- SAS - Numeric Formats
- SAS - Arrays
- SAS - Strings
- SAS - Variables
- SAS - Data Sets
- SAS - Basic Syntax
- SAS - Program Structure
- SAS - User Interface
- SAS - Environment
- SAS - Overview
- SAS - Home
SAS Data Set Operations
- SAS - Simulations
- SAS - Output Delivery System
- SAS - SQL
- SAS - Format Data Sets
- SAS - Sort Data Sets
- SAS - Subsetting Data Sets
- SAS - Merging Data Sets
- SAS - Concatenate Data Sets
- SAS - Write Data Sets
- SAS - Read Raw Data
SAS Data Representation
SAS Basic Statistical Procedure
- SAS - Hypothesis Testing
- SAS - One-Way Anova
- SAS - Repeated Measure Analysis
- SAS - Fishers Exact Tests
- SAS - Chi-Square
- SAS - Bland-Altman Analysis
- SAS - Linear Regression
- SAS - Correlation Analysis
- SAS - T Tests
- SAS - Cross Tabulations
- SAS - Frequency Distributions
- SAS - Standard Deviation
- SAS - Arithmetic Mean
SAS Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
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 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 pbrary 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 −
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 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 −
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 −
Advertisements