sas运行if语句宏变量

时间:2016-08-10 08:39:44

标签: if-statement sas sas-macro

我有以下两个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',它由变量a,b,x和y的条件的if语句产生。实际上'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

;

1 个答案:

答案 0 :(得分:2)

使用哈希表重做我的解决方案。在我的方法之下

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;

希望有所帮助