{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to PROC SQL\n", "\n", "PROC SQL is a powerful tool for data manipulation and querying. It can perform many functions such as the conventional DATA and PROC steps but with fewer statements and computer resources. In this lesson, we will investigate how to select, subset, sort, summarize and group data with SQL procedure.\n", "\n", "## PROC SQL Basics\n", "\n", "PROC SQL is a procedure that SAS developed for the implementation of Structured Query Language. You can use this procedure to modify, retrieve and report data in tables and views (created on tables). Just as with other SAS procedures, PROC SQL also has basic syntax structures. It takes the following general form:\n", "\n", "
\n",
"PROC SQL;\n",
"SELECT column-1<,…column-n>\n",
"FROM table-1|view-1<,…table-n|view-n>\n",
"<WHERE expression>\n",
"<GROUP BY column-1<,…column-n>>\n",
"<HAVING expression >\n",
"<ORDER BY column-1<,…column-n >>;\n",
"QUIT;\n",
"\n",
"
\n",
"\n",
"First of all, you may see differences in terminology between SQL and other SAS steps. For example, the data file is called data set in other SAS steps, but table in SQL. Correspondingly, records are called observations in the previous lessons, but rows in SQL tables; and we call a field of data set as variable, but column in this lesson.\n",
"\n",
"Other SAS steps | \n", "\t\t\tSQL Procedure | \n", "\t\t
data set | \n", "\t\t\ttable | \n", "\t\t
observation | \n", "\t\t\trow | \n", "\t\t
variable | \n", "\t\t\tcolumn | \n", "\t\t
The following SAS SQL code is just query that retrieves data from a single table:
\n", "To run the program above, you will need to save the SAS data file (survey.sas7bdat) to your computer first (see the data folder on the course website). Edit LIBNAME statement to reflect the directory in which you saved the survey data set. Then run the program and check the output.
\n", "The SQL procedure in this code represents the most basic form of the procedure. Like other SAS procedures, you need to run PROC SQL at the beginning to invoke it. Inside the procedure, there is only one statement starting with SELECT, which chooses the columns you want. You can list as many columns as needed, separated by commas. Another clause is FROM, which is used to specify the table(s). PROC SQL follows the same protocol of SAS file names. Here we used a two-level name to reference the permanent file. Just as you read the code, this program is used to select three columns (student id, SAT Math score and SAS Verbal score) from the table.
\n", "The following SAS program uses CREATE TABLE statement to create a new table named SAT_scores, which contains student id, SAT math scores and verbal scores.
\n", "Launch and run the SAS program. You may notice that there is no output displayed in the SAS output window or any open ODS destination. That’s because the CREATE TABLE statement suppresses the printed output of the query. However, if you check the SAS log window, it shows a message that indicates that the table has been created, and the number of rows and columns in the table (see output above). In this example, table SAT_scores has 226 rows and 3 columns. And the new table’s columns have the same attributes (type, length, format, label) as those of the selected source columns.
\n", "From these two examples, you now have some idea about what PROC SQL is like to work with. Let’s summarize what makes it so unique from other SAS procedures.
\n", "The following SAS program creates a new temporary table with all columns retrieved from permanent file traffic.sas7bdat (see the data folder on the course website):
\n", "First, you need to download the permanent SAS data file traffic to your own computer. Revise the libname statement as needed. Then run the program.
\n", "One thing you need to know about this program is the shortcut, noted with an asterisk (*) after SELECT. The asterisk refers to all columns in the original table. So, this code is to select all columns in the permanent file into the temporary file, traffic.
\n", "To check the data, you may use the other procedures we learned in previous lessons, such as the PRINT procedure. In the above program, PROC CONTENTS has been used to check the variable attributes in the original and the new table. As we mentioned in the previous section, the variables chosen from other table(s) keep the same attributes.
\n", "Besides selecting original columns, the SELECT clause can also be used to create new columns, just as we used assignment statements in DATA step to create new variables.
\n", "The following program is to create new columns with the SELECT statement:
\n", "As you can see, this code uses the traffic table we created previously. Using the SELECT statement you can create new columns that contain either characters or numbers. With valid statements within the SELECT clause, you can use any expression for new columns. And, the new columns can be named by using the keyword AS followed by the names you would like to use. (Column names also follow the rules for SAS names.) In the above code, the first new column is created by a character function scan(), which substring is the orientation information from the existing column, count_location. The name for this new column is orientation after AS. (It may make no sense, just for the use of example.) The second new column is a math expression that estimates the traffic volume during weekends by multiplying daily vehicle volume by 0.5. Its alias is weekends_traffic_volume
.
Launch and run the SAS program, and review the output to convince yourself that SAS does indeed create two new columns as you expect. But you should note that new columns only exist during the query, unless you created a table out of it.
\n", "While observing the data in traffic, you may notice that some data are not formatted as you want. Fortunately, SAS provides many options in SELECT statement so you can enhance the appearance of the query output.
\n", "The following program adds the format to dates, labels columns and add titles to the output:
\n", "Launch and run the SAS program and then review the resulting output to convince yourself that the data has been formatted and labeled as you expect. Except for titles, you can also add a footnote to the output using footnote statement. But unlike using title and footnote statements with other SAS steps, both statements have to be placed either before the PROC SQL statement, or between the PROC SQL statement and SELECT statement.
\n", "One more thing we will talk about in this section is the CASE operator, which just follows the SELECT clause to create new columns conditionally. You must remember that this applies only to IF-THEN-ELSE statements that are available in DATA step. In PROC SQL, the CASE operator can perform the equivalent functions. First, let’s look at the syntax for the CASE construct.
\n", "\n",
"CASE\n",
"WHEN when-condition THEN result-expression\n",
"<… WHEN when-condition THEN\n",
"result-expression>\n",
"<ELSE result-expression>\n",
"END AS < column name>\n",
"
\n",
" As in IF-THEN statements, you can add as many WHEN conditions as you want. The conditions can be any valid SAS expression, including calculations, functions, and logical operators. It works as IF-THEN statements, too. If the conditions have been met, SAS will carry out the corresponding actions following the keyword THEN. If the WHEN condition is false, then PROC SQL executes the ELSE expression. You can create a new column and name it with AS keywords after END. The ELSE and AS keywords are optional. But it’s good practice to keep original columns while creating new ones.
\n", "The following SAS program uses CASE operator to assign different salary raise plans for each salary range:
\n", "You already know format and label options from the previous explanations. There are a couple of new things in this example, however. First, you can insert a character(or numeric) constant as a new column in the table. Here a character string “next year raise” has been added between salary and raise. Raise is also a new column which has been created by the CASE operator based on the current annual salary of each person.
\n", "Download the SAS data set salary.sas7bdat (see the data folder on the course website) on your computer and revise the libname statement to reflect the directory where you save the file. Then launch and run the program. Review the query result to convince yourself that the raise values have been assigned correctly.
\n", "The CASE operator has two forms of syntax. In fact, if you use only one column for WHEN condition(s), this column’s name can be put after CASE and before WHEN. So you don’t have to repeat the column’s name in each WHEN condition. Below is the syntax for this form:
\n", "\n",
"CASE <column-name>\n",
"when-condition THEN result-expression\n",
"<… WHEN when-condition THEN\n",
"result-expression>\n",
"<ELSE result-expression>\n",
"END AS < column name>\n",
"
\n",
"The following program uses the simpler form of CASE construct to decide compensation (Yes or N/A) based on departments:
\n", "Name | \n", "Department | \n", "salary | \n", "Compensation | \n", "
---|---|---|---|
WARNER | \n", "PROCUREMENT | \n", "$76,980.00 | \n", "N/A | \n", "
EDWARDS | \n", "POLICE | \n", "$83,616.00 | \n", "Yes | \n", "
PENDARVIS | \n", "POLICE | \n", "$103,590.00 | \n", "Yes | \n", "
CLARK | \n", "FIRE | \n", "$85,680.00 | \n", "Yes | \n", "
FOLINO | \n", "POLICE | \n", "$83,616.00 | \n", "Yes | \n", "
SAWYER | \n", "CITY COUNCIL | \n", "$117,333.00 | \n", "N/A | \n", "
WALTON | \n", "POLICE | \n", "$89,718.00 | \n", "Yes | \n", "
QUINLAN | \n", "TRANSPORTN | \n", "$95,888.04 | \n", "N/A | \n", "
SOTO | \n", "WATER MGMNT | \n", "$79,040.00 | \n", "N/A | \n", "
RUSS | \n", "WATER MGMNT | \n", "$79,040.00 | \n", "N/A | \n", "
MC GUIRE | \n", "POLICE | \n", "$86,520.00 | \n", "Yes | \n", "
GAWRISCH | \n", "POLICE | \n", "$86,520.00 | \n", "Yes | \n", "
CANO | \n", "FIRE | \n", "$86,520.00 | \n", "Yes | \n", "
CLARK | \n", "STREETS & SAN | \n", "$72,862.40 | \n", "N/A | \n", "
GIBOWICZ | \n", "POLICE | \n", "$83,616.00 | \n", "Yes | \n", "
MANCILLA | \n", "POLICE | \n", "$90,456.00 | \n", "Yes | \n", "
WYATT | \n", "WATER MGMNT | \n", "$65,686.40 | \n", "N/A | \n", "
VALLE | \n", "FIRE | \n", "$54,114.00 | \n", "Yes | \n", "
SISSAC | \n", "PUBLIC LIBRARY | \n", "$12,407.20 | \n", "N/A | \n", "
ARMSTEAD | \n", "POLICE | \n", "$80,778.00 | \n", "Yes | \n", "
The above code uses the same data set as the previous example, salary. It assigns the different compensation plans based on which department people work for and creates a new column, Compensation, for the result. This time, the column name Department has been put outside the WHEN conditions and into CASE operator. So we don’t need coding like “WHEN department=’POLICE’” any more.
\n", "Another feature is the option you can use in the PROC SQL statement, OUTOBS=n. It can be used to limit the number of rows displayed in the output. So in this case, we would expect the table in the output window shows the first 20 rows of the data. And such a warning message will be delivered in the log file.
\n", "WARNING: Statement terminated early due to OUTBOS=20 option.
\n",
" Note that OUTOBS= will also affect tables that are created by the CREATE TABLE statement.
\n", "Launch and run the program. Then check the query result to make sure the records have been processed as expected. Note that you have to be cautious with this simpler form. For instance, if you move Employee_annual_salary out of the WHEN conditions in the program of the previous example, SAS will report an error and not execute!
\n", "The following example uses the WHERE clause to select employees who work at a police department and have the job title as sergeant:
\n", "Name | \n", "Department | \n", "Employee Annual Salary | \n", "
---|---|---|
PENDARVIS | \n", "POLICE | \n", "103590 | \n", "
ODUM | \n", "POLICE | \n", "106920 | \n", "
DANIELS | \n", "POLICE | \n", "106920 | \n", "
TATE JR | \n", "POLICE | \n", "103590 | \n", "
GOLDSMITH | \n", "POLICE | \n", "110370 | \n", "
FRANKO | \n", "POLICE | \n", "106920 | \n", "
RADDATZ | \n", "POLICE | \n", "110370 | \n", "
FLEMING | \n", "POLICE | \n", "100440 | \n", "
CASEY | \n", "POLICE | \n", "110370 | \n", "
MC COY | \n", "POLICE | \n", "110370 | \n", "
JACOBS | \n", "POLICE | \n", "106920 | \n", "
Reading through the program, you must have known that it selects the name, department and annual salary information from salary data for police sergeants. Note that the columns in the WHERE clause do not have to be specified in the SELECT clause, (such as Position title), which is used in the WHERE clause but not in the SELECT clause. However, for the sake of the results checking, I would suggest to keep these columns in the query until verified.
\n", "Launch and run the SAS program, and review the output to convince yourself that the records have been selected as described.
\n", "We saw two types of operators used in the above program, the comparison (=) and the logical (and). Besides these common ones, another type that could be very useful in your programming is called a conditional operator. You may know some of them already, like IN, CONTAINS and MISSING. You can find the complete list of operators in the SAS documentation. Next, let’s look at a couple of examples on this using BETWEEN AND and LIKE.
\n", "BETWEEN value-1 AND value-2
\n",
" Both value-1 and value-2 are end values. So you can use the BETWEEN AND operator to specify a range of values, such as from one date to another, or from lower limit to upper limit. The smaller value does not have to be the first.
\n", "The following program uses the operator, BETWEEN AND, to select observations from salary data whose annual salary is between \\$65,000 and \\$70,000, and also works in Fire department:
\n", "Name | \n", "Department | \n", "Salary | \n", "
---|---|---|
KELLY | \n", "FIRE | \n", "$65,946.00 | \n", "
KOCHANEY | \n", "FIRE | \n", "$65,946.00 | \n", "
Launch and run the SAS program, and review the query output to convince yourself that the SAS yield the result as expected.
\n", "Patterns | \n", "\t\t\tResults | \n", "\t\t
Kath_ | \n", "\t\t\tKathy | \n", "\t\t
Kath__ | \n", "\t\t\tKathie | \n", "\t\t
Kath% | \n", "\t\t\tKathy, Kathie, Katherine | \n", "\t\t
_ath% | \n", "\t\t\tAll of the names above | \n", "\t\t
The following program shows the use of the LIKE operator in a WHERE clause to select name, department, position title and annual salary information for people whose name starts with R and the third letter is B:
\n", "Name | \n", "Department | \n", "Position Title | \n", "Salary | \n", "
---|---|---|---|
ROBINSON | \n", "WATER MGMNT | \n", "OPERATING ENGINEER-GROUP C | \n", "$93,745.60 | \n", "
RABANALES | \n", "FINANCE | \n", "AUDITOR II | \n", "$87,912.00 | \n", "
ROBERTS | \n", "FIRE | \n", "PARAMEDIC I/C | \n", "$79,404.00 | \n", "
ROBINSON | \n", "WATER MGMNT | \n", "CONSTRUCTION LABORER | \n", "$79,040.00 | \n", "
Launch and run the SAS program, and review the query output to convince yourself that the SAS behaves as described.
\n", "The following program attempts to calculate the bonus for every employee, then select ones who has more than \\$2,000 as bonus:
\n", "176 ods listing close;ods html5 (id=saspy_internal) file=stdout options(bitmap_mode='inline') device=svg style=HTMLBlue; ods
176! graphics on / outputfmt=png;
NOTE: Writing HTML5(SASPY_INTERNAL) Body file: STDOUT
177
178 PROC SQL;
179 select Name, Department,
180 Employee_annual_salary label='Salary' format=DOLLAR12.2,
181 Employee_annual_salary * 0.02 as Bonus
182 from phc6089.salary
183 where Bonus >2000 ;
NOTE: Data file PHC6089.SALARY.DATA is in a format that is native to another host, or the file encoding does not match the session
encoding. Cross Environment Data Access will be used, which might require additional CPU resources and might reduce
performance.
ERROR: The following columns were not found in the contributing tables: Bonus.
NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.
184 QUIT;
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE SQL used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
185
186 ods html5 (id=saspy_internal) close;ods listing;
187
Launch and run the SAS program. You may want to see what’s going wrong yourself. In the log window, SAS delivered an error message that the column Bonus cannot be found (see output above). That’s because SAS processes the WHERE clause before the SELECT clause. To make it right, add CALCULATED in the WHERE clause as shown below.
\n", "Name | \n", "Department | \n", "Salary | \n", "Bonus | \n", "
---|---|---|---|
PENDARVIS | \n", "POLICE | \n", "$103,590.00 | \n", "2071.8 | \n", "
SAWYER | \n", "CITY COUNCIL | \n", "$117,333.00 | \n", "2346.66 | \n", "
ODUM | \n", "POLICE | \n", "$106,920.00 | \n", "2138.4 | \n", "
PLANTZ | \n", "GENERAL SERVICES | \n", "$102,060.00 | \n", "2041.2 | \n", "
FORD | \n", "FIRE | \n", "$100,440.00 | \n", "2008.8 | \n", "
DANIELS | \n", "POLICE | \n", "$106,920.00 | \n", "2138.4 | \n", "
MALONEY | \n", "FIRE | \n", "$127,566.00 | \n", "2551.32 | \n", "
MOLLOY | \n", "COMMUNITY DEVELOPMENT | \n", "$102,060.00 | \n", "2041.2 | \n", "
NIEGO | \n", "FIRE | \n", "$143,682.00 | \n", "2873.64 | \n", "
PRICE | \n", "FIRE | \n", "$103,590.00 | \n", "2071.8 | \n", "
TATE JR | \n", "POLICE | \n", "$103,590.00 | \n", "2071.8 | \n", "
JIMENEZ | \n", "PROCUREMENT | \n", "$113,448.00 | \n", "2268.96 | \n", "
CEBALLOS | \n", "WATER MGMNT | \n", "$113,448.00 | \n", "2268.96 | \n", "
FERMAN | \n", "FIRE | \n", "$131,466.00 | \n", "2629.32 | \n", "
SHUM | \n", "TRANSPORTN | \n", "$104,736.00 | \n", "2094.72 | \n", "
FUNK | \n", "FIRE | \n", "$101,688.00 | \n", "2033.76 | \n", "
WRZESINSKI | \n", "FIRE | \n", "$105,918.00 | \n", "2118.36 | \n", "
GOLDSMITH | \n", "POLICE | \n", "$110,370.00 | \n", "2207.4 | \n", "
FRANKO | \n", "POLICE | \n", "$106,920.00 | \n", "2138.4 | \n", "
RADDATZ | \n", "POLICE | \n", "$110,370.00 | \n", "2207.4 | \n", "
MC NABB | \n", "FIRE | \n", "$105,918.00 | \n", "2118.36 | \n", "
FLEMING | \n", "POLICE | \n", "$100,440.00 | \n", "2008.8 | \n", "
CASEY | \n", "POLICE | \n", "$110,370.00 | \n", "2207.4 | \n", "
MACELLAIO JR | \n", "WATER MGMNT | \n", "$101,920.00 | \n", "2038.4 | \n", "
DARLING | \n", "LAW | \n", "$149,160.00 | \n", "2983.2 | \n", "
NASH | \n", "FINANCE | \n", "$103,740.00 | \n", "2074.8 | \n", "
MC COY | \n", "POLICE | \n", "$110,370.00 | \n", "2207.4 | \n", "
HOLDER | \n", "WATER MGMNT | \n", "$108,534.40 | \n", "2170.688 | \n", "
TAYLOR | \n", "FIRE | \n", "$108,462.00 | \n", "2169.24 | \n", "
PERFETTI | \n", "POLICE | \n", "$112,068.00 | \n", "2241.36 | \n", "
JACOBS | \n", "POLICE | \n", "$106,920.00 | \n", "2138.4 | \n", "
HENRY | \n", "AVIATION | \n", "$108,534.40 | \n", "2170.688 | \n", "
IRELAND | \n", "FIRE | \n", "$113,400.00 | \n", "2268 | \n", "
Now it’s working! Make the same change to your program. Check the output to make sure that SAS processes the data properly.
\n", "An alternative to using the keyword CALCULATED is to repeat the calculation expression in the WHERE clause. In the preceding program, the WHERE clause can be rewritten as:
\n", "where Employee_annual_salary *0.02 >2000;
\n",
" But note that this is not an efficient way to do this because SAS has to do the calculation twice.
\n", "The following SAS program uses ORDER BY inside PROC SQL to sort the data in the file survey.sas7bdat by the values of gender and GPA:
\n", "id | \n", "Gender | \n", "GPA | \n", "SATM | \n", "SATV | \n", "
---|---|---|---|---|
1219 | \n", "Female | \n", "3.01 | \n", "630 | \n", "590 | \n", "
1039 | \n", "Female | \n", "3.02 | \n", "560 | \n", "560 | \n", "
1125 | \n", "Female | \n", "3.08 | \n", "470 | \n", "510 | \n", "
1203 | \n", "Female | \n", "3.1 | \n", "500 | \n", "610 | \n", "
1139 | \n", "Female | \n", "3.1 | \n", "530 | \n", "600 | \n", "
1068 | \n", "Female | \n", "3.1 | \n", "550 | \n", "610 | \n", "
1116 | \n", "Female | \n", "3.1 | \n", "560 | \n", "550 | \n", "
1072 | \n", "Female | \n", "3.1 | \n", "570 | \n", "570 | \n", "
1138 | \n", "Female | \n", "3.12 | \n", "560 | \n", "580 | \n", "
1120 | \n", "Female | \n", "3.16 | \n", "680 | \n", "670 | \n", "
1102 | \n", "Female | \n", "3.2 | \n", "620 | \n", "630 | \n", "
1142 | \n", "Female | \n", "3.2 | \n", "720 | \n", "500 | \n", "
1020 | \n", "Female | \n", "3.2 | \n", "600 | \n", "630 | \n", "
1201 | \n", "Female | \n", "3.21 | \n", "760 | \n", "660 | \n", "
1089 | \n", "Female | \n", "3.25 | \n", "500 | \n", "600 | \n", "
1133 | \n", "Female | \n", "3.27 | \n", "450 | \n", "550 | \n", "
1144 | \n", "Female | \n", "3.27 | \n", "640 | \n", "600 | \n", "
1163 | \n", "Female | \n", "3.3 | \n", "600 | \n", "590 | \n", "
1038 | \n", "Female | \n", "3.3 | \n", "680 | \n", "650 | \n", "
1069 | \n", "Female | \n", "3.3 | \n", "600 | \n", "490 | \n", "
1033 | \n", "Female | \n", "3.3 | \n", "650 | \n", "600 | \n", "
1115 | \n", "Female | \n", "3.3 | \n", "580 | \n", "620 | \n", "
1057 | \n", "Female | \n", "3.3 | \n", "600 | \n", "600 | \n", "
1037 | \n", "Female | \n", "3.3 | \n", "500 | \n", "400 | \n", "
1109 | \n", "Female | \n", "3.31 | \n", "500 | \n", "490 | \n", "
1215 | \n", "Female | \n", "3.33 | \n", "650 | \n", "630 | \n", "
1078 | \n", "Female | \n", "3.33 | \n", "570 | \n", "490 | \n", "
1060 | \n", "Female | \n", "3.36 | \n", "540 | \n", "550 | \n", "
1030 | \n", "Female | \n", "3.36 | \n", "450 | \n", "450 | \n", "
1129 | \n", "Female | \n", "3.4 | \n", "500 | \n", "540 | \n", "
1100 | \n", "Female | \n", "3.4 | \n", "490 | \n", "520 | \n", "
1196 | \n", "Female | \n", "3.41 | \n", "480 | \n", "560 | \n", "
1046 | \n", "Female | \n", "3.42 | \n", "660 | \n", "580 | \n", "
1165 | \n", "Female | \n", "3.45 | \n", "560 | \n", "500 | \n", "
1148 | \n", "Female | \n", "3.46 | \n", "620 | \n", "640 | \n", "
1082 | \n", "Female | \n", "3.48 | \n", "550 | \n", "690 | \n", "
1096 | \n", "Female | \n", "3.48 | \n", "750 | \n", "550 | \n", "
1168 | \n", "Female | \n", "3.5 | \n", "650 | \n", "560 | \n", "
1091 | \n", "Female | \n", "3.5 | \n", "800 | \n", "650 | \n", "
1018 | \n", "Female | \n", "3.5 | \n", "600 | \n", "750 | \n", "
1181 | \n", "Female | \n", "3.5 | \n", "600 | \n", "550 | \n", "
1123 | \n", "Female | \n", "3.51 | \n", "560 | \n", "530 | \n", "
1177 | \n", "Female | \n", "3.53 | \n", "520 | \n", "500 | \n", "
1063 | \n", "Female | \n", "3.53 | \n", "560 | \n", "590 | \n", "
1015 | \n", "Female | \n", "3.55 | \n", "400 | \n", "600 | \n", "
1058 | \n", "Female | \n", "3.55 | \n", "585 | \n", "590 | \n", "
1160 | \n", "Female | \n", "3.57 | \n", "700 | \n", "700 | \n", "
1159 | \n", "Female | \n", "3.59 | \n", "640 | \n", "440 | \n", "
1202 | \n", "Female | \n", "3.6 | \n", "460 | \n", "540 | \n", "
1004 | \n", "Female | \n", "3.6 | \n", "710 | \n", "560 | \n", "
1086 | \n", "Female | \n", "3.6 | \n", "540 | \n", "570 | \n", "
1035 | \n", "Female | \n", "3.61 | \n", "500 | \n", "550 | \n", "
1187 | \n", "Female | \n", "3.62 | \n", "780 | \n", "660 | \n", "
1149 | \n", "Female | \n", "3.63 | \n", "640 | \n", "700 | \n", "
1110 | \n", "Female | \n", "3.63 | \n", "700 | \n", "600 | \n", "
1011 | \n", "Female | \n", "3.67 | \n", "690 | \n", "690 | \n", "
1044 | \n", "Female | \n", "3.7 | \n", "570 | \n", "560 | \n", "
1145 | \n", "Female | \n", "3.7 | \n", "500 | \n", "450 | \n", "
1207 | \n", "Female | \n", "3.7 | \n", "640 | \n", "670 | \n", "
1052 | \n", "Female | \n", "3.7 | \n", "600 | \n", "620 | \n", "
1218 | \n", "Female | \n", "3.71 | \n", "540 | \n", "560 | \n", "
1174 | \n", "Female | \n", "3.74 | \n", "680 | \n", "600 | \n", "
1118 | \n", "Female | \n", "3.74 | \n", "650 | \n", "700 | \n", "
1031 | \n", "Female | \n", "3.75 | \n", "640 | \n", "620 | \n", "
1073 | \n", "Female | \n", "3.76 | \n", "600 | \n", "550 | \n", "
1005 | \n", "Female | \n", "3.76 | \n", "600 | \n", "520 | \n", "
1055 | \n", "Female | \n", "3.76 | \n", "760 | \n", "600 | \n", "
1204 | \n", "Female | \n", "3.77 | \n", "650 | \n", "630 | \n", "
1199 | \n", "Female | \n", "3.77 | \n", "620 | \n", "640 | \n", "
1134 | \n", "Female | \n", "3.78 | \n", "650 | \n", "600 | \n", "
1208 | \n", "Female | \n", "3.78 | \n", "550 | \n", "400 | \n", "
1010 | \n", "Female | \n", "3.8 | \n", "580 | \n", "540 | \n", "
1197 | \n", "Female | \n", "3.8 | \n", "600 | \n", "700 | \n", "
1205 | \n", "Female | \n", "3.8 | \n", "550 | \n", "550 | \n", "
1077 | \n", "Female | \n", "3.81 | \n", "560 | \n", "610 | \n", "
1105 | \n", "Female | \n", "3.81 | \n", "510 | \n", "680 | \n", "
1179 | \n", "Female | \n", "3.83 | \n", "660 | \n", "660 | \n", "
1023 | \n", "Female | \n", "3.88 | \n", "670 | \n", "680 | \n", "
1094 | \n", "Female | \n", "3.89 | \n", "640 | \n", "710 | \n", "
1067 | \n", "Female | \n", "3.9 | \n", "640 | \n", "560 | \n", "
1065 | \n", "Female | \n", "3.9 | \n", "575 | \n", "600 | \n", "
1081 | \n", "Female | \n", "3.94 | \n", "620 | \n", "600 | \n", "
1014 | \n", "Female | \n", "4 | \n", "700 | \n", "700 | \n", "
1075 | \n", "Female | \n", "4 | \n", "650 | \n", "550 | \n", "
1214 | \n", "Female | \n", "4 | \n", "590 | \n", "500 | \n", "
1157 | \n", "Male | \n", "3.02 | \n", "400 | \n", "400 | \n", "
1047 | \n", "Male | \n", "3.02 | \n", "700 | \n", "570 | \n", "
1084 | \n", "Male | \n", "3.03 | \n", "690 | \n", "690 | \n", "
1140 | \n", "Male | \n", "3.04 | \n", "540 | \n", "600 | \n", "
1152 | \n", "Male | \n", "3.05 | \n", "600 | \n", "500 | \n", "
1130 | \n", "Male | \n", "3.06 | \n", "660 | \n", "620 | \n", "
1151 | \n", "Male | \n", "3.08 | \n", "590 | \n", "590 | \n", "
1053 | \n", "Male | \n", "3.08 | \n", "420 | \n", "490 | \n", "
1097 | \n", "Male | \n", "3.1 | \n", "640 | \n", "430 | \n", "
1137 | \n", "Male | \n", "3.1 | \n", "580 | \n", "640 | \n", "
1090 | \n", "Male | \n", "3.12 | \n", "560 | \n", "330 | \n", "
1185 | \n", "Male | \n", "3.13 | \n", "600 | \n", "600 | \n", "
1101 | \n", "Male | \n", "3.13 | \n", "570 | \n", "490 | \n", "
1209 | \n", "Male | \n", "3.14 | \n", "580 | \n", "580 | \n", "
1041 | \n", "Male | \n", "3.16 | \n", "620 | \n", "760 | \n", "
1220 | \n", "Male | \n", "3.19 | \n", "480 | \n", "480 | \n", "
1153 | \n", "Male | \n", "3.2 | \n", "550 | \n", "630 | \n", "
1071 | \n", "Male | \n", "3.2 | \n", "650 | \n", "660 | \n", "
1119 | \n", "Male | \n", "3.2 | \n", "640 | \n", "630 | \n", "
1045 | \n", "Male | \n", "3.2 | \n", "680 | \n", "710 | \n", "
1217 | \n", "Male | \n", "3.21 | \n", "620 | \n", "400 | \n", "
1200 | \n", "Male | \n", "3.21 | \n", "650 | \n", "590 | \n", "
1222 | \n", "Male | \n", "3.24 | \n", "510 | \n", "680 | \n", "
1175 | \n", "Male | \n", "3.3 | \n", "640 | \n", "600 | \n", "
1170 | \n", "Male | \n", "3.3 | \n", "610 | \n", "590 | \n", "
1016 | \n", "Male | \n", "3.3 | \n", "640 | \n", "600 | \n", "
1095 | \n", "Male | \n", "3.33 | \n", "690 | \n", "650 | \n", "
1212 | \n", "Male | \n", "3.35 | \n", "570 | \n", "480 | \n", "
1098 | \n", "Male | \n", "3.36 | \n", "540 | \n", "520 | \n", "
1027 | \n", "Male | \n", "3.36 | \n", "550 | \n", "600 | \n", "
1017 | \n", "Male | \n", "3.38 | \n", "720 | \n", "580 | \n", "
1087 | \n", "Male | \n", "3.4 | \n", "580 | \n", "570 | \n", "
1161 | \n", "Male | \n", "3.4 | \n", "600 | \n", "630 | \n", "
1036 | \n", "Male | \n", "3.42 | \n", "530 | \n", "760 | \n", "
1221 | \n", "Male | \n", "3.42 | \n", "590 | \n", "610 | \n", "
1194 | \n", "Male | \n", "3.46 | \n", "640 | \n", "600 | \n", "
1009 | \n", "Male | \n", "3.48 | \n", "690 | \n", "620 | \n", "
1029 | \n", "Male | \n", "3.5 | \n", "680 | \n", "680 | \n", "
1048 | \n", "Male | \n", "3.51 | \n", "650 | \n", "590 | \n", "
1024 | \n", "Male | \n", "3.51 | \n", "600 | \n", "730 | \n", "
1034 | \n", "Male | \n", "3.53 | \n", "670 | \n", "630 | \n", "
1206 | \n", "Male | \n", "3.53 | \n", "620 | \n", "630 | \n", "
1111 | \n", "Male | \n", "3.54 | \n", "540 | \n", "570 | \n", "
1150 | \n", "Male | \n", "3.55 | \n", "630 | \n", "650 | \n", "
1106 | \n", "Male | \n", "3.57 | \n", "620 | \n", "500 | \n", "
1042 | \n", "Male | \n", "3.66 | \n", "640 | \n", "640 | \n", "
1169 | \n", "Male | \n", "3.67 | \n", "650 | \n", "670 | \n", "
1019 | \n", "Male | \n", "3.7 | \n", "600 | \n", "720 | \n", "
1080 | \n", "Male | \n", "3.72 | \n", "660 | \n", "660 | \n", "
1079 | \n", "Male | \n", "3.72 | \n", "700 | \n", "640 | \n", "
1059 | \n", "Male | \n", "3.72 | \n", "800 | \n", "750 | \n", "
1173 | \n", "Male | \n", "3.73 | \n", "580 | \n", "590 | \n", "
1166 | \n", "Male | \n", "3.74 | \n", "700 | \n", "520 | \n", "
1070 | \n", "Male | \n", "3.76 | \n", "600 | \n", "610 | \n", "
1062 | \n", "Male | \n", "3.76 | \n", "670 | \n", "670 | \n", "
1226 | \n", "Male | \n", "3.78 | \n", "630 | \n", "520 | \n", "
1162 | \n", "Male | \n", "3.83 | \n", "710 | \n", "710 | \n", "
1006 | \n", "Male | \n", "3.86 | \n", "610 | \n", "720 | \n", "
1180 | \n", "Male | \n", "3.86 | \n", "720 | \n", "500 | \n", "
1172 | \n", "Male | \n", "3.87 | \n", "780 | \n", "580 | \n", "
1122 | \n", "Male | \n", "3.88 | \n", "670 | \n", "510 | \n", "
1131 | \n", "Male | \n", "3.92 | \n", "730 | \n", "800 | \n", "
1007 | \n", "Male | \n", "3.94 | \n", "710 | \n", "670 | \n", "
1028 | \n", "Male | \n", "4 | \n", "610 | \n", "600 | \n", "
Launch and run the SAS program, and review the output to convince yourself that the query result is in order first by gender and then by GPA.
\n", "Several things need to be pointed out regarding the above program:
\n", "As in PROC SORT, if you want to change the default ascending order into descending order, you just need to specify DESC following the column name.
\n", "The following SAS program sorts the data survey.sas7bdat by the values of gender in descending order then by GPA ascendingly:
\n", "id | \n", "Gender | \n", "GPA | \n", "SATM | \n", "SATV | \n", "
---|---|---|---|---|
1047 | \n", "Male | \n", "3.02 | \n", "700 | \n", "570 | \n", "
1157 | \n", "Male | \n", "3.02 | \n", "400 | \n", "400 | \n", "
1084 | \n", "Male | \n", "3.03 | \n", "690 | \n", "690 | \n", "
1140 | \n", "Male | \n", "3.04 | \n", "540 | \n", "600 | \n", "
1152 | \n", "Male | \n", "3.05 | \n", "600 | \n", "500 | \n", "
1130 | \n", "Male | \n", "3.06 | \n", "660 | \n", "620 | \n", "
1053 | \n", "Male | \n", "3.08 | \n", "420 | \n", "490 | \n", "
1151 | \n", "Male | \n", "3.08 | \n", "590 | \n", "590 | \n", "
1137 | \n", "Male | \n", "3.1 | \n", "580 | \n", "640 | \n", "
1097 | \n", "Male | \n", "3.1 | \n", "640 | \n", "430 | \n", "
1090 | \n", "Male | \n", "3.12 | \n", "560 | \n", "330 | \n", "
1185 | \n", "Male | \n", "3.13 | \n", "600 | \n", "600 | \n", "
1101 | \n", "Male | \n", "3.13 | \n", "570 | \n", "490 | \n", "
1209 | \n", "Male | \n", "3.14 | \n", "580 | \n", "580 | \n", "
1041 | \n", "Male | \n", "3.16 | \n", "620 | \n", "760 | \n", "
1220 | \n", "Male | \n", "3.19 | \n", "480 | \n", "480 | \n", "
1045 | \n", "Male | \n", "3.2 | \n", "680 | \n", "710 | \n", "
1071 | \n", "Male | \n", "3.2 | \n", "650 | \n", "660 | \n", "
1119 | \n", "Male | \n", "3.2 | \n", "640 | \n", "630 | \n", "
1153 | \n", "Male | \n", "3.2 | \n", "550 | \n", "630 | \n", "
1200 | \n", "Male | \n", "3.21 | \n", "650 | \n", "590 | \n", "
1217 | \n", "Male | \n", "3.21 | \n", "620 | \n", "400 | \n", "
1222 | \n", "Male | \n", "3.24 | \n", "510 | \n", "680 | \n", "
1175 | \n", "Male | \n", "3.3 | \n", "640 | \n", "600 | \n", "
1016 | \n", "Male | \n", "3.3 | \n", "640 | \n", "600 | \n", "
1170 | \n", "Male | \n", "3.3 | \n", "610 | \n", "590 | \n", "
1095 | \n", "Male | \n", "3.33 | \n", "690 | \n", "650 | \n", "
1212 | \n", "Male | \n", "3.35 | \n", "570 | \n", "480 | \n", "
1098 | \n", "Male | \n", "3.36 | \n", "540 | \n", "520 | \n", "
1027 | \n", "Male | \n", "3.36 | \n", "550 | \n", "600 | \n", "
1017 | \n", "Male | \n", "3.38 | \n", "720 | \n", "580 | \n", "
1161 | \n", "Male | \n", "3.4 | \n", "600 | \n", "630 | \n", "
1087 | \n", "Male | \n", "3.4 | \n", "580 | \n", "570 | \n", "
1221 | \n", "Male | \n", "3.42 | \n", "590 | \n", "610 | \n", "
1036 | \n", "Male | \n", "3.42 | \n", "530 | \n", "760 | \n", "
1194 | \n", "Male | \n", "3.46 | \n", "640 | \n", "600 | \n", "
1009 | \n", "Male | \n", "3.48 | \n", "690 | \n", "620 | \n", "
1029 | \n", "Male | \n", "3.5 | \n", "680 | \n", "680 | \n", "
1048 | \n", "Male | \n", "3.51 | \n", "650 | \n", "590 | \n", "
1024 | \n", "Male | \n", "3.51 | \n", "600 | \n", "730 | \n", "
1034 | \n", "Male | \n", "3.53 | \n", "670 | \n", "630 | \n", "
1206 | \n", "Male | \n", "3.53 | \n", "620 | \n", "630 | \n", "
1111 | \n", "Male | \n", "3.54 | \n", "540 | \n", "570 | \n", "
1150 | \n", "Male | \n", "3.55 | \n", "630 | \n", "650 | \n", "
1106 | \n", "Male | \n", "3.57 | \n", "620 | \n", "500 | \n", "
1042 | \n", "Male | \n", "3.66 | \n", "640 | \n", "640 | \n", "
1169 | \n", "Male | \n", "3.67 | \n", "650 | \n", "670 | \n", "
1019 | \n", "Male | \n", "3.7 | \n", "600 | \n", "720 | \n", "
1080 | \n", "Male | \n", "3.72 | \n", "660 | \n", "660 | \n", "
1079 | \n", "Male | \n", "3.72 | \n", "700 | \n", "640 | \n", "
1059 | \n", "Male | \n", "3.72 | \n", "800 | \n", "750 | \n", "
1173 | \n", "Male | \n", "3.73 | \n", "580 | \n", "590 | \n", "
1166 | \n", "Male | \n", "3.74 | \n", "700 | \n", "520 | \n", "
1070 | \n", "Male | \n", "3.76 | \n", "600 | \n", "610 | \n", "
1062 | \n", "Male | \n", "3.76 | \n", "670 | \n", "670 | \n", "
1226 | \n", "Male | \n", "3.78 | \n", "630 | \n", "520 | \n", "
1162 | \n", "Male | \n", "3.83 | \n", "710 | \n", "710 | \n", "
1180 | \n", "Male | \n", "3.86 | \n", "720 | \n", "500 | \n", "
1006 | \n", "Male | \n", "3.86 | \n", "610 | \n", "720 | \n", "
1172 | \n", "Male | \n", "3.87 | \n", "780 | \n", "580 | \n", "
1122 | \n", "Male | \n", "3.88 | \n", "670 | \n", "510 | \n", "
1131 | \n", "Male | \n", "3.92 | \n", "730 | \n", "800 | \n", "
1007 | \n", "Male | \n", "3.94 | \n", "710 | \n", "670 | \n", "
1028 | \n", "Male | \n", "4 | \n", "610 | \n", "600 | \n", "
1219 | \n", "Female | \n", "3.01 | \n", "630 | \n", "590 | \n", "
1039 | \n", "Female | \n", "3.02 | \n", "560 | \n", "560 | \n", "
1125 | \n", "Female | \n", "3.08 | \n", "470 | \n", "510 | \n", "
1072 | \n", "Female | \n", "3.1 | \n", "570 | \n", "570 | \n", "
1139 | \n", "Female | \n", "3.1 | \n", "530 | \n", "600 | \n", "
1068 | \n", "Female | \n", "3.1 | \n", "550 | \n", "610 | \n", "
1116 | \n", "Female | \n", "3.1 | \n", "560 | \n", "550 | \n", "
1203 | \n", "Female | \n", "3.1 | \n", "500 | \n", "610 | \n", "
1138 | \n", "Female | \n", "3.12 | \n", "560 | \n", "580 | \n", "
1120 | \n", "Female | \n", "3.16 | \n", "680 | \n", "670 | \n", "
1020 | \n", "Female | \n", "3.2 | \n", "600 | \n", "630 | \n", "
1142 | \n", "Female | \n", "3.2 | \n", "720 | \n", "500 | \n", "
1102 | \n", "Female | \n", "3.2 | \n", "620 | \n", "630 | \n", "
1201 | \n", "Female | \n", "3.21 | \n", "760 | \n", "660 | \n", "
1089 | \n", "Female | \n", "3.25 | \n", "500 | \n", "600 | \n", "
1144 | \n", "Female | \n", "3.27 | \n", "640 | \n", "600 | \n", "
1133 | \n", "Female | \n", "3.27 | \n", "450 | \n", "550 | \n", "
1163 | \n", "Female | \n", "3.3 | \n", "600 | \n", "590 | \n", "
1038 | \n", "Female | \n", "3.3 | \n", "680 | \n", "650 | \n", "
1037 | \n", "Female | \n", "3.3 | \n", "500 | \n", "400 | \n", "
1069 | \n", "Female | \n", "3.3 | \n", "600 | \n", "490 | \n", "
1033 | \n", "Female | \n", "3.3 | \n", "650 | \n", "600 | \n", "
1057 | \n", "Female | \n", "3.3 | \n", "600 | \n", "600 | \n", "
1115 | \n", "Female | \n", "3.3 | \n", "580 | \n", "620 | \n", "
1109 | \n", "Female | \n", "3.31 | \n", "500 | \n", "490 | \n", "
1078 | \n", "Female | \n", "3.33 | \n", "570 | \n", "490 | \n", "
1215 | \n", "Female | \n", "3.33 | \n", "650 | \n", "630 | \n", "
1060 | \n", "Female | \n", "3.36 | \n", "540 | \n", "550 | \n", "
1030 | \n", "Female | \n", "3.36 | \n", "450 | \n", "450 | \n", "
1129 | \n", "Female | \n", "3.4 | \n", "500 | \n", "540 | \n", "
1100 | \n", "Female | \n", "3.4 | \n", "490 | \n", "520 | \n", "
1196 | \n", "Female | \n", "3.41 | \n", "480 | \n", "560 | \n", "
1046 | \n", "Female | \n", "3.42 | \n", "660 | \n", "580 | \n", "
1165 | \n", "Female | \n", "3.45 | \n", "560 | \n", "500 | \n", "
1148 | \n", "Female | \n", "3.46 | \n", "620 | \n", "640 | \n", "
1082 | \n", "Female | \n", "3.48 | \n", "550 | \n", "690 | \n", "
1096 | \n", "Female | \n", "3.48 | \n", "750 | \n", "550 | \n", "
1181 | \n", "Female | \n", "3.5 | \n", "600 | \n", "550 | \n", "
1168 | \n", "Female | \n", "3.5 | \n", "650 | \n", "560 | \n", "
1018 | \n", "Female | \n", "3.5 | \n", "600 | \n", "750 | \n", "
1091 | \n", "Female | \n", "3.5 | \n", "800 | \n", "650 | \n", "
1123 | \n", "Female | \n", "3.51 | \n", "560 | \n", "530 | \n", "
1177 | \n", "Female | \n", "3.53 | \n", "520 | \n", "500 | \n", "
1063 | \n", "Female | \n", "3.53 | \n", "560 | \n", "590 | \n", "
1058 | \n", "Female | \n", "3.55 | \n", "585 | \n", "590 | \n", "
1015 | \n", "Female | \n", "3.55 | \n", "400 | \n", "600 | \n", "
1160 | \n", "Female | \n", "3.57 | \n", "700 | \n", "700 | \n", "
1159 | \n", "Female | \n", "3.59 | \n", "640 | \n", "440 | \n", "
1086 | \n", "Female | \n", "3.6 | \n", "540 | \n", "570 | \n", "
1004 | \n", "Female | \n", "3.6 | \n", "710 | \n", "560 | \n", "
1202 | \n", "Female | \n", "3.6 | \n", "460 | \n", "540 | \n", "
1035 | \n", "Female | \n", "3.61 | \n", "500 | \n", "550 | \n", "
1187 | \n", "Female | \n", "3.62 | \n", "780 | \n", "660 | \n", "
1149 | \n", "Female | \n", "3.63 | \n", "640 | \n", "700 | \n", "
1110 | \n", "Female | \n", "3.63 | \n", "700 | \n", "600 | \n", "
1011 | \n", "Female | \n", "3.67 | \n", "690 | \n", "690 | \n", "
1044 | \n", "Female | \n", "3.7 | \n", "570 | \n", "560 | \n", "
1145 | \n", "Female | \n", "3.7 | \n", "500 | \n", "450 | \n", "
1052 | \n", "Female | \n", "3.7 | \n", "600 | \n", "620 | \n", "
1207 | \n", "Female | \n", "3.7 | \n", "640 | \n", "670 | \n", "
1218 | \n", "Female | \n", "3.71 | \n", "540 | \n", "560 | \n", "
1174 | \n", "Female | \n", "3.74 | \n", "680 | \n", "600 | \n", "
1118 | \n", "Female | \n", "3.74 | \n", "650 | \n", "700 | \n", "
1031 | \n", "Female | \n", "3.75 | \n", "640 | \n", "620 | \n", "
1073 | \n", "Female | \n", "3.76 | \n", "600 | \n", "550 | \n", "
1055 | \n", "Female | \n", "3.76 | \n", "760 | \n", "600 | \n", "
1005 | \n", "Female | \n", "3.76 | \n", "600 | \n", "520 | \n", "
1204 | \n", "Female | \n", "3.77 | \n", "650 | \n", "630 | \n", "
1199 | \n", "Female | \n", "3.77 | \n", "620 | \n", "640 | \n", "
1134 | \n", "Female | \n", "3.78 | \n", "650 | \n", "600 | \n", "
1208 | \n", "Female | \n", "3.78 | \n", "550 | \n", "400 | \n", "
1010 | \n", "Female | \n", "3.8 | \n", "580 | \n", "540 | \n", "
1205 | \n", "Female | \n", "3.8 | \n", "550 | \n", "550 | \n", "
1197 | \n", "Female | \n", "3.8 | \n", "600 | \n", "700 | \n", "
1077 | \n", "Female | \n", "3.81 | \n", "560 | \n", "610 | \n", "
1105 | \n", "Female | \n", "3.81 | \n", "510 | \n", "680 | \n", "
1179 | \n", "Female | \n", "3.83 | \n", "660 | \n", "660 | \n", "
1023 | \n", "Female | \n", "3.88 | \n", "670 | \n", "680 | \n", "
1094 | \n", "Female | \n", "3.89 | \n", "640 | \n", "710 | \n", "
1067 | \n", "Female | \n", "3.9 | \n", "640 | \n", "560 | \n", "
1065 | \n", "Female | \n", "3.9 | \n", "575 | \n", "600 | \n", "
1081 | \n", "Female | \n", "3.94 | \n", "620 | \n", "600 | \n", "
1075 | \n", "Female | \n", "4 | \n", "650 | \n", "550 | \n", "
1014 | \n", "Female | \n", "4 | \n", "700 | \n", "700 | \n", "
1214 | \n", "Female | \n", "4 | \n", "590 | \n", "500 | \n", "
There are only two places that are different from the program in the previous example. DESC has been added after Gender to tell SAS to sort the data descending. Another way to refer to the column rather than its name is its location in the SELECT clause. GPA is listed as the third one so that we can use 3 to specify GPA.
\n", "Launch and run the SAS program, and then review the output to convince yourself that the output from this query is in descending order of Gender and in ascending order of GPA.
\n", "Up until now, you might think that ORDER BY can perform the same as PROC SORT. Actually, it can do more than that. Let’s find out with the next example.
\n", "The following program sorts the survey data first by gender in descending order as before, then by mean values of SAT math and verbal scores in ascending order:
\n", "id | \n", "Gender | \n", "GPA | \n", "SmokeCigarettes | \n", "SATM | \n", "SATV | \n", "
---|---|---|---|---|---|
1157 | \n", "Male | \n", "3.02 | \n", "No | \n", "400 | \n", "400 | \n", "
1090 | \n", "Male | \n", "3.12 | \n", "No | \n", "560 | \n", "330 | \n", "
1053 | \n", "Male | \n", "3.08 | \n", "No | \n", "420 | \n", "490 | \n", "
1220 | \n", "Male | \n", "3.19 | \n", "No | \n", "480 | \n", "480 | \n", "
1217 | \n", "Male | \n", "3.21 | \n", "No | \n", "620 | \n", "400 | \n", "
1212 | \n", "Male | \n", "3.35 | \n", "No | \n", "570 | \n", "480 | \n", "
1101 | \n", "Male | \n", "3.13 | \n", "No | \n", "570 | \n", "490 | \n", "
1098 | \n", "Male | \n", "3.36 | \n", "No | \n", "540 | \n", "520 | \n", "
1097 | \n", "Male | \n", "3.1 | \n", "No | \n", "640 | \n", "430 | \n", "
1152 | \n", "Male | \n", "3.05 | \n", "No | \n", "600 | \n", "500 | \n", "
1111 | \n", "Male | \n", "3.54 | \n", "No | \n", "540 | \n", "570 | \n", "
1106 | \n", "Male | \n", "3.57 | \n", "No | \n", "620 | \n", "500 | \n", "
1140 | \n", "Male | \n", "3.04 | \n", "Yes | \n", "540 | \n", "600 | \n", "
1226 | \n", "Male | \n", "3.78 | \n", "No | \n", "630 | \n", "520 | \n", "
1087 | \n", "Male | \n", "3.4 | \n", "No | \n", "580 | \n", "570 | \n", "
1027 | \n", "Male | \n", "3.36 | \n", "No | \n", "550 | \n", "600 | \n", "
1209 | \n", "Male | \n", "3.14 | \n", "No | \n", "580 | \n", "580 | \n", "
1173 | \n", "Male | \n", "3.73 | \n", "No | \n", "580 | \n", "590 | \n", "
1122 | \n", "Male | \n", "3.88 | \n", "No | \n", "670 | \n", "510 | \n", "
1153 | \n", "Male | \n", "3.2 | \n", "No | \n", "550 | \n", "630 | \n", "
1151 | \n", "Male | \n", "3.08 | \n", "No | \n", "590 | \n", "590 | \n", "
1222 | \n", "Male | \n", "3.24 | \n", "No | \n", "510 | \n", "680 | \n", "
1185 | \n", "Male | \n", "3.13 | \n", "No | \n", "600 | \n", "600 | \n", "
1221 | \n", "Male | \n", "3.42 | \n", "Yes | \n", "590 | \n", "610 | \n", "
1170 | \n", "Male | \n", "3.3 | \n", "No | \n", "610 | \n", "590 | \n", "
1070 | \n", "Male | \n", "3.76 | \n", "No | \n", "600 | \n", "610 | \n", "
1028 | \n", "Male | \n", "4 | \n", "No | \n", "610 | \n", "600 | \n", "
1137 | \n", "Male | \n", "3.1 | \n", "No | \n", "580 | \n", "640 | \n", "
1166 | \n", "Male | \n", "3.74 | \n", "No | \n", "700 | \n", "520 | \n", "
1180 | \n", "Male | \n", "3.86 | \n", "No | \n", "720 | \n", "500 | \n", "
1161 | \n", "Male | \n", "3.4 | \n", "No | \n", "600 | \n", "630 | \n", "
1175 | \n", "Male | \n", "3.3 | \n", "Yes | \n", "640 | \n", "600 | \n", "
1016 | \n", "Male | \n", "3.3 | \n", "No | \n", "640 | \n", "600 | \n", "
1194 | \n", "Male | \n", "3.46 | \n", "No | \n", "640 | \n", "600 | \n", "
1048 | \n", "Male | \n", "3.51 | \n", "No | \n", "650 | \n", "590 | \n", "
1200 | \n", "Male | \n", "3.21 | \n", "No | \n", "650 | \n", "590 | \n", "
1206 | \n", "Male | \n", "3.53 | \n", "No | \n", "620 | \n", "630 | \n", "
1119 | \n", "Male | \n", "3.2 | \n", "No | \n", "640 | \n", "630 | \n", "
1047 | \n", "Male | \n", "3.02 | \n", "No | \n", "700 | \n", "570 | \n", "
1150 | \n", "Male | \n", "3.55 | \n", "No | \n", "630 | \n", "650 | \n", "
1042 | \n", "Male | \n", "3.66 | \n", "No | \n", "640 | \n", "640 | \n", "
1130 | \n", "Male | \n", "3.06 | \n", "No | \n", "660 | \n", "620 | \n", "
1036 | \n", "Male | \n", "3.42 | \n", "No | \n", "530 | \n", "760 | \n", "
1034 | \n", "Male | \n", "3.53 | \n", "No | \n", "670 | \n", "630 | \n", "
1017 | \n", "Male | \n", "3.38 | \n", "No | \n", "720 | \n", "580 | \n", "
1071 | \n", "Male | \n", "3.2 | \n", "No | \n", "650 | \n", "660 | \n", "
1009 | \n", "Male | \n", "3.48 | \n", "No | \n", "690 | \n", "620 | \n", "
1080 | \n", "Male | \n", "3.72 | \n", "No | \n", "660 | \n", "660 | \n", "
1169 | \n", "Male | \n", "3.67 | \n", "No | \n", "650 | \n", "670 | \n", "
1019 | \n", "Male | \n", "3.7 | \n", "No | \n", "600 | \n", "720 | \n", "
1006 | \n", "Male | \n", "3.86 | \n", "No | \n", "610 | \n", "720 | \n", "
1024 | \n", "Male | \n", "3.51 | \n", "No | \n", "600 | \n", "730 | \n", "
1079 | \n", "Male | \n", "3.72 | \n", "No | \n", "700 | \n", "640 | \n", "
1062 | \n", "Male | \n", "3.76 | \n", "No | \n", "670 | \n", "670 | \n", "
1095 | \n", "Male | \n", "3.33 | \n", "No | \n", "690 | \n", "650 | \n", "
1172 | \n", "Male | \n", "3.87 | \n", "Yes | \n", "780 | \n", "580 | \n", "
1029 | \n", "Male | \n", "3.5 | \n", "No | \n", "680 | \n", "680 | \n", "
1041 | \n", "Male | \n", "3.16 | \n", "No | \n", "620 | \n", "760 | \n", "
1007 | \n", "Male | \n", "3.94 | \n", "No | \n", "710 | \n", "670 | \n", "
1084 | \n", "Male | \n", "3.03 | \n", "No | \n", "690 | \n", "690 | \n", "
1045 | \n", "Male | \n", "3.2 | \n", "No | \n", "680 | \n", "710 | \n", "
1162 | \n", "Male | \n", "3.83 | \n", "No | \n", "710 | \n", "710 | \n", "
1131 | \n", "Male | \n", "3.92 | \n", "No | \n", "730 | \n", "800 | \n", "
1059 | \n", "Male | \n", "3.72 | \n", "No | \n", "800 | \n", "750 | \n", "
1037 | \n", "Female | \n", "3.3 | \n", "No | \n", "500 | \n", "400 | \n", "
1030 | \n", "Female | \n", "3.36 | \n", "No | \n", "450 | \n", "450 | \n", "
1145 | \n", "Female | \n", "3.7 | \n", "No | \n", "500 | \n", "450 | \n", "
1208 | \n", "Female | \n", "3.78 | \n", "No | \n", "550 | \n", "400 | \n", "
1125 | \n", "Female | \n", "3.08 | \n", "No | \n", "470 | \n", "510 | \n", "
1109 | \n", "Female | \n", "3.31 | \n", "No | \n", "500 | \n", "490 | \n", "
1133 | \n", "Female | \n", "3.27 | \n", "No | \n", "450 | \n", "550 | \n", "
1015 | \n", "Female | \n", "3.55 | \n", "No | \n", "400 | \n", "600 | \n", "
1202 | \n", "Female | \n", "3.6 | \n", "No | \n", "460 | \n", "540 | \n", "
1100 | \n", "Female | \n", "3.4 | \n", "No | \n", "490 | \n", "520 | \n", "
1177 | \n", "Female | \n", "3.53 | \n", "No | \n", "520 | \n", "500 | \n", "
1196 | \n", "Female | \n", "3.41 | \n", "No | \n", "480 | \n", "560 | \n", "
1129 | \n", "Female | \n", "3.4 | \n", "No | \n", "500 | \n", "540 | \n", "
1035 | \n", "Female | \n", "3.61 | \n", "No | \n", "500 | \n", "550 | \n", "
1078 | \n", "Female | \n", "3.33 | \n", "No | \n", "570 | \n", "490 | \n", "
1165 | \n", "Female | \n", "3.45 | \n", "No | \n", "560 | \n", "500 | \n", "
1159 | \n", "Female | \n", "3.59 | \n", "No | \n", "640 | \n", "440 | \n", "
1069 | \n", "Female | \n", "3.3 | \n", "No | \n", "600 | \n", "490 | \n", "
1060 | \n", "Female | \n", "3.36 | \n", "No | \n", "540 | \n", "550 | \n", "
1123 | \n", "Female | \n", "3.51 | \n", "No | \n", "560 | \n", "530 | \n", "
1214 | \n", "Female | \n", "4 | \n", "No | \n", "590 | \n", "500 | \n", "
1089 | \n", "Female | \n", "3.25 | \n", "Yes | \n", "500 | \n", "600 | \n", "
1218 | \n", "Female | \n", "3.71 | \n", "No | \n", "540 | \n", "560 | \n", "
1205 | \n", "Female | \n", "3.8 | \n", "No | \n", "550 | \n", "550 | \n", "
1086 | \n", "Female | \n", "3.6 | \n", "No | \n", "540 | \n", "570 | \n", "
1116 | \n", "Female | \n", "3.1 | \n", "No | \n", "560 | \n", "550 | \n", "
1203 | \n", "Female | \n", "3.1 | \n", "No | \n", "500 | \n", "610 | \n", "
1039 | \n", "Female | \n", "3.02 | \n", "No | \n", "560 | \n", "560 | \n", "
1010 | \n", "Female | \n", "3.8 | \n", "No | \n", "580 | \n", "540 | \n", "
1005 | \n", "Female | \n", "3.76 | \n", "No | \n", "600 | \n", "520 | \n", "
1044 | \n", "Female | \n", "3.7 | \n", "No | \n", "570 | \n", "560 | \n", "
1139 | \n", "Female | \n", "3.1 | \n", "No | \n", "530 | \n", "600 | \n", "
1072 | \n", "Female | \n", "3.1 | \n", "No | \n", "570 | \n", "570 | \n", "
1138 | \n", "Female | \n", "3.12 | \n", "No | \n", "560 | \n", "580 | \n", "
1181 | \n", "Female | \n", "3.5 | \n", "No | \n", "600 | \n", "550 | \n", "
1073 | \n", "Female | \n", "3.76 | \n", "No | \n", "600 | \n", "550 | \n", "
1063 | \n", "Female | \n", "3.53 | \n", "No | \n", "560 | \n", "590 | \n", "
1068 | \n", "Female | \n", "3.1 | \n", "No | \n", "550 | \n", "610 | \n", "
1077 | \n", "Female | \n", "3.81 | \n", "No | \n", "560 | \n", "610 | \n", "
1065 | \n", "Female | \n", "3.9 | \n", "No | \n", "575 | \n", "600 | \n", "
1058 | \n", "Female | \n", "3.55 | \n", "No | \n", "585 | \n", "590 | \n", "
1163 | \n", "Female | \n", "3.3 | \n", "No | \n", "600 | \n", "590 | \n", "
1105 | \n", "Female | \n", "3.81 | \n", "No | \n", "510 | \n", "680 | \n", "
1075 | \n", "Female | \n", "4 | \n", "No | \n", "650 | \n", "550 | \n", "
1067 | \n", "Female | \n", "3.9 | \n", "No | \n", "640 | \n", "560 | \n", "
1057 | \n", "Female | \n", "3.3 | \n", "No | \n", "600 | \n", "600 | \n", "
1115 | \n", "Female | \n", "3.3 | \n", "No | \n", "580 | \n", "620 | \n", "
1168 | \n", "Female | \n", "3.5 | \n", "No | \n", "650 | \n", "560 | \n", "
1081 | \n", "Female | \n", "3.94 | \n", "No | \n", "620 | \n", "600 | \n", "
1142 | \n", "Female | \n", "3.2 | \n", "No | \n", "720 | \n", "500 | \n", "
1052 | \n", "Female | \n", "3.7 | \n", "No | \n", "600 | \n", "620 | \n", "
1219 | \n", "Female | \n", "3.01 | \n", "No | \n", "630 | \n", "590 | \n", "
1020 | \n", "Female | \n", "3.2 | \n", "No | \n", "600 | \n", "630 | \n", "
1082 | \n", "Female | \n", "3.48 | \n", "No | \n", "550 | \n", "690 | \n", "
1144 | \n", "Female | \n", "3.27 | \n", "No | \n", "640 | \n", "600 | \n", "
1046 | \n", "Female | \n", "3.42 | \n", "No | \n", "660 | \n", "580 | \n", "
1033 | \n", "Female | \n", "3.3 | \n", "No | \n", "650 | \n", "600 | \n", "
1134 | \n", "Female | \n", "3.78 | \n", "No | \n", "650 | \n", "600 | \n", "
1102 | \n", "Female | \n", "3.2 | \n", "No | \n", "620 | \n", "630 | \n", "
1148 | \n", "Female | \n", "3.46 | \n", "No | \n", "620 | \n", "640 | \n", "
1031 | \n", "Female | \n", "3.75 | \n", "No | \n", "640 | \n", "620 | \n", "
1199 | \n", "Female | \n", "3.77 | \n", "No | \n", "620 | \n", "640 | \n", "
1004 | \n", "Female | \n", "3.6 | \n", "No | \n", "710 | \n", "560 | \n", "
1174 | \n", "Female | \n", "3.74 | \n", "No | \n", "680 | \n", "600 | \n", "
1215 | \n", "Female | \n", "3.33 | \n", "No | \n", "650 | \n", "630 | \n", "
1204 | \n", "Female | \n", "3.77 | \n", "No | \n", "650 | \n", "630 | \n", "
1110 | \n", "Female | \n", "3.63 | \n", "No | \n", "700 | \n", "600 | \n", "
1096 | \n", "Female | \n", "3.48 | \n", "No | \n", "750 | \n", "550 | \n", "
1197 | \n", "Female | \n", "3.8 | \n", "No | \n", "600 | \n", "700 | \n", "
1207 | \n", "Female | \n", "3.7 | \n", "No | \n", "640 | \n", "670 | \n", "
1179 | \n", "Female | \n", "3.83 | \n", "No | \n", "660 | \n", "660 | \n", "
1038 | \n", "Female | \n", "3.3 | \n", "No | \n", "680 | \n", "650 | \n", "
1149 | \n", "Female | \n", "3.63 | \n", "No | \n", "640 | \n", "700 | \n", "
1023 | \n", "Female | \n", "3.88 | \n", "No | \n", "670 | \n", "680 | \n", "
1018 | \n", "Female | \n", "3.5 | \n", "No | \n", "600 | \n", "750 | \n", "
1120 | \n", "Female | \n", "3.16 | \n", "No | \n", "680 | \n", "670 | \n", "
1118 | \n", "Female | \n", "3.74 | \n", "No | \n", "650 | \n", "700 | \n", "
1094 | \n", "Female | \n", "3.89 | \n", "No | \n", "640 | \n", "710 | \n", "
1055 | \n", "Female | \n", "3.76 | \n", "No | \n", "760 | \n", "600 | \n", "
1011 | \n", "Female | \n", "3.67 | \n", "Yes | \n", "690 | \n", "690 | \n", "
1160 | \n", "Female | \n", "3.57 | \n", "No | \n", "700 | \n", "700 | \n", "
1014 | \n", "Female | \n", "4 | \n", "No | \n", "700 | \n", "700 | \n", "
1201 | \n", "Female | \n", "3.21 | \n", "Yes | \n", "760 | \n", "660 | \n", "
1187 | \n", "Female | \n", "3.62 | \n", "No | \n", "780 | \n", "660 | \n", "
1091 | \n", "Female | \n", "3.5 | \n", "No | \n", "800 | \n", "650 | \n", "
Since all columns will be used in the query, * is used to specify all columns after SELECT. The WHERE clause remains the same. In the ORDER BY clause, besides Gender, one function is used to calculate the average scores of SATM and SATV, then uses the calculation results to sort the data inside each gender group. To get the same result, could you try other SAS steps and count how many of them will be needed?
\n", "Launch and run the SAS program, and review the output to convince yourself that the data has been sorted in desired order.
\n", "One more thing, you may notice a note in log window when running this program.
\n", "NOTE: The query as specified involves ordering by an item that doesn't appear in its SELECT clause.\n", "
That’s because MEAN(SATM,SATV) is not listed in the SELECT clause, only in the ORDER BY clause.
\n", "Summary function | \n", "\t\t\tDescription | \n", "\t\t
AVG, MEAN | \n", "\t\t\tmean or average of values | \n", "\t\t
COUNT, FREQ, N | \n", "\t\t\tnumber of non-missing values | \n", "\t\t
CSS | \n", "\t\t\tcorrected sum of squares | \n", "\t\t
CV | \n", "\t\t\tcoefficient of variation(percent) | \n", "\t\t
MAX | \n", "\t\t\tlargest value | \n", "\t\t
MIN | \n", "\t\t\tsmallest value | \n", "\t\t
NMISS | \n", "\t\t\tnumber of missing values | \n", "\t\t
PRT | \n", "\t\t\tprobability of a greater absolute value of student's t | \n", "\t\t
RANGE | \n", "\t\t\trange of values | \n", "\t\t
STD | \n", "\t\t\tstandard deviation | \n", "\t\t
STDERR | \n", "\t\t\tstandard error of the mean | \n", "\t\t
SUM | \n", "\t\t\tsum of values | \n", "\t\t
T | \n", "\t\t\tstudent's t value for testing hypothesis | \n", "\t\t
USS | \n", "\t\t\tuncorrected sum of squares | \n", "\t\t
VAR | \n", "\t\t\tvariance | \n", "\t\t
The following program uses the AVG() function to calculate the mean scores of SAT math and verbal test:
\n", "average_Math | \n", "average_Verbal | \n", "
---|---|
599.0046 | \n", "580.3256 | \n", "
First launch and run the SAS program. When checking the output you will see two overall average scores have been calculated for SATM and SATV separately. There is only one observation in the output window.
\n", "Let’s review the function in the code. To calculate average, either MEAN() or AVG() can be used in this case. Note that there is only one argument (column) inside the function AVG(). So the statistic is calculated across all rows for one column.
\n", "Quite simple, right? Let’s add one more argument into the function. Can you guess how many observations will be in the output?
\n", "In the following program, two columns are the arguments of the function MEAN():
\n", "average | \n", "
---|
700 | \n", "
600 | \n", "
470 | \n", "
635 | \n", "
560 | \n", "
665 | \n", "
690 | \n", "
595 | \n", "
655 | \n", "
560 | \n", "
690 | \n", "
. | \n", "
515 | \n", "
700 | \n", "
500 | \n", "
620 | \n", "
650 | \n", "
675 | \n", "
660 | \n", "
615 | \n", "
450 | \n", "
650 | \n", "
675 | \n", "
665 | \n", "
620 | \n", "
560 | \n", "
575 | \n", "
605 | \n", "
680 | \n", "
450 | \n", "
630 | \n", "
350 | \n", "
625 | \n", "
650 | \n", "
525 | \n", "
645 | \n", "
450 | \n", "
665 | \n", "
560 | \n", "
. | \n", "
690 | \n", "
640 | \n", "
615 | \n", "
565 | \n", "
695 | \n", "
620 | \n", "
635 | \n", "
620 | \n", "
630 | \n", "
450 | \n", "
600 | \n", "
610 | \n", "
455 | \n", "
605 | \n", "
680 | \n", "
550 | \n", "
600 | \n", "
587.5 | \n", "
775 | \n", "
545 | \n", "
615 | \n", "
670 | \n", "
575 | \n", "
625 | \n", "
587.5 | \n", "
425 | \n", "
600 | \n", "
580 | \n", "
545 | \n", "
605 | \n", "
655 | \n", "
570 | \n", "
575 | \n", "
475 | \n", "
600 | \n", "
580 | \n", "
585 | \n", "
530 | \n", "
670 | \n", "
660 | \n", "
610 | \n", "
620 | \n", "
570 | \n", "
690 | \n", "
710 | \n", "
555 | \n", "
575 | \n", "
. | \n", "
550 | \n", "
445 | \n", "
725 | \n", "
575 | \n", "
560 | \n", "
675 | \n", "
670 | \n", "
650 | \n", "
535 | \n", "
530 | \n", "
650 | \n", "
505 | \n", "
530 | \n", "
625 | \n", "
475 | \n", "
. | \n", "
595 | \n", "
560 | \n", "
580 | \n", "
525 | \n", "
495 | \n", "
650 | \n", "
555 | \n", "
. | \n", "
600 | \n", "
530 | \n", "
600 | \n", "
555 | \n", "
525 | \n", "
675 | \n", "
635 | \n", "
675 | \n", "
475 | \n", "
590 | \n", "
545 | \n", "
435 | \n", "
490 | \n", "
645 | \n", "
625 | \n", "
610 | \n", "
520 | \n", "
640 | \n", "
765 | \n", "
420 | \n", "
500 | \n", "
625 | \n", "
585 | \n", "
625 | \n", "
610 | \n", "
570 | \n", "
565 | \n", "
570 | \n", "
. | \n", "
610 | \n", "
525 | \n", "
620 | \n", "
475 | \n", "
590 | \n", "
600 | \n", "
630 | \n", "
670 | \n", "
640 | \n", "
590 | \n", "
550 | \n", "
590 | \n", "
485 | \n", "
. | \n", "
660 | \n", "
400 | \n", "
540 | \n", "
540 | \n", "
700 | \n", "
615 | \n", "
710 | \n", "
595 | \n", "
625 | \n", "
530 | \n", "
610 | \n", "
535 | \n", "
605 | \n", "
660 | \n", "
600 | \n", "
500 | \n", "
680 | \n", "
585 | \n", "
640 | \n", "
620 | \n", "
595 | \n", "
510 | \n", "
650 | \n", "
660 | \n", "
610 | \n", "
575 | \n", "
550 | \n", "
505 | \n", "
. | \n", "
600 | \n", "
530 | \n", "
720 | \n", "
610 | \n", "
590 | \n", "
540 | \n", "
530 | \n", "
. | \n", "
500 | \n", "
620 | \n", "
555 | \n", "
520 | \n", "
650 | \n", "
525 | \n", "
630 | \n", "
620 | \n", "
710 | \n", "
500 | \n", "
555 | \n", "
640 | \n", "
550 | \n", "
625 | \n", "
655 | \n", "
475 | \n", "
580 | \n", "
625 | \n", "
610 | \n", "
525 | \n", "
425 | \n", "
545 | \n", "
640 | \n", "
640 | \n", "
510 | \n", "
550 | \n", "
610 | \n", "
480 | \n", "
600 | \n", "
595 | \n", "
. | \n", "
640 | \n", "
635 | \n", "
575 | \n", "
We changed the program a little bit. Both SATM and SATV are put inside the function as arguments. Launch and run the SAS program. You will see there are 226 observations, which is the same as in the original survey data.
\n", "If you add more than one column as arguments of summary functions, SAS will perform the calculation across the columns for each row to generate the above output.
\n", "In this case, the summary function is not performing aggregation anymore. SAS then looks for a like-named function in BASE SAS. If yes, the calculation will be performed for each row; if not, an error message will be output in the log window. You can try to change MEAN() to AVG() to see what will happen.
\n", "ERROR: Function AVG could not be located.\n", "
The following program uses only one argument for MEAN(), but add one more column in the SELECT clause:
\n", "Gender | \n", "average_Math | \n", "
---|---|
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
Female | \n", "599.0046 | \n", "
Male | \n", "599.0046 | \n", "
In the above program, the the SELECT statement changed again. This time, only one argument is for the MEAN() function to calculate the overall average score of SAT math grades. Outside the function, another column has been selected as well. What output will it produce?
\n", "Launch and run the SAS program. You may be surprised that the output contains 226 rows. Review the output you will see two things that have been done by the above code:
\n", "Note that the overall average math score is just repeated for each row. You can find a message like the one below in the log window. When you submit such a program, SAS calculate the statistic first. Then merge it back with other columns. That’s how “remerging” happens.
\n", "NOTE: The query requires remerging summary statistics back with the original data\n", "
The above result is not what we wanted. Now, let’s see how to use the GROUP BY clause to make it reasonable.
\n", "The following example calculates the average SAT math score for each gender group:
\n", "Gender | \n", "average_Math | \n", "
---|---|
Female | \n", "589.5082 | \n", "
Male | \n", "611.3298 | \n", "
The above program seems identical to the program in the previous example except for one more clause: GROUP BY. Finally, we get it right and obtain the desired result: the average SAT math scores for female and male students. Of course, you can make further use of GROUP BY by adding multiple columns. Let’s find out with the next example.
\n", "The following program uses both Gender and SmokeCigarettes in the GROUP BY clause to calculate the average SAT math scores:
\n", "Gender | \n", "SmokeCigarettes | \n", "average_Math | \n", "
---|---|---|
Female | \n", "No | \n", "589.6552 | \n", "
Female | \n", "Yes | \n", "586.6667 | \n", "
Male | \n", "No | \n", "613.2353 | \n", "
Male | \n", "Yes | \n", "593.3333 | \n", "
Launch and run the SAS program, then review the output. As you can see, the average math scores are calculated for each smoking group (Yes or No) inside each gender group (Female or Male).
\n", "Just one more thing about this program, the columns can also be referred to by their locations in the SELECT clause as in the WHERE clause. Here, 1 and 2 are used to refer to Gender and SmokeCigarettes.
\n", "Next, we will pay attention to one special summary function in SQL, which is COUNT(). You can use the COUN() function to count the non-missing values.
\n", "The following example count the number of rows in survey data, the number of non-missing records for math and verbal test scores, and the distinct values of gender:
\n", "No_obs | \n", "No_Math_records | \n", "No_Verbal_records | \n", "Gender_group | \n", "
---|---|---|---|
226 | \n", "216 | \n", "215 | \n", "2 | \n", "
The above code reveals three different common ways of using the COUNT() function.
\n", "Launch and run the SAS program, then review the output. With knowledge of some of the missing values inside the table, we are not surprised to see the first three numbers unmatched. The total number of rows in survey data is 226. The total numbers of non-missing values of math and verbal scores are 216 and 215, separately. Both numbers are less than 226, which means there are missing values in each column, and SATV has one more value missing. There are only two categories in Gender, Male and Female. So the last count is 2.
\n", "The following program calculates the average salary for each department, then select three departments as needed in the query output:
\n", "Department | \n", "Avg_salary | \n", "
---|---|
LAW | \n", "$71,082.20 | \n", "
FINANCE | \n", "$82,184.00 | \n", "
FIRE | \n", "$90,742.33 | \n", "
Let’s review the program first. The code selects the column Department and uses the summary function AVG() to compute the average salaries. Since the GROUP BY clause also is also present in the SELECT statement, the averages are for each department. The user is only interested in three departments, Law, Finance and Fire. So we use the HAVING clause to select only these three to be output. Finally, we ask SAS to sort the data by average salaries. This program contains every clause we have learned so far except the WHERE clause, which we will address later.
\n", "Launch and run the SAS program and review the output to make sure you understand the output.
\n", "You may wonder if WHERE can do the same thing as HAVING does in the above program. You can try replacing Having with WHERE clause as following. You will get identical output as before.
\n", "Department | \n", "Avg_salary | \n", "
---|---|
LAW | \n", "$71,082.20 | \n", "
FINANCE | \n", "$82,184.00 | \n", "
FIRE | \n", "$90,742.33 | \n", "
However, let’s not assume that WHERE and HAVING are the same based on this. There are some big differences between them. Generally speaking, HAVING has control on grouped data during output; WHERE controls input data row by row. Let’s see more examples about these two commands.
\n", "The following program calculates the average salary for each department and choose ones having more than \\$70,000:
\n", "Department | \n", "Avg_salary | \n", "
---|---|
LAW | \n", "$71,082.20 | \n", "
BOARD OF ELECTION | \n", "$74,988.00 | \n", "
HEALTH | \n", "$75,066.86 | \n", "
TRANSPORTN | \n", "$79,438.18 | \n", "
POLICE | \n", "$81,850.26 | \n", "
FINANCE | \n", "$82,184.00 | \n", "
WATER MGMNT | \n", "$84,780.42 | \n", "
PROCUREMENT | \n", "$89,236.00 | \n", "
COMMUNITY DEVELOPMENT | \n", "$90,096.00 | \n", "
DoIT | \n", "$90,252.00 | \n", "
FIRE | \n", "$90,742.33 | \n", "
ADMIN HEARNG | \n", "$91,980.00 | \n", "
BUILDINGS | \n", "$94,793.01 | \n", "
Only a small change has been made to this program. The condition in the HAVING clause changed the department average salary more than \\$70,000. So, the expression used in the HAVING statement is a summary function. And, the data is sorted by average values.
\n", "Launch and run the SAS program and review the output. As we expect, all departments having more than \\$70,000 average salary are listed as the query result.
\n", "Next, let’s try using WHERE to perform the same task.
\n", "349 ods listing close;ods html5 (id=saspy_internal) file=stdout options(bitmap_mode='inline') device=svg style=HTMLBlue; ods
349! graphics on / outputfmt=png;
NOTE: Writing HTML5(SASPY_INTERNAL) Body file: STDOUT
350
351 PROC SQL;
352 select Department,
353 avg(Employee_annual_salary) as Avg_salary format=DOLLAR12.2
354 from phc6089.salary
355 where calculated Avg_salary > 70000
356 group by Department
357 order by Avg_salary;
NOTE: Data file PHC6089.SALARY.DATA is in a format that is native to another host, or the file encoding does not match the session
encoding. Cross Environment Data Access will be used, which might require additional CPU resources and might reduce
performance.
ERROR: Summary functions are restricted to the SELECT and HAVING clauses only.
NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.
358 QUIT;
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE SQL used (Total process time):
real time 0.00 seconds
cpu time 0.01 seconds
359
360 ods html5 (id=saspy_internal) close;ods listing;
361
You must remember that to use the computed result in the WHERE clause, the keyword “CALCULATED” should be inserted. Oops! SAS gives us an error message like this:
\n", "ERROR: Summary functions are restricted to the SELECT and HAVING clauses only.\n", "
This example illustrates a big difference between HAVING and WHERE. The summary functions can be used in a HAVING clause but not in a WHERE clause, because HAVING works on grouped data, but WHERE evaluates existing or calculated data row by row.
\n", "Based on our current experiences with these two clauses, you might prefer to use HAVING since it can be used for both situations. However, don’t rush to this conclusion either. You will find out more in the next example.
\n", "The following two SAS program are similar. The only difference is that the first program uses a WHERE clause and the second program uses a HAVING clause. They try to accomplish the same task: count how many employees at each position inside Police Department:
\n", "Position Title | \n", "Employees | \n", "
---|---|
ACCOUNTANT II | \n", "1 | \n", "
CLINICAL THERAPIST III | \n", "1 | \n", "
CROSSING GUARD | \n", "5 | \n", "
CROSSING GUARD - PER AGREEMENT | \n", "3 | \n", "
DIR OF POLICE RECORDS | \n", "1 | \n", "
FISCAL ADMINISTRATOR | \n", "1 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER (ASSIGNED AS DETECTIVE) | \n", "13 | \n", "
POLICE OFFICER / FLD TRNG OFFICER | \n", "1 | \n", "
SENIOR DATA ENTRY OPERATOR | \n", "2 | \n", "
SERGEANT | \n", "11 | \n", "
Position Title | \n", "Employees | \n", "
---|---|
ACCOUNTANT II | \n", "1 | \n", "
CLINICAL THERAPIST III | \n", "2 | \n", "
CROSSING GUARD | \n", "5 | \n", "
CROSSING GUARD | \n", "5 | \n", "
CROSSING GUARD | \n", "5 | \n", "
CROSSING GUARD | \n", "5 | \n", "
CROSSING GUARD | \n", "5 | \n", "
CROSSING GUARD - PER AGREEMENT | \n", "3 | \n", "
CROSSING GUARD - PER AGREEMENT | \n", "3 | \n", "
CROSSING GUARD - PER AGREEMENT | \n", "3 | \n", "
DIR OF POLICE RECORDS | \n", "1 | \n", "
FISCAL ADMINISTRATOR | \n", "1 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER | \n", "85 | \n", "
POLICE OFFICER (ASSIGNED AS DETECTIVE) | \n", "13 | \n", "
POLICE OFFICER (ASSIGNED AS DETECTIVE) | \n", "13 | \n", "
POLICE OFFICER (ASSIGNED AS DETECTIVE) | \n", "13 | \n", "
POLICE OFFICER (ASSIGNED AS DETECTIVE) | \n", "13 | \n", "
POLICE OFFICER (ASSIGNED AS DETECTIVE) | \n", "13 | \n", "
POLICE OFFICER (ASSIGNED AS DETECTIVE) | \n", "13 | \n", "
POLICE OFFICER (ASSIGNED AS DETECTIVE) | \n", "13 | \n", "
POLICE OFFICER (ASSIGNED AS DETECTIVE) | \n", "13 | \n", "
POLICE OFFICER (ASSIGNED AS DETECTIVE) | \n", "13 | \n", "
POLICE OFFICER (ASSIGNED AS DETECTIVE) | \n", "13 | \n", "
POLICE OFFICER (ASSIGNED AS DETECTIVE) | \n", "13 | \n", "
POLICE OFFICER (ASSIGNED AS DETECTIVE) | \n", "13 | \n", "
POLICE OFFICER (ASSIGNED AS DETECTIVE) | \n", "13 | \n", "
POLICE OFFICER / FLD TRNG OFFICER | \n", "1 | \n", "
SENIOR DATA ENTRY OPERATOR | \n", "4 | \n", "
SENIOR DATA ENTRY OPERATOR | \n", "4 | \n", "
SERGEANT | \n", "11 | \n", "
SERGEANT | \n", "11 | \n", "
SERGEANT | \n", "11 | \n", "
SERGEANT | \n", "11 | \n", "
SERGEANT | \n", "11 | \n", "
SERGEANT | \n", "11 | \n", "
SERGEANT | \n", "11 | \n", "
SERGEANT | \n", "11 | \n", "
SERGEANT | \n", "11 | \n", "
SERGEANT | \n", "11 | \n", "
SERGEANT | \n", "11 | \n", "
Now, Launch and run both programs. The output on the top is from the program using WHERE clause; the output on the bottom is the partial output from the program using HAVING clause.
\n", "You might be surprised to see how different these two results are. One would expect a result like the output on the top. But the output on the bottom has so many more rows, and even some numbers do not match! Let’s review the code to understand what happened. There are two columns in the SELECT clause, Position_Title and a summary function, count(*), which counts total number of rows for each position group since we specify Position_Title in the GROUP BY clause. Unlike the programs in the previous example, the expression used inside WHERE and HAVING references another column, Department, which is not in the SELECT clause. Therefore, SAS handles them differently in the two programs.
\n", "The first program uses the WHERE clause. Since SAS processes the WHERE clause before SELECT and on a row-by-row basis, the records from Police department are selected from the data first. Then SAS counts the number of employees under each position title inside the department. For example, there is only one person who is a “CLINICAL THERAPIST III” in the Police Department. So the count is 1. We obtained the desired output.
\n", "
On the other hand, the second program uses the HAVING clause. It is equivalent to the following program but without Department column in the output:
\n", "Position Title | \n", "Department | \n", "Employees | \n", "
---|---|---|
ACCOUNTANT II | \n", "POLICE | \n", "1 | \n", "
CLINICAL THERAPIST III | \n", "POLICE | \n", "2 | \n", "
CROSSING GUARD | \n", "POLICE | \n", "5 | \n", "
CROSSING GUARD | \n", "POLICE | \n", "5 | \n", "
CROSSING GUARD | \n", "POLICE | \n", "5 | \n", "
CROSSING GUARD | \n", "POLICE | \n", "5 | \n", "
CROSSING GUARD | \n", "POLICE | \n", "5 | \n", "
CROSSING GUARD - PER AGREEMENT | \n", "POLICE | \n", "3 | \n", "
CROSSING GUARD - PER AGREEMENT | \n", "POLICE | \n", "3 | \n", "
CROSSING GUARD - PER AGREEMENT | \n", "POLICE | \n", "3 | \n", "
DIR OF POLICE RECORDS | \n", "POLICE | \n", "1 | \n", "
FISCAL ADMINISTRATOR | \n", "POLICE | \n", "1 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER | \n", "POLICE | \n", "85 | \n", "
POLICE OFFICER (ASSIGNED AS DETECTIVE) | \n", "POLICE | \n", "13 | \n", "
POLICE OFFICER (ASSIGNED AS DETECTIVE) | \n", "POLICE | \n", "13 | \n", "
POLICE OFFICER (ASSIGNED AS DETECTIVE) | \n", "POLICE | \n", "13 | \n", "
POLICE OFFICER (ASSIGNED AS DETECTIVE) | \n", "POLICE | \n", "13 | \n", "
POLICE OFFICER (ASSIGNED AS DETECTIVE) | \n", "POLICE | \n", "13 | \n", "
POLICE OFFICER (ASSIGNED AS DETECTIVE) | \n", "POLICE | \n", "13 | \n", "
POLICE OFFICER (ASSIGNED AS DETECTIVE) | \n", "POLICE | \n", "13 | \n", "
POLICE OFFICER (ASSIGNED AS DETECTIVE) | \n", "POLICE | \n", "13 | \n", "
POLICE OFFICER (ASSIGNED AS DETECTIVE) | \n", "POLICE | \n", "13 | \n", "
POLICE OFFICER (ASSIGNED AS DETECTIVE) | \n", "POLICE | \n", "13 | \n", "
POLICE OFFICER (ASSIGNED AS DETECTIVE) | \n", "POLICE | \n", "13 | \n", "
POLICE OFFICER (ASSIGNED AS DETECTIVE) | \n", "POLICE | \n", "13 | \n", "
POLICE OFFICER (ASSIGNED AS DETECTIVE) | \n", "POLICE | \n", "13 | \n", "
POLICE OFFICER / FLD TRNG OFFICER | \n", "POLICE | \n", "1 | \n", "
SENIOR DATA ENTRY OPERATOR | \n", "POLICE | \n", "4 | \n", "
SENIOR DATA ENTRY OPERATOR | \n", "POLICE | \n", "4 | \n", "
SERGEANT | \n", "POLICE | \n", "11 | \n", "
SERGEANT | \n", "POLICE | \n", "11 | \n", "
SERGEANT | \n", "POLICE | \n", "11 | \n", "
SERGEANT | \n", "POLICE | \n", "11 | \n", "
SERGEANT | \n", "POLICE | \n", "11 | \n", "
SERGEANT | \n", "POLICE | \n", "11 | \n", "
SERGEANT | \n", "POLICE | \n", "11 | \n", "
SERGEANT | \n", "POLICE | \n", "11 | \n", "
SERGEANT | \n", "POLICE | \n", "11 | \n", "
SERGEANT | \n", "POLICE | \n", "11 | \n", "
SERGEANT | \n", "POLICE | \n", "11 | \n", "
In this program, SAS counts employee numbers on each position across all departments because of GROUP BY clause. For example, there is each one person titled “CLINICAL THERAPIST III” in POLICE department and HEALTH department. So the total count on this position is 2. Since there is an extra column in SELECT clause besides the summary function and a GROUP BY column, all rows are in the output with counts on each job position. For instance, under position title “CLINICAL THERAPIST III”, both records have 2 as value of “Employees”. At last, SAS evaluates the condition (Department=POLICE) in HAVING clause to select rows for the output. That’s why you see Employees=2 for position title “CLINICAL THERAPIST III” in the output from the second query.
\n", "We have seen two examples that show the differences between HAVING and WHERE so far. Since SAS handles them so differently, when it comes to WHERE or HAVING, pick one that fits your needs the best.
\n", "Last but not the least, let’s check out one more cool feature of HAVING clause.
\n", "The following program selects the departments whose average salary is lower than the overall salary level:
\n", "Department | \n", "Avg_salary | \n", "
---|---|
DISABILITIES | \n", "$36,264.00 | \n", "
OEMC | \n", "$49,116.80 | \n", "
FAMILY & SUPPORT | \n", "$53,642.00 | \n", "
PUBLIC LIBRARY | \n", "$59,030.00 | \n", "
GENERAL SERVICES | \n", "$65,600.80 | \n", "
BUSINESS AFFAIRS | \n", "$65,652.00 | \n", "
CITY COUNCIL | \n", "$66,983.00 | \n", "
AVIATION | \n", "$67,704.48 | \n", "
STREETS & SAN | \n", "$68,625.08 | \n", "
LAW | \n", "$71,082.20 | \n", "
BOARD OF ELECTION | \n", "$74,988.00 | \n", "
HEALTH | \n", "$75,066.86 | \n", "
Going through this program, you may not find anything unusual until HAVING clause. Inside the clause it’s not a standard expression as before, but a query:
\n", "(select avg(Employee_annual_salary) from stat482.salary)
\n",
" Such kind of query is called subquery, inner query or nested query. You can use this query-expression in a HAVING or WHERE clause. The subquery used in this example is to calculate the overall average salary. The result is compared with average salaries of each department. Then SAS evaluates the condition “Less than” in HAVING clause to select departments who have less average salaries to output.
\n", "Launch and run the SAS program, and review the query result. Convince yourself that the departments’ information has been selected as described.
\n", "id | \n", "\t\t\tGender | \n", "\t\t\tGPA | \n", "\t\t\tSmokeCig | \n", "\t\t\tSATM | \n", "\t\t\tSATV | \n", "\t\t
---|---|---|---|---|---|
1001 | \n", "\t\t\tMale | \n", "\t\t\t2.67 | \n", "\t\t\tNo | \n", "\t\t\t700 | \n", "\t\t\t700 | \n", "\t\t
1002 | \n", "\t\t\tFemale | \n", "\t\t\t2.98 | \n", "\t\t\tNo | \n", "\t\t\t700 | \n", "\t\t\t500 | \n", "\t\t
1003 | \n", "\t\t\tFemale | \n", "\t\t\t2.67 | \n", "\t\t\tNo | \n", "\t\t\t470 | \n", "\t\t\t470 | \n", "\t\t
1004 | \n", "\t\t\tFemale | \n", "\t\t\t3.6 | \n", "\t\t\tNo | \n", "\t\t\t710 | \n", "\t\t\t560 | \n", "\t\t
1005 | \n", "\t\t\tFemale | \n", "\t\t\t3.76 | \n", "\t\t\tNo | \n", "\t\t\t600 | \n", "\t\t\t520 | \n", "\t\t
1006 | \n", "\t\t\tMale | \n", "\t\t\t3.86 | \n", "\t\t\tNo | \n", "\t\t\t610 | \n", "\t\t\t720 | \n", "\t\t
1007 | \n", "\t\t\tMale | \n", "\t\t\t3.94 | \n", "\t\t\tNo | \n", "\t\t\t710 | \n", "\t\t\t670 | \n", "\t\t
1008 | \n", "\t\t\tMale | \n", "\t\t\t2.8 | \n", "\t\t\tYes | \n", "\t\t\t610 | \n", "\t\t\t580 | \n", "\t\t
1009 | \n", "\t\t\tMale | \n", "\t\t\t3.48 | \n", "\t\t\tNo | \n", "\t\t\t690 | \n", "\t\t\t620 | \n", "\t\t
id | \n", "\t\t\tSeating | \n", "\t\t\tDriverInfluen | \n", "\t\t\tHeight | \n", "\t\t\tWeight | \n", "\t\t
---|---|---|---|---|
1001 | \n", "\t\t\tMiddle | \n", "\t\t\tNo | \n", "\t\t\t68 | \n", "\t\t\t190 | \n", "\t\t
1002 | \n", "\t\t\tMiddle | \n", "\t\t\tNo | \n", "\t\t\t54 | \n", "\t\t\t110 | \n", "\t\t
1003 | \n", "\t\t\tMiddle | \n", "\t\t\tNo | \n", "\t\t\t65 | \n", "\t\t\t225 | \n", "\t\t
1004 | \n", "\t\t\tMiddle | \n", "\t\t\tNo | \n", "\t\t\t52 | \n", "\t\t\t135 | \n", "\t\t
1005 | \n", "\t\t\tBack | \n", "\t\t\tNo | \n", "\t\t\t72 | \n", "\t\t\t128 | \n", "\t\t
1006 | \n", "\t\t\tMiddle | \n", "\t\t\tNo | \n", "\t\t\t70 | \n", "\t\t\t188 | \n", "\t\t
1007 | \n", "\t\t\tBack | \n", "\t\t\tNo | \n", "\t\t\t70 | \n", "\t\t\t155 | \n", "\t\t
1008 | \n", "\t\t\tMiddle | \n", "\t\t\tYes | \n", "\t\t\t68 | \n", "\t\t\t160 | \n", "\t\t
1009 | \n", "\t\t\tFront | \n", "\t\t\tNo | \n", "\t\t\t72 | \n", "\t\t\t160 | \n", "\t\t
The following program attempts to get demographic information about students from two separate tables, survey and survey2:
\n", "423 ods listing close;ods html5 (id=saspy_internal) file=stdout options(bitmap_mode='inline') device=svg style=HTMLBlue; ods
423! graphics on / outputfmt=png;
NOTE: Writing HTML5(SASPY_INTERNAL) Body file: STDOUT
424
425 PROC SQL;
426 create table demo_info as
427 select ID, Gender, Height, Weight
428 from phc6089.survey, phc6089.survey2;
NOTE: Data file PHC6089.SURVEY.DATA is in a format that is native to another host, or the file encoding does not match the session
encoding. Cross Environment Data Access will be used, which might require additional CPU resources and might reduce
performance.
NOTE: Data file PHC6089.SURVEY2.DATA is in a format that is native to another host, or the file encoding does not match the session
encoding. Cross Environment Data Access will be used, which might require additional CPU resources and might reduce
performance.
ERROR: Ambiguous reference, column ID is in more than one table.
NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.
429 QUIT;
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE SQL used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
430
431 ods html5 (id=saspy_internal) close;ods listing;
432
Let’s review the code. In this SQL procedure, we used the CREATE TABLE clause to save and name the new table as demo_info. The subsequent SELECT clause chooses ID, gender, height and weight columns from two tables. In FROM clause, two tables’ names are listed.
\n", "Launch and run the SAS program. You should expect no result in the output window because the CREATE TABLE clause suppresses output. On the other hand, check the log window and you will find the error message: “Ambiguous reference, column ID is in more than one table”.
\n", "As you observed two tables, ID is in both tables and contains the same information. If a column in the SELECT statement appears in multiple tables, the table it is chosen from has to be specified by adding the table’s name in front as this:
\n", "Table.Column
\n",
" So to make it right, we revise the previous program a little bit: change ID to survey.ID, which means that we use ID from survey data. The other change is the tables’ names. You can give a table an alias with or without the keyword AS after its original name. In the following program, we use S1 for survey data and S2 to survey2 data. And as you can see, it’s okay to use one level alias even for a permanent file. This makes life easier! In this way, ID can be specified as S1.ID.
\n", "434 ods listing close;ods html5 (id=saspy_internal) file=stdout options(bitmap_mode='inline') device=svg style=HTMLBlue; ods
434! graphics on / outputfmt=png;
NOTE: Writing HTML5(SASPY_INTERNAL) Body file: STDOUT
435
436 PROC SQL;
437 create table demo_info as
438 select s1.ID, Gender, Height, Weight
439 from phc6089.survey as s1, phc6089.survey2 as s2;
NOTE: Data file PHC6089.SURVEY.DATA is in a format that is native to another host, or the file encoding does not match the session
encoding. Cross Environment Data Access will be used, which might require additional CPU resources and might reduce
performance.
NOTE: Data file PHC6089.SURVEY2.DATA is in a format that is native to another host, or the file encoding does not match the session
encoding. Cross Environment Data Access will be used, which might require additional CPU resources and might reduce
performance.
NOTE: The execution of this query involves performing one or more Cartesian product joins that can not be optimized.
NOTE: Table WORK.DEMO_INFO created, with 51076 rows and 4 columns.
440 QUIT;
NOTE: PROCEDURE SQL used (Total process time):
real time 0.03 seconds
cpu time 0.01 seconds
441
442 ods html5 (id=saspy_internal) close;ods listing;
443
Everything seems good. Now launch and run the SAS program. As before, there is no output because of the CREATE TABLE statement. Check the log file in which there are two notes that need your attention (see the last two notes above).
\n", "The first is “The execution of this query involves performing one or more Cartesian product joins that can not be optimized”. What is a Cartesian product? It refers to a query result in which each row in the first table is combined with every row in the second table. If you specify multiple tables in FROM clause but do not use a WHERE clause to choose needed rows, a Cartesian product is generated. For example, if we submit the following program:
\n", "\n",
"PROC SQL;\n",
"Select *\n",
"from table1, table2;\n",
"
\n",
" Table1 has 3 rows; Table2 has 3 rows as well. Their Cartesian product contains (3*3)9 rows.
\n", "\n", "Table1
\n", "\n", "name | \n", "\t\t\tvalue1 | \n", "\t\t
---|---|
x | \n", "\t\t\t1 | \n", "\t\t
y | \n", "\t\t\t2 | \n", "\t\t
z | \n", "\t\t\t3 | \n", "\t\t
Table2
\n", "\n", "name | \n", "\t\t\tvalue2 | \n", "\t\t
---|---|
A | \n", "\t\t\t4 | \n", "\t\t
B | \n", "\t\t\t5 | \n", "\t\t
C | \n", "\t\t\t6 | \n", "\t\t
Result:
\n", "\n", "name | \n", "\t\t\tvalue1 | \n", "\t\t\tname | \n", "\t\t\tvalue2 | \n", "\t\t
---|---|---|---|
x | \n", "\t\t\t1 | \n", "\t\t\tA | \n", "\t\t\t4 | \n", "\t\t
x | \n", "\t\t\t1 | \n", "\t\t\tB | \n", "\t\t\t5 | \n", "\t\t
x | \n", "\t\t\t1 | \n", "\t\t\tC | \n", "\t\t\t6 | \n", "\t\t
y | \n", "\t\t\t2 | \n", "\t\t\tA | \n", "\t\t\t4 | \n", "\t\t
y | \n", "\t\t\t2 | \n", "\t\t\tB | \n", "\t\t\t5 | \n", "\t\t
y | \n", "\t\t\t2 | \n", "\t\t\tC | \n", "\t\t\t6 | \n", "\t\t
z | \n", "\t\t\t3 | \n", "\t\t\tA | \n", "\t\t\t4 | \n", "\t\t
z | \n", "\t\t\t3 | \n", "\t\t\tB | \n", "\t\t\t5 | \n", "\t\t
z | \n", "\t\t\t3 | \n", "\t\t\tC | \n", "\t\t\t6 | \n", "\t\t
In the program for this example, there is no WHERE clause. So SAS generated a Cartesian product and gave you the note. Both Survey and Survey2 have 226 rows in the table. The query should have (226*226) = 51076 rows as the result. That’s why you got the other note, “Table Work.demo_info created, with 51076 rows and 4 columns.” Clearly, this can’t be correct. How do we get the desired result? Let’s make a final push.
\n", "The following program selects the demographic information of students (ID, gender, height and weight) from two tables, survey and survey2:
\n", "id | \n", "Gender | \n", "Height | \n", "Weight | \n", "
---|---|---|---|
1001 | \n", "Male | \n", "67 | \n", "190 | \n", "
1002 | \n", "Female | \n", "54 | \n", "110 | \n", "
1003 | \n", "Female | \n", "65 | \n", "225 | \n", "
1004 | \n", "Female | \n", "52 | \n", "135 | \n", "
1005 | \n", "Female | \n", "72 | \n", "128 | \n", "
1006 | \n", "Male | \n", "70 | \n", "188 | \n", "
1007 | \n", "Male | \n", "70 | \n", "155 | \n", "
1008 | \n", "Male | \n", "68 | \n", "160 | \n", "
1009 | \n", "Male | \n", "72 | \n", "160 | \n", "
Let’s check through the code. Only one more clause has been added to the query, WHERE. We use the WHERE clause to subset the whole Cartesian product by only selecting the rows with matched ID numbers. Note that the column names in the WHERE clause do not have to be the same. At last, to be able to check the table in person, another query is added to display the data in the output window.
\n", "Launch and run the SAS program, and review the log file and the output.
\n", "NOTE: Table WORK.DEMO_INFO creates, with 226 rows and 4 columns.\n", "
Finally, we got what we want. As you can see from the query result, it’s like combining two columns from each table horizontally. SAS also call it join. In this particular case, since we only chose the matched rows, it’s also called the inner join. Such type of join is very similar to Merge By in the DATA step but requiring less computing resources and less coding. There are other types of join and data union (a vertical combination of rows) in PROC SQL which are beyond this lesson’s scope. If you are interested, you can explore them yourself with the foundation of this lesson!
\n", "