【问题标题】:sas run if statement over macro variablessas 在宏变量上运行 if 语句
【发布时间】:2016-08-10 08:39:44
【问题描述】:

我有以下两个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
;

我想创建一个变量“结果”,它由 if 语句根据变量 a、b、x 和 y 的条件生成。实际上,“拥有”数据集非常大,我想避免排序并将两个数据集合并在一起(其中 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

;

【问题讨论】:

  • 您的参考数据集有多大? proc format可以是个好盟友
  • “ref”数据集只有 72 个观察值。 “拥有”数据集超过 8Gb。

标签: if-statement sas sas-macro


【解决方案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;

希望对你有帮助

【讨论】:

  • ups 我误读了您的if a=&amp;listx and b le &amp;listy then outcome = 1 ; else outcome = 0 ; 我的解决方案不太奏效。认为hash tables 是一个更好的解决方案。给我一些时间,我会提供解决方案
  • 添加了使用哈希表的新解决方案 - 如果可行,请接受我的回答。
猜你喜欢
  • 1970-01-01
  • 2021-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-04
  • 2022-10-23
  • 1970-01-01
相关资源
最近更新 更多