SAS - 从数据集的每一行创建宏

时间:2017-05-09 11:56:41

标签: sas

我有以下数据集:

Target_attr Target_level
ATTR1   CHOCOLATE SBU
ATTR1   CHOCOLATE SBU
ATTR1|ATTR5 CHOCOLATE SBU|EVERYDAY
ATTR1|ATTR5 CHOCOLATE SBU|EVERYDAY
ATTR1|ATTR5 CHOCOLATE SBU|EVERYDAY
ATTR1|ATTR5 CHOCOLATE SBU|EVERYDAY
ATTR1|ATTR5 CHOCOLATE SBU|EVERYDAY
ATTR1|ATTR5 CHOCOLATE SBU|EVERYDAY
ATTR1|ATTR5 CHOCOLATE SBU|EASTER
ATTR1|ATTR5 CHOCOLATE SBU|HALLOWEEN
ATTR1|ATTR5 CHOCOLATE SBU|HOLIDAY
ATTR1|ATTR5 CHOCOLATE SBU|VALENTINE
ATTR1|ATTR5 CHOCOLATE SBU|IN/OUT

列的Target_attr值是另一个数据集的变量,列的Target_level是该列中的值。 所以我想做的是创建一个宏来设置数据集的子集,如下所示:

row1: if ATTR1="CHOCOLATE SBU" then ...
row2: if ATTR1="CHOCOLATE SBU" then ...
row3: if ATTR1="CHOCOLATE SBU" and ATTR5="EVERYDAY" then ...

其他数据集如下:

PPGs    ATTR1   ATTR2   ATTR3   ATTR4   ATTR5   ATTR6
A/O BRAND EASTER CH BASKET <=4OZ    CHOCOLATE SBU   A/O MANUFACTURER    A/O FRANCHISE   TAKE HOME   EASTER  BASKET <=4OZ
A/O BRAND EASTER BASKET <=4OZ   SWEETS SBU  A/O MANUFACTURER    A/O FRANCHISE   TAKE HOME   EASTER  BASKET <=4OZ
FRANKFORD EASTER BASKET <=4OZ   SWEETS SBU  FRANKFORD   FRANKFORD FR    TAKE HOME   EASTER  BASKET <=4OZ
A/O BRAND EASTER CH BASKET >4.1OZ   CHOCOLATE SBU   A/O MANUFACTURER    A/O FRANCHISE   TAKE HOME   EASTER  BASKET >4.1OZ
A/O BRAND EASTER BASKET >4.1OZ  SWEETS SBU  A/O MANUFACTURER    A/O FRANCHISE   TAKE HOME   EASTER  BASKET >4.1OZ
TIC TAC EVERYDAY BF BOTTLE/POUCH    BREATH FRESHENER    FERRERO TIC TAC FR  INSTANT CONSUMABLE  EVERYDAY    BF BOTTLE/POUCH
TIC TAC EVERYDAY BF CT BOX  CHOCOLATE SBU   FERRERO TIC TAC FR  INSTANT CONSUMABLE  EVERYDAY    BF CT BOX
BREATHSAVERS EVERYDAY BF CT BOX CHOCOLATE SBU   HERSHEY BREATHSAVERS FR INSTANT CONSUMABLE  EVERYDAY    BF CT BOX
ICE BREAKERS FR EVERYDAY BF CT BOX  BREATH FRESHENER    HERSHEY ICE BREAKERS FR INSTANT CONSUMABLE  EVERYDAY    BF CT BOX
MENTOS EVERYDAY BF CT BOX   BREATH FRESHENER    PERFETTI VAN MELLE  MENTOS FR   INSTANT CONSUMABLE  EVERYDAY    BF CT BOX

所以从第一行开始,子数据集应该是

PPGs    ATTR1   ATTR2   ATTR3   ATTR4   ATTR5   ATTR6
A/O BRAND EASTER CH BASKET <=4OZ    CHOCOLATE SBU   A/O MANUFACTURER    A/O FRANCHISE   TAKE HOME   EASTER  BASKET <=4OZ
A/O BRAND EASTER CH BASKET >4.1OZ   CHOCOLATE SBU   A/O MANUFACTURER    A/O FRANCHISE   TAKE HOME   EASTER  BASKET >4.1OZ
TIC TAC EVERYDAY BF CT BOX  CHOCOLATE SBU   FERRERO TIC TAC FR  INSTANT CONSUMABLE  EVERYDAY    BF CT BOX
BREATHSAVERS EVERYDAY BF CT BOX CHOCOLATE SBU   HERSHEY BREATHSAVERS FR INSTANT CONSUMABLE  EVERYDAY    BF CT BOX
第3行的

应为:

PPGs    ATTR1   ATTR2   ATTR3   ATTR4   ATTR5   ATTR6
TIC TAC EVERYDAY BF CT BOX  CHOCOLATE SBU   FERRERO TIC TAC FR  INSTANT CONSUMABLE  EVERYDAY    BF CT BOX
BREATHSAVERS EVERYDAY BF CT BOX CHOCOLATE SBU   HERSHEY BREATHSAVERS FR INSTANT CONSUMABLE  EVERYDAY    BF CT BOX

我尝试了一些东西,但不值得分享...... 你能帮忙吗?

1 个答案:

答案 0 :(得分:0)

如果您无法将其重新设计为连接或格式,那么使用数据步骤生成所需代码可能比使用宏变量或宏逻辑生成代码更容易。

所以如果你想生成

if ATTR1="CHOCOLATE SBU" then ...

您可以使用如下的put语句:

put 'if ' target_attr '=' target_level :$quote. 'then ...';

所以你的整个程序都会遵循这个结构。

filename code temp;
data _null_;
  set METADATA ;
  file code ;
  * generate code ;
  put 'if ' target_attr ....
  ...
run;
data want ;
  set have ;
  %include code /source2 ;
run;

此方法的一个优点是您可以查看生成的代码并确保生成逻辑生成有效的代码。查看和测试纯SAS代码比使用宏变量引用或宏逻辑查看代码要容易得多。