Retrieve column(s) from table
SELECT name
FROM school;
Proc SQL is often used to retrieve information from a SAS table.
Let's take a look at an example.
Copy and run the code from the yellow box below to get the SCHOOL table.
The SCHOOL table contains a list of students and their respective age, gender, school and height.
Now, we will retrieve the list of student names from the table using Proc SQL.
Example
Proc sql;
select name
from school;
quit;
Proc SQL is fairly simple and straightforward.
In order to retrieve the NAME column from the SCHOOL table, you simply tell SAS to select the NAME column from the SCHOOL table.
SAS will then display the list of student names in the output:
(Try it in your SAS Studio!)
SELECT Statement
Let's go back to the code for a moment.
The Proc SQL step in our example contains a SELECT statement:
The SELECT statement has two clauses:
The SELECT clause selects the NAME column from the SAS table.
The FROM clause specifies the SCHOOL table as the source of the data.
Unlike data step, the SQL procedure ends with the QUIT statement, as opposed to the RUN statement.
Together with the SELECT clause and FROM clause, you can retrieve any columns from a SAS table.
Exercise
Locate the CARS table from the SASHelp library.
Write a Proc SQL step to retrieve the list of car models from the CARS table.
The car models are captured in the MODEL column.
Need some help?
HINT:
The FROM clause is part of the SELECT statement. You do not need a semi-colon between the SELECT clause and the FROM clause.
SOLUTION:
proc sql;
select model
from sashelp.cars;
quit;
Cookie | Duration | Description |
---|---|---|
cookielawinfo-checkbox-analytics | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics". |
cookielawinfo-checkbox-functional | 11 months | The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". |
cookielawinfo-checkbox-necessary | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary". |
cookielawinfo-checkbox-others | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other. |
cookielawinfo-checkbox-performance | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance". |
viewed_cookie_policy | 11 months | The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data. |