如何获取表的所有列的最小值和最大值?

时间:2015-08-07 20:11:11

标签: sas

如何获取表格所有列的最小值和最大值?请注意,列可以是数字,日期或字符。我们必须以下列格式找到所有变量的最小值和最大值: Name_of_columns,最小值,最大值

2 个答案:

答案 0 :(得分:1)

这是一个宏,可以满足您的要求,不需要您知道变量名称或类型:

%macro maxmin;
/* get variable names */
proc contents noprint data = test.hashval out=test.contents;run;

proc sql noprint;
select count(*) into: cnt from test.contents;quit;

%let cnt = &cnt;

proc sql noprint;
select name into: name1 - : name&cnt from test.contents;quit;

/* get length of all variable names and results */
proc delete data = test.results; run;
%let name_len = 0;
%let max_len = 0;
%let min_len = 0;

%do i = 1 %to &cnt;
    proc sql noprint;
         select max(&&name&i),min(&&name&i) into: max&i, :min&i from test.hashval;quit;

    %let max&i = %cmpres(&&max&i);
    %let min&i = %cmpres(&&min&i);

    %if (&name_len < %length(&&name&i)) %then %let name_len = %length(&&name&i);
    %if (&max_len < %length(&&max&i)) %then %let max_len = %length(&&max&i);
    %if (&min_len < %length(&&min&i)) %then %let min_len = %length(&&min&i);

%end;

/*create results */
%do i = 1 %to &cnt;

   data temp;
   length NAME $&name_len MAX $&max_len MIN $&min_len;
   NAME = "&&name&i";
   MAX = "&&max&i";
   MIN = "&&min&i";
   run;

   proc append base = test.results data= temp force;run; 

%end;
%mend maxmin;
%maxmin;

答案 1 :(得分:0)

scope

根据评论者的要求,我用

测试了它
proc sql;
    create view myExtrema_1 as 
    Select min(alphaVar) as alphaVar, ..., put(min(numVar),best32.) as numVar, ...
    from myTable
    Union
    Select max(alphaVar), ..., put(max(numVar),best32.), ...
    from myTable;
quit;
proc transpose data=myExtrema_1
    out=myExtrema(rename=(
        _name_ = Variable
        col1 = Minimum
        col2 = Maximum
    ));
    var alphaVar ... numVar ...;
run;