sas 运行 if 宏变量语句

sas run if statement over macro variables

我有以下两个 sas 数据集:

data have ;
 input a b;
cards;
1 15
2 10
3 40
4 200
1 25
2 15
3 10
4 75
1 1
2 99
3 30
4 100
;

data ref ;
 input x y;
cards;
1 10
2 20
3 30
4 100
;

我想要以下数据集:

data want ;
 input a b outcome ;
cards;
1 15 0
2 10 1
3 40 0
4 200 0
1 25 0
2 15 1
3 10 1
4 75 1
1 1 1
2 99 0
3 30 1
4 100 1
;

我想创建一个变量 'outcome',它由 if 语句根据变量 a、b、x 和 y 的条件生成。实际上 'have' 数据集非常大,我想避免排序并将两个数据集合并在一起(其中 a = x)。

我正在尝试通过以下代码使用宏变量:

data _null_ ;
set ref ;
  call symput('listx', x) ;
  call symput('listy', y) ;
run ;

data want ;
set have ;
if a=&listx and b le &listy then outcome = 1 ; else outcome = 0 ;
run ;

然而并没有产生预期的结果:

data want ;
 input a b outcome ;
cards;
1 15 0
2 10 1
3 40 0
4 200 0
1 25 0
2 15 1
3 10 1
4 75 1
1 1 1
2 99 0
3 30 1
4 100 1

;

使用哈希表重做我的解决方案。低于我的方法

data ref2(rename=(x=a));
set ref ;
run;

data want;
declare Hash Plan ();
    rc = plan.DefineKey ('a');  /*x originally*/
    rc = plan.DefineData('a', 'y');
    rc = plan.DefineDone();

    do until (eof1);
     set ref2 end=eof1;
     rc = plan.add();   /*add each record from ref2 to plan (hash table)*/
    end;

    do until (eof2);
     set have end=eof2;
     call missing(y);
     rc = plan.find();
     outcome = (rc =0 and b<y);
     output;
    end;
    stop;
run;

希望对您有所帮助