Sorting SAS 9.3按行排序数据
我有一个导入SAS的excel文件,其中包含3个变量和3个观察值 所有值都是数字Sorting SAS 9.3按行排序数据,sorting,sas,Sorting,Sas,我有一个导入SAS的excel文件,其中包含3个变量和3个观察值 所有值都是数字 241247 993014 50541 是否有一种编码方法可以使每一行按升序排序 结果将是: 122447 143099 54150 我需要对几个包含大量变量和观察值的excel文件执行此操作。 谢谢。我将使用FCMP排序例程。FCMP函数和子例程只允许将临时数组传递给它们进行修改。因此,必须将值分配到临时数组中,进行排序,然后重新分配给永久变量 根据列数和列名修改下面的代码 options cmplib=work
24
12
47
99
30
14
50
5
41
是否有一种编码方法可以使每一行按升序排序
结果将是:
12
24
47
14
30
99
5
41
50
我需要对几个包含大量变量和观察值的excel文件执行此操作。
谢谢。我将使用FCMP排序例程。FCMP函数和子例程只允许将临时数组传递给它们进行修改。因此,必须将值分配到临时数组中,进行排序,然后重新分配给永久变量 根据列数和列名修改下面的代码
options cmplib=work.cmp;
proc fcmp outlib=work.cmp.fns;
subroutine qsort(arr[*],lo,hi);
outargs arr;
i = lo;
j = hi;
do while (i < hi);
pivot = arr[floor((lo+hi)/2)];
do while (i<=j);
do while (arr[i] < pivot);
i = i + 1;
end;
do while (arr[j] > pivot);
j = j - 1;
end;
if (i<=j) then do;
t = arr[i];
arr[i] = arr[j];
arr[j] = t;
i = i + 1;
j = j - 1;
end;
end;
if (lo < j) then
call qsort(arr,lo,j);
lo = i;
j = hi;
end;
endsub;
run;
quit;
data test;
input a b c;
datalines;
24 12 47
99 30 14
50 5 41
;
run;
%let ncol=3;
%let cols = a b c;
data sorted;
set test;
array vars[&ncol] &cols;
/*Only temporary arrays can be passed to FCMP functions*/
array tmp[&ncol] _temporary_;
/*Assign to tmp*/
do i=1 to &ncol;
tmp[i] = vars[i];
end;
/*Sort*/
call qsort(tmp,1,&ncol);
/*Put back sorted values*/
do i=1 to &ncol;
vars[i] = tmp[i];
end;
drop i;
run;
options cmplib=work.cmp;
proc fcmp outlib=work.cmp.fns;
子程序qsort(arr[*],lo,hi);
外来者;
i=lo;
j=高;
做一会儿(我<你好);
枢轴=arr[地板((低+高)/2)];
边走边做(我转动);
j=j-1;
结束;
如果(i我将使用FCMP排序例程。FCMP函数和子例程只允许将临时数组传递给它们进行修改。因此,必须将值分配到临时数组中,进行排序,然后重新分配给永久变量
根据列数和列名修改下面的代码
options cmplib=work.cmp;
proc fcmp outlib=work.cmp.fns;
subroutine qsort(arr[*],lo,hi);
outargs arr;
i = lo;
j = hi;
do while (i < hi);
pivot = arr[floor((lo+hi)/2)];
do while (i<=j);
do while (arr[i] < pivot);
i = i + 1;
end;
do while (arr[j] > pivot);
j = j - 1;
end;
if (i<=j) then do;
t = arr[i];
arr[i] = arr[j];
arr[j] = t;
i = i + 1;
j = j - 1;
end;
end;
if (lo < j) then
call qsort(arr,lo,j);
lo = i;
j = hi;
end;
endsub;
run;
quit;
data test;
input a b c;
datalines;
24 12 47
99 30 14
50 5 41
;
run;
%let ncol=3;
%let cols = a b c;
data sorted;
set test;
array vars[&ncol] &cols;
/*Only temporary arrays can be passed to FCMP functions*/
array tmp[&ncol] _temporary_;
/*Assign to tmp*/
do i=1 to &ncol;
tmp[i] = vars[i];
end;
/*Sort*/
call qsort(tmp,1,&ncol);
/*Put back sorted values*/
do i=1 to &ncol;
vars[i] = tmp[i];
end;
drop i;
run;
options cmplib=work.cmp;
proc fcmp outlib=work.cmp.fns;
子程序qsort(arr[*],lo,hi);
外来者;
i=lo;
j=高;
做一会儿(我<你好);
枢轴=arr[地板((低+高)/2)];
边走边做(我转动);
j=j-1;
结束;
如果(我尽管有一个专门为矩阵操作设计的软件包SAS/IML(我相信,这项任务很简单),它仍然可以通过SAS Base使用封装在宏循环中的两个过程来完成
data raw;
input a b c;
datalines;
24 12 47
99 30 14
50 5 41
;
run;
proc transpose data=raw out=raw_t(drop=_:); run;
proc sql noprint;
select name into :vars separated by ' '
from sashelp.vcolumn
where libname='WORK' and memname='RAW_T';
quit;
%macro sort_rows;
%do i=1 %to %sysfunc(countw(&vars));
proc sort data=raw_t(keep=%scan(&vars,&i)) out=column;
by %scan(&vars,&i);
run;
data sortedrows;
%if &i>1 %then set sortedrows;;
set column;
run;
%end;
%mend sort_rows;
%sort_rows
proc transpose data=sortedrows out=sortedrows(drop=_:); run;
首先,转置原始数据集
然后逐个遍历所有列(最初是行),对它们进行排序并相互右键连接
最后,把所有的东西都调回原处。虽然有一个专门为矩阵操作设计的SAS/IML包(我相信,这项任务很简单),但它仍然可以通过SAS Base使用封装在宏循环中的几个过程来完成
data raw;
input a b c;
datalines;
24 12 47
99 30 14
50 5 41
;
run;
proc transpose data=raw out=raw_t(drop=_:); run;
proc sql noprint;
select name into :vars separated by ' '
from sashelp.vcolumn
where libname='WORK' and memname='RAW_T';
quit;
%macro sort_rows;
%do i=1 %to %sysfunc(countw(&vars));
proc sort data=raw_t(keep=%scan(&vars,&i)) out=column;
by %scan(&vars,&i);
run;
data sortedrows;
%if &i>1 %then set sortedrows;;
set column;
run;
%end;
%mend sort_rows;
%sort_rows
proc transpose data=sortedrows out=sortedrows(drop=_:); run;
首先,转置原始数据集
然后逐个遍历所有列(最初是行),对它们进行排序并相互右键连接
最后,将所有内容调回原处。最简单的方法是使用CALL SORTN跨行排序
data have;
input a b c;
datalines;
24 12 47
99 30 14
50 5 41
;
run;
data have;
modify have;
call sortn(of _numeric_);
run;
简单的方法是使用跨行排序的callsortn
data have;
input a b c;
datalines;
24 12 47
99 30 14
50 5 41
;
run;
data have;
modify have;
call sortn(of _numeric_);
run;