2013-12-11 40 views
0

我有一个导入到SAS的Excel文件,其中包含3个变量和3个观察值。SAS 9.3按行排序数据

所有的值都是数字。


241247
993014
50541

有没有一种方法,我可以代码,以便每次在升序排序?


结果将是:


122447
143099
54150


我需要为包含的变量和意见数量庞大的几个Excel文件做到这一点。
谢谢。

回答

4

简单的方法是使用跨行排序的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; 
0

我会使用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; 
0

虽然有一个包SAS/IML专门为矩阵(其中,我相信,这个任务将是微不足道的)操作设计的,它仍然可以使用SAS基地使用几个特效的包裹成宏循环中完成。

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; 

首先,你转置你的原始数据集。

然后,您逐个遍历所有列(最初是行),对它们进行排序并正确连接到对方。

最后,将所有东西都转回去。