proc transpose data=raw out=temp prefix=obs name=var_name;
var _all_;
run;
proc means data=temp noprint;
output out=temp_median(drop=_type_ _freq_) median=;
run;
data temp;
set temp temp_median(in=flag);
if flag then var_name='median';
run;
proc transpose data=temp out=temp(drop=_name_);
var obs:;
id var_name;
run;[/code:p8kqngna]作者: shiyiming 时间: 2009-12-25 08:13 标题: Re: 如何在数据步求出多个变量的中位数? Excellent solution by hopewell. Here is another approach in case you really need it done through data steps:
data median(keep = median);
set raw;
array raw var1-var5;
do i = 1 to 5;
do j = 1 to i;
if raw(i) > raw(j) then do;
temp = raw(i);
raw(i) = raw(j);
raw(j) = temp;
end;
end;
end;
median = raw(3);
run;
data results;
merge raw median;
run;[/code:4i9g5qbk]作者: shiyiming 时间: 2009-12-25 11:42 标题: Re: 如何在数据步求出多个变量的中位数? [code:wf1pojun]data a;
input var1 var2 var3 var4 var5;
cards;
1 2 3 4 5
9 8 7 6 0
11 22 88 99 25
;
/*take advantage of odd number of variables*/
/*also account for missing values */
data _null_;
set a;
array var(5);
call sortn(of var(*));
put 'median =' var(3);
run;[/code:wf1pojun]作者: shiyiming 时间: 2009-12-26 03:31 标题: Re: 如何在数据步求出多个变量的中位数? Thanks jingju11! Just wondering if there is a quick way to both take advantage of the call sortn routine and have the results in a dataset as well. <!-- s:) --><img src="{SMILIES_PATH}/icon_smile.gif" alt=":)" title="Smile" /><!-- s:) -->作者: shiyiming 时间: 2009-12-30 17:16 标题: Re: 如何在数据步求出多个变量的中位数? to hopewell
谢谢hopewell的方法,这个方法虽然需要两次转秩但是很经典,非常感谢!作者: shiyiming 时间: 2009-12-30 17:19 标题: Re: 如何在数据步求出多个变量的中位数? to cloudpan2002