# 在SAS数据集中排序

## 在SAS中排序

SAS中的排序是根据变量以升序或降序排列数据的方式排列数据值(或观察值)的过程。 SAS的默认排序顺序是升序, 但是我们也可以使用” by语句”以降序和根据我们的选择对数据值进行排序。在使用”按语句”时, SAS根据数据值的子集产生结果。变量的排序有助于更好的分析并提供最佳结果。

### 句法：

``````PROC sort data=<name of data>;
by <name of variable>;
run;``````

## SAS PROC默认排序(升序)

``````data one;
input studyid name\$ sex\$ age weight height\$;
datalines;
1 Akash m 22 64 120
6 Jaya f 55 63 125
2 Vinita f 34 68 130
7 Kumar m 43 64 130
4 Anita f 29 65 153
5 Taufik m 21 45 110
8 Boby m 32 71 155
3 Ankita f 40 69 120
;
run;
proc sort data=one;
by weight;
run;
/*will sort data one by the variable weight in ascending order */
proc print data=one;
run;``````

## 降序排列

### 句法：

``````proc sort data= dataset name;
by descending variable name;
run;
proc print data= dataset name ;
run;``````

``````data one;
input studyid name\$ sex\$ age weight height\$;
datalines;
1 Akash m 22 64 120
6 Jaya f 55 63 125
2 Vinita f 34 68 130
7 Kumar m 43 64 130
4 Anita f 29 65 153
5 Taufik m 21 45 110
8 Boby m 32 71 155
3 Ankita f 40 69 120
;
run;
proc sort data=one;
by descending age;
run;
proc print data=one;
run;``````

## 按语句排序

### 句法：

``````proc sort data=dataset name;
by variable name;
run;
proc print data= dataset name;
by variable name;
run;``````

``````data one;
input studyid name\$ sex\$ age weight height\$;
datalines;
1 Akash m 22 64 120
6 Jaya f 55 63 125
2 Vinita f 34 68 130
7 Kumar m 43 64 130
4 Anita f 29 65 153
5 Taufik m 21 45 110
8 Boby m 32 71 155
3 Ankita f 40 69 120
;
run;
proc sort data=one;
by sex;
run;
proc print data=one;
by sex;
run;``````

• 回顶