32.4 - Sorting Data

32.4 - Sorting Data

The SELECT and FROM clauses are indispensable in the SELECT statement. Other clauses may be optional but very useful when querying a table. The last section introduced the WHERE clause and how to use it to select rows conditionally. From previous examples, you may have noticed that the order of observations might remain the same as they were in the original data set. If, however, you want to specify the order of data, you will need the ORDER BY clause to sort the data as you want.

Example 32.13

The following SAS program uses the ORDER BY in SELECT statement inside PROC SQL to sort the data in the file survey.sas7bdat by the values of gender and GPA:

PROC SQL;
	select ID,
		Gender,
        GPA,
		SATM,
		SATV
	from stat482.survey
	where SATV is not null and GPA>3
	order by Gender, GPA ;
QUIT;

id Gender GPA SATM SATV
1219 Female 3.01 630 590
1039 Female 3.02 560 560
1125 Female 3.08 470 510
1203 Female 3.1 500 610
1139 Female 3.1 530 600
1068 Female 3.1 550 610
1116 Female 3.1 560 550
1072 Female 3.1 570 570
1138 Female 3.12 560 580
1120 Female 3.16 680 670
1102 Female 3.2 620 630
1142 Female 3.2 720 500
1020 Female 3.2 600 630
1201 Female 3.21 760 660
1089 Female 3.25 500 600
1133 Female 3.27 450 550
1144 Female 3.27 640 600
1163 Female 3.3 600 590
1038 Female 3.3 680 650
1069 Female 3.3 600 490
1033 Female 3.3 650 600
1115 Female 3.3 580 620
1057 Female 3.3 600 600
1037 Female 3.3 500 400
1109 Female 3.31 500 490
1215 Female 3.33 650 630
1078 Female 3.33 570 490
1060 Female 3.36 540 550
1030 Female 3.36 450 450
1129 Female 3.4 500 540
1100 Female 3.4 490 520
1196 Female 3.41 480 560
1046 Female 3.42 660 580
1165 Female 3.45 560 500
1148 Female 3.46 620 640
1082 Female 3.48 550 690
1096 Female 3.48 750 550
1168 Female 3.5 650 560
1091 Female 3.5 800 650
1018 Female 3.5 600 750
1181 Female 3.5 600 550
1123 Female 3.51 560 530
1177 Female 3.53 520 500
1063 Female 3.53 560 590
1015 Female 3.55 400 600
1058 Female 3.55 585 590
1160 Female 3.57 700 700
1159 Female 3.59 640 440
1202 Female 3.6 460 540
1004 Female 3.6 710 560
1086 Female 3.6 540 570
1035 Female 3.61 500 550
1187 Female 3.62 780 660
1149 Female 3.63 640 700
1110 Female 3.63 700 600
1011 Female 3.67 690 690
1044 Female 3.7 570 560
1145 Female 3.7 500 450
1207 Female 3.7 640 670
1052 Female 3.7 600 620
1218 Female 3.71 540 560
1174 Female 3.74 680 600
1118 Female 3.74 650 700
1031 Female 3.75 640 620
1073 Female 3.76 600 550
1005 Female 3.76 600 520
1055 Female 3.76 760 600
1204 Female 3.77 650 630
1199 Female 3.77 620 640
1134 Female 3.78 650 600
1208 Female 3.78 550 400
1010 Female 3.8 580 540
1197 Female 3.8 600 700
1205 Female 3.8 550 550
1077 Female 3.81 560 610
1105 Female 3.81 510 680
1179 Female 3.83 660 660
1023 Female 3.88 670 680
1094 Female 3.89 640 710
1067 Female 3.9 640 560
1065 Female 3.9 575 600
1081 Female 3.94 620 600
1014 Female 4 700 700
1075 Female 4 650 550
1214 Female 4 590 500
1157 Male 3.02 400 400
1047 Male 3.02 700 570
1084 Male 3.03 690 690
1140 Male 3.04 540 600
1152 Male 3.05 600 500
1130 Male 3.06 660 620
1151 Male 3.08 590 590
1053 Male 3.08 420 490
1097 Male 3.1 640 430
1137 Male 3.1 580 640
1090 Male 3.12 560 330
1185 Male 3.13 600 600
1101 Male 3.13 570 490
1209 Male 3.14 580 580
1041 Male 3.16 620 760
1220 Male 3.19 480 480
1153 Male 3.2 550 630
1071 Male 3.2 650 660
1119 Male 3.2 640 630
1045 Male 3.2 680 710
1217 Male 3.21 620 400
1200 Male 3.21 650 590
1222 Male 3.24 510 680
1175 Male 3.3 640 600
1170 Male 3.3 610 590
1016 Male 3.3 640 600
1095 Male 3.33 690 650
1212 Male 3.35 570 480
1098 Male 3.36 540 520
1027 Male 3.36 550 600
1017 Male 3.38 720 580
1087 Male 3.4 580 570
1161 Male 3.4 600 630
1036 Male 3.42 530 760
1221 Male 3.42 590 610
1194 Male 3.46 640 600
1009 Male 3.48 690 620
1029 Male 3.5 680 680
1048 Male 3.51 650 590
1024 Male 3.51 600 730
1034 Male 3.53 670 630
1206 Male 3.53 620 630
1111 Male 3.54 540 570
1150 Male 3.55 630 650
1106 Male 3.57 620 500
1042 Male 3.66 640 640
1169 Male 3.67 650 670
1019 Male 3.7 600 720
1080 Male 3.72 660 660
1079 Male 3.72 700 640
1059 Male 3.72 800 750
1173 Male 3.73 580 590
1166 Male 3.74 700 520
1070 Male 3.76 600 610
1062 Male 3.76 670 670
1226 Male 3.78 630 520
1162 Male 3.83 710 710
1006 Male 3.86 610 720
1180 Male 3.86 720 500
1172 Male 3.87 780 580
1122 Male 3.88 670 510
1131 Male 3.92 730 800
1007 Male 3.94 710 670
1028 Male 4 610 600

Download survey data to your own computer and change the directory if needed to reflect the local directory where you saved the file. Then 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.

Several things need to be pointed out regarding the above program:

  1. You can use one or more columns in ORDER BY to sort the data. A comma is used to separate multiple column names. In this example, two columns have been used, Gender and GPA. So the data will be sorted by Gender first, then by GPA in order.
  2. By default, the values of column(s) will be sorted ascendingly. For example, there are two values in Gender, Female, and Male. In the query result, Female records are listed first, then male ones because SAS sorted them by the first letter in alphabetical order. As to GPA order, since it’s numeric, SAS sorted observations by number values of GPA inside each gender group.
  3. The WHERE clause is used to select observations that her/his SAT verbal score is not missing and GPA greater than 3. “is not null” and “is not missing” are interchangeable to indicate no missing values included.

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.

Example 32.14

The following SAS program sorts the data survey.sas7bdat by the values of gender in descending order and then by GPA ascendingly:

PROC SQL;
	select ID,
			Gender,
			GPA,
			SATM,
			SATV
	from stat482.survey
	where SATV is not null and GPA>3
	order by Gender desc, 3 ;
QUIT;

id Gender GPA SATM SATV
1047 Male 3.02 700 570
1157 Male 3.02 400 400
1084 Male 3.03 690 690
1140 Male 3.04 540 600
1152 Male 3.05 600 500
1130 Male 3.06 660 620
1053 Male 3.08 420 490
1151 Male 3.08 590 590
1137 Male 3.1 580 640
1097 Male 3.1 640 430
1090 Male 3.12 560 330
1185 Male 3.13 600 600
1101 Male 3.13 570 490
1209 Male 3.14 580 580
1041 Male 3.16 620 760
1220 Male 3.19 480 480
1045 Male 3.2 680 710
1071 Male 3.2 650 660
1119 Male 3.2 640 630
1153 Male 3.2 550 630
1200 Male 3.21 650 590
1217 Male 3.21 620 400
1222 Male 3.24 510 680
1175 Male 3.3 640 600
1016 Male 3.3 640 600
1170 Male 3.3 610 590
1095 Male 3.33 690 650
1212 Male 3.35 570 480
1098 Male 3.36 540 520
1027 Male 3.36 550 600
1017 Male 3.38 720 580
1161 Male 3.4 600 630
1087 Male 3.4 580 570
1221 Male 3.42 590 610
1036 Male 3.42 530 760
1194 Male 3.46 640 600
1009 Male 3.48 690 620
1029 Male 3.5 680 680
1048 Male 3.51 650 590
1024 Male 3.51 600 730
1034 Male 3.53 670 630
1206 Male 3.53 620 630
1111 Male 3.54 540 570
1150 Male 3.55 630 650
1106 Male 3.57 620 500
1042 Male 3.66 640 640
1169 Male 3.67 650 670
1019 Male 3.7 600 720
1080 Male 3.72 660 660
1079 Male 3.72 700 640
1059 Male 3.72 800 750
1173 Male 3.73 580 590
1166 Male 3.74 700 520
1070 Male 3.76 600 610
1062 Male 3.76 670 670
1226 Male 3.78 630 520
1162 Male 3.83 710 710
1180 Male 3.86 720 500
1006 Male 3.86 610 720
1172 Male 3.87 780 580
1122 Male 3.88 670 510
1131 Male 3.92 730 800
1007 Male 3.94 710 670
1028 Male 4 610 600
1219 Female 3.01 630 590
1039 Female 3.02 560 560
1125 Female 3.08 470 510
1072 Female 3.1 570 570
1139 Female 3.1 530 600
1068 Female 3.1 550 610
1116 Female 3.1 560 550
1203 Female 3.1 500 610
1138 Female 3.12 560 580
1120 Female 3.16 680 670
1020 Female 3.2 600 630
1142 Female 3.2 720 500
1102 Female 3.2 620 630
1201 Female 3.21 760 660
1089 Female 3.25 500 600
1144 Female 3.27 640 600
1133 Female 3.27 450 550
1163 Female 3.3 600 590
1038 Female 3.3 680 650
1037 Female 3.3 500 400
1069 Female 3.3 600 490
1033 Female 3.3 650 600
1057 Female 3.3 600 600
1115 Female 3.3 580 620
1109 Female 3.31 500 490
1078 Female 3.33 570 490
1215 Female 3.33 650 630
1060 Female 3.36 540 550
1030 Female 3.36 450 450
1129 Female 3.4 500 540
1100 Female 3.4 490 520
1196 Female 3.41 480 560
1046 Female 3.42 660 580
1165 Female 3.45 560 500
1148 Female 3.46 620 640
1082 Female 3.48 550 690
1096 Female 3.48 750 550
1181 Female 3.5 600 550
1168 Female 3.5 650 560
1018 Female 3.5 600 750
1091 Female 3.5 800 650
1123 Female 3.51 560 530
1177 Female 3.53 520 500
1063 Female 3.53 560 590
1058 Female 3.55 585 590
1015 Female 3.55 400 600
1160 Female 3.57 700 700
1159 Female 3.59 640 440
1086 Female 3.6 540 570
1004 Female 3.6 710 560
1202 Female 3.6 460 540
1035 Female 3.61 500 550
1187 Female 3.62 780 660
1149 Female 3.63 640 700
1110 Female 3.63 700 600
1011 Female 3.67 690 690
1044 Female 3.7 570 560
1145 Female 3.7 500 450
1052 Female 3.7 600 620
1207 Female 3.7 640 670
1218 Female 3.71 540 560
1174 Female 3.74 680 600
1118 Female 3.74 650 700
1031 Female 3.75 640 620
1073 Female 3.76 600 550
1055 Female 3.76 760 600
1005 Female 3.76 600 520
1204 Female 3.77 650 630
1199 Female 3.77 620 640
1134 Female 3.78 650 600
1208 Female 3.78 550 400
1010 Female 3.8 580 540
1205 Female 3.8 550 550
1197 Female 3.8 600 700
1077 Female 3.81 560 610
1105 Female 3.81 510 680
1179 Female 3.83 660 660
1023 Female 3.88 670 680
1094 Female 3.89 640 710
1067 Female 3.9 640 560
1065 Female 3.9 575 600
1081 Female 3.94 620 600
1075 Female 4 650 550
1014 Female 4 700 700
1214 Female 4 590 500

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 we can use 3 to specify GPA.

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.

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.

Example 32.15

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:

PROC SQL;
	select *
	from stat482.survey
	where SATV is not null and GPA>3
	order by Gender desc, MEAN(SATM,SATV) ;
QUIT;

id Gender GPA SmokeCigarettes SATM SATV
1157 Male 3.02 No 400 400
1090 Male 3.12 No 560 330
1053 Male 3.08 No 420 490
1220 Male 3.19 No 480 480
1217 Male 3.21 No 620 400
1212 Male 3.35 No 570 480
1101 Male 3.13 No 570 490
1098 Male 3.36 No 540 520
1097 Male 3.1 No 640 430
1152 Male 3.05 No 600 500
1111 Male 3.54 No 540 570
1106 Male 3.57 No 620 500
1140 Male 3.04 Yes 540 600
1226 Male 3.78 No 630 520
1087 Male 3.4 No 580 570
1027 Male 3.36 No 550 600
1209 Male 3.14 No 580 580
1173 Male 3.73 No 580 590
1122 Male 3.88 No 670 510
1153 Male 3.2 No 550 630
1151 Male 3.08 No 590 590
1222 Male 3.24 No 510 680
1185 Male 3.13 No 600 600
1221 Male 3.42 Yes 590 610
1170 Male 3.3 No 610 590
1070 Male 3.76 No 600 610
1028 Male 4 No 610 600
1137 Male 3.1 No 580 640
1166 Male 3.74 No 700 520
1180 Male 3.86 No 720 500
1161 Male 3.4 No 600 630
1175 Male 3.3 Yes 640 600
1016 Male 3.3 No 640 600
1194 Male 3.46 No 640 600
1048 Male 3.51 No 650 590
1200 Male 3.21 No 650 590
1206 Male 3.53 No 620 630
1119 Male 3.2 No 640 630
1047 Male 3.02 No 700 570
1150 Male 3.55 No 630 650
1042 Male 3.66 No 640 640
1130 Male 3.06 No 660 620
1036 Male 3.42 No 530 760
1034 Male 3.53 No 670 630
1017 Male 3.38 No 720 580
1071 Male 3.2 No 650 660
1009 Male 3.48 No 690 620
1080 Male 3.72 No 660 660
1169 Male 3.67 No 650 670
1019 Male 3.7 No 600 720
1006 Male 3.86 No 610 720
1024 Male 3.51 No 600 730
1079 Male 3.72 No 700 640
1062 Male 3.76 No 670 670
1095 Male 3.33 No 690 650
1172 Male 3.87 Yes 780 580
1029 Male 3.5 No 680 680
1041 Male 3.16 No 620 760
1007 Male 3.94 No 710 670
1084 Male 3.03 No 690 690
1045 Male 3.2 No 680 710
1162 Male 3.83 No 710 710
1131 Male 3.92 No 730 800
1059 Male 3.72 No 800 750
1037 Female 3.3 No 500 400
1030 Female 3.36 No 450 450
1145 Female 3.7 No 500 450
1208 Female 3.78 No 550 400
1125 Female 3.08 No 470 510
1109 Female 3.31 No 500 490
1133 Female 3.27 No 450 550
1015 Female 3.55 No 400 600
1202 Female 3.6 No 460 540
1100 Female 3.4 No 490 520
1177 Female 3.53 No 520 500
1196 Female 3.41 No 480 560
1129 Female 3.4 No 500 540
1035 Female 3.61 No 500 550
1078 Female 3.33 No 570 490
1165 Female 3.45 No 560 500
1159 Female 3.59 No 640 440
1069 Female 3.3 No 600 490
1060 Female 3.36 No 540 550
1123 Female 3.51 No 560 530
1214 Female 4 No 590 500
1089 Female 3.25 Yes 500 600
1218 Female 3.71 No 540 560
1205 Female 3.8 No 550 550
1086 Female 3.6 No 540 570
1116 Female 3.1 No 560 550
1203 Female 3.1 No 500 610
1039 Female 3.02 No 560 560
1010 Female 3.8 No 580 540
1005 Female 3.76 No 600 520
1044 Female 3.7 No 570 560
1139 Female 3.1 No 530 600
1072 Female 3.1 No 570 570
1138 Female 3.12 No 560 580
1181 Female 3.5 No 600 550
1073 Female 3.76 No 600 550
1063 Female 3.53 No 560 590
1068 Female 3.1 No 550 610
1077 Female 3.81 No 560 610
1065 Female 3.9 No 575 600
1058 Female 3.55 No 585 590
1163 Female 3.3 No 600 590
1105 Female 3.81 No 510 680
1075 Female 4 No 650 550
1067 Female 3.9 No 640 560
1057 Female 3.3 No 600 600
1115 Female 3.3 No 580 620
1168 Female 3.5 No 650 560
1081 Female 3.94 No 620 600
1142 Female 3.2 No 720 500
1052 Female 3.7 No 600 620
1219 Female 3.01 No 630 590
1020 Female 3.2 No 600 630
1082 Female 3.48 No 550 690
1144 Female 3.27 No 640 600
1046 Female 3.42 No 660 580
1033 Female 3.3 No 650 600
1134 Female 3.78 No 650 600
1102 Female 3.2 No 620 630
1148 Female 3.46 No 620 640
1031 Female 3.75 No 640 620
1199 Female 3.77 No 620 640
1004 Female 3.6 No 710 560
1174 Female 3.74 No 680 600
1215 Female 3.33 No 650 630
1204 Female 3.77 No 650 630
1110 Female 3.63 No 700 600
1096 Female 3.48 No 750 550
1197 Female 3.8 No 600 700
1207 Female 3.7 No 640 670
1179 Female 3.83 No 660 660
1038 Female 3.3 No 680 650
1149 Female 3.63 No 640 700
1023 Female 3.88 No 670 680
1018 Female 3.5 No 600 750
1120 Female 3.16 No 680 670
1118 Female 3.74 No 650 700
1094 Female 3.89 No 640 710
1055 Female 3.76 No 760 600
1011 Female 3.67 Yes 690 690
1160 Female 3.57 No 700 700
1014 Female 4 No 700 700
1201 Female 3.21 Yes 760 660
1187 Female 3.62 No 780 660
1091 Female 3.5 No 800 650

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?

Launch and run the SAS program, and review the output to convince yourself that the data has been sorted in the desired order.

One more thing, you may notice a note in the log window when running this program.

NOTE: The query as specified involves ordering by an item that doesn't appear in its SELECT clause.

That’s because MEAN(SATM, SATV) is not listed in the SELECT clause, only in the ORDER BY clause.


Legend
[1]Link
Has Tooltip/Popover
 Toggleable Visibility