[英]Pass argument to a SAS function using a SAS variable
I want to calculate the maximum of a list of SAS variables, where the list is determined by another variable present in the dataset. 我想计算SAS变量列表的最大值,该列表由数据集中存在的另一个变量确定。 That is, 那是,
| var_1 | var_2 | var_3 | var_4 | maximum till | formula used | var_output |
|-------|-------|-------|-------|--------------|----------------------|------------|
| 3 | 6 | 9 | 12 | 4 | =max(of var_1-var_4) | 12 |
| 1 | 10 | 100 | 1000 | 2 | =max(of var_1-var_2) | 10 |
| 5 | 15 | 25 | 35 | 3 | =max(of var_1-var_3) | 25 |
Appreciate any help. 感谢任何帮助。 Thanks :) 谢谢 :)
Use a do loop and a rolling maximum: 使用do循环和滚动最大值:
data want;
set have;
array vars{4} var1-var4;
do i = 1 to max_till;
var_out = max(vars{i},var_out);
end;
run;
The FCMP solution. FCMP解决方案。 This is similar to user667489's solution, but implemented as a function. 这与user667489的解决方案相似,但已实现为功能。 This will only work in 9.4, and possibly only 9.4 TS1M0+. 仅在9.4 TS1M0 +中有效,并且可能仅在9.4中起作用。
data have; *some data;
input var1-var4 varmax;
datalines;
3 6 9 12 4
1 10 100 1000 2
5 15 25 35 3
;;;;
run;
proc fcmp outlib=work.funcs.func; *store functions here;
function maxof(mxarr[*],maxlim); *returns numeric;
do _i = 1 to maxlim;
_max = max(mxarr[_i],_max);
end;
return(_max);
endsub;
run;
options cmplib=work.funcs; *define where functions come from;
data want;
set have;
array vars var1-var4;
varout = maxof(vars,varmax); *use function (pass array by reference);
run;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.