Vector 声明为SAS函数参数的向量

Vector 声明为SAS函数参数的向量,vector,sas,cut,Vector,Sas,Cut,我目前正在学习SAS,已经知道R和其他编程语言,我对如何声明向量感到困惑 我创建了一个模拟Rcut函数的小函数: proc fcmp outlib=sasuser.funcs.trial; function cut(var, cutoff1, cutoff2, cutoff3, value1, value2, value3, value4); if var<cutoff1 then x=value1; if var>=cutoff1 & var<cu

我目前正在学习SAS,已经知道R和其他编程语言,我对如何声明向量感到困惑

我创建了一个模拟R
cut
函数的小函数:

proc fcmp outlib=sasuser.funcs.trial;
function cut(var, cutoff1, cutoff2, cutoff3, value1, value2, value3, value4);
    if  var<cutoff1 then x=value1;
    if  var>=cutoff1 & var<cutoff2 then x=value2;
    if  var>=cutoff2 & var<cutoff3 then x=value3;
    if  var>=cutoff3 then x=value4;
    return (x);
endsub;
CUTTED_COL = cut(NOT_CUTTED_COL, 3.5, 5, 7.5, 0, 0.5, 1, 2);
当然,这种语法失败得很惨。我怎样才能写对呢

注:即使有比自定义函数更好的方法(我尝试了proc格式,但它不是我想要的),对于教育目的来说,关于向量和函数的真实问题的答案也是非常受欢迎的。
NB2:如果答案是RTFM,我想知道哪个部分


最后:这里是我使用DomPazz的代码()编写的
cut
函数。工作起来很有魅力。

您可以将参数声明为数组(向量)。试试这个:

options cmplib=work.fns;

proc fcmp outlib=work.fns.test;
function cut2(var, cutoffs[*], values[*]);
   put var=;
   put cutoffs=;
   put values=;
   return (1);
endsub;
run;

data _null_;
   array cutoffs[3] (1 2 3);
   array values[3] (4 5 6);

    x = cut2(1,cutoffs,values);
run;
这会将以下内容放入SAS日志:

96   data _null_;
97   array cutoffs[3] (1 2 3);
98   array values[3] (4 5 6);
99
100  x = cut2(1,cutoffs,values);
101  run;

var=1
cutoffs[1]=1 cutoffs[2]=2 cutoffs[3]=3
values[1]=4 values[2]=5 values[3]=6

DomPazz已经发布了如何将数组传递给FCMP函数的答案。但是你的问题看起来像是你应该用一种格式来解决的。如果您定义这样的格式

proc format ;
  value cut 
   low -< 3.5 = '0'
   3.5 -< 5 = '0.5'
   5 -< 7.5 = '1'
   7.5 - high = '2'
  ;
run;
下面是一个使用基于格式的解决方案和基于公式的解决方案的示例

data want ;
  array cutoff (3) _temporary_ (3.5 5 7.5);
  array outcome (4) _temporary_ (0 0.5 1 2);
  input not_cut @@ ;
  do i=1 to dim(cutoff) until (not_cut < cutoff(i));
  end;
  cut=outcome(i);
  cut2 = input(put(not_cut,cut.),32.);
cards;
1 3.5 4 5 6 7.5 8
;

谢谢,这看起来很棒!但这不会在日志窗口中输出任何内容,这正常吗?我假设那些
put
语句应该记录变量,不是吗?有没有办法直接在函数的参数中声明数组?不幸的是,没有。SAS要求在传递数组之前声明数组。
cut2 = input(put(not_cut,cut.),32.);
data want ;
  array cutoff (3) _temporary_ (3.5 5 7.5);
  array outcome (4) _temporary_ (0 0.5 1 2);
  input not_cut @@ ;
  do i=1 to dim(cutoff) until (not_cut < cutoff(i));
  end;
  cut=outcome(i);
  cut2 = input(put(not_cut,cut.),32.);
cards;
1 3.5 4 5 6 7.5 8
;
Obs    not_cut    i    cut    cut2

 1       1.0      1    0.0     0.0
 2       3.5      2    0.5     0.5
 3       4.0      2    0.5     0.5
 4       5.0      3    1.0     1.0
 5       6.0      3    1.0     1.0
 6       7.5      4    2.0     2.0
 7       8.0      4    2.0     2.0