如果那就是SAS中的MACRO

时间:2014-03-28 14:01:42

标签: sas sas-macro

我必须创建EVENT 1-EVENT 20但是我一个接一个地手动添加,添加所有事件和代码看起来很难看,有没有办法简单地做,比如添加宏?

DATA Z3_ALL91;
SET Z3_ALL8;

EVENT1=INPUT('.',$CHAR10.);
EVENT2=INPUT('.',$CHAR10.);
EVENT3=INPUT('.',$CHAR10.);
EVENT4=INPUT('.',$CHAR10.);

IF RE1='HO'  THEN EVENT1=ERP1;
IF RE1='RE'  THEN EVENT1='REP';
IF RE1='HSS' THEN EVENT1=ERP1;

IF RE2='HO' THEN EVENT2=ERP2; ELSE
IF RE2='RE' THEN EVENT2='REP';ELSE
IF (RE2='HSS' AND ROH1^="") THEN EVENT2=CATX("_",ERP2,ROH1);ELSE EVENT2=ERP2;

IF RE3='HO' THEN EVENT3=ERP3; ELSE
IF RE3='RE' THEN EVENT3='REP';ELSE
IF (RE3='HSS' AND ROH2>ROH1) THEN EVENT3=CATX("_",ERP3,ROH2);ELSE
IF (RE3='HSS' AND ROH1^=".") THEN EVENT3=CATX("_",ERP3,ROH1);ELSE EVENT3=ERP3;

IF RE4='HO' THEN EVENT4=ERP4; ELSE
IF RE4='RE' THEN EVENT4='REP';ELSE
IF (RE4='HSS' AND ROH3>ROH2) THEN EVENT4=CATX("_",ERP4,ROH3);ELSE
IF (RE4='HSS' AND ROH2>ROH1) THEN EVENT4=CATX("_",ERP4,ROH2);ELSE 
IF (RE4='HSS' AND ROH1^=".") THEN EVENT4=CATX("_",ERP4,ROH1);ELSE EVENT4=ERP4;

RUN;

1 个答案:

答案 0 :(得分:1)

这是我的尝试......

%MACRO EVENTS(N) ;
  %IF &N = 1 %THEN %DO ;
    if RE&N = 'HO' then EVENT&N = ERP&N ;
    else
    if RE&N = 'RE' then EVENT&N = 'REP' ;
    else
    if RE&N = 'HSS' then EVENT&N = ERP&N ;
  %END ;
  %ELSE %DO ;
    if RE&N = 'HO' then EVENT&N = ERP&N ;
    else
    if RE&N = 'RE' then EVENT&N = 'REP' ;
    else
    %DO L1 = %EVAL(&N - 1) %TO 1 %BY -1 ;
      %LET L2 = %EVAL(&L1 - 1) ;
      %IF &L1 = 1 %THEN %DO ;
        if (RE&N = 'HSS' and ROH&L1 ^= ".") then EVENT&N = catx('_',ERP&N,ROP&L1) ;
      %END ;
      %ELSE %DO ;
        if (RE&N = 'HSS' and ROH&L1 > ROH&L2) then EVENT&N = catx('_',ERP&N,ROP&L1) ;
      %END ;
      else
    %END ;
    EVENT&N = ERP&N ;
  %END ;
%MEND ;

options nosymbolgen ;

data z3_all91 ;
  set z3_all8 ;

  EVENT1 = input('.',$CHAR10.);
  EVENT2 = input('.',$CHAR10.);
  EVENT3 = input('.',$CHAR10.);
  EVENT4 = input('.',$CHAR10.);

  %EVENTS(1) ;
  %EVENTS(2) ;
  %EVENTS(3) ;
  %EVENTS(4) ;
run ;

日志中生成的代码:

3940  data z3_all91 ;
3941    set z3_all8 ;
3942    EVENT1 = input('.',$CHAR10.);
3943    EVENT2 = input('.',$CHAR10.);
3944    EVENT3 = input('.',$CHAR10.);
3945    EVENT4 = input('.',$CHAR10.);
3946    %EVENTS(1) ;
MPRINT(EVENTS):   if RE1 = 'HO' then EVENT1 = ERP1 ;
MPRINT(EVENTS):   else if RE1 = 'RE' then EVENT1 = 'REP' ;
MPRINT(EVENTS):   else if RE1 = 'HSS' then EVENT1 = ERP1 ;
3947    %EVENTS(2) ;
MPRINT(EVENTS):   if RE2 = 'HO' then EVENT2 = ERP2 ;
MPRINT(EVENTS):   else if RE2 = 'RE' then EVENT2 = 'REP' ;
MPRINT(EVENTS):   else if (RE2 = 'HSS' and ROH1 ^= ".") then EVENT2 = catx('_',ERP2,ROP1) ;
MPRINT(EVENTS):   else EVENT2 = ERP2 ;
3948    %EVENTS(3) ;
MPRINT(EVENTS):   if RE3 = 'HO' then EVENT3 = ERP3 ;
MPRINT(EVENTS):   else if RE3 = 'RE' then EVENT3 = 'REP' ;
MPRINT(EVENTS):   else if (RE3 = 'HSS' and ROH2 > ROH1) then EVENT3 = catx('_',ERP3,ROP2) ;
MPRINT(EVENTS):   else if (RE3 = 'HSS' and ROH1 ^= ".") then EVENT3 = catx('_',ERP3,ROP1) ;
MPRINT(EVENTS):   else EVENT3 = ERP3 ;
3949    %EVENTS(4) ;
MPRINT(EVENTS):   if RE4 = 'HO' then EVENT4 = ERP4 ;
MPRINT(EVENTS):   else if RE4 = 'RE' then EVENT4 = 'REP' ;
MPRINT(EVENTS):   else if (RE4 = 'HSS' and ROH3 > ROH2) then EVENT4 = catx('_',ERP4,ROP3) ;
MPRINT(EVENTS):   else if (RE4 = 'HSS' and ROH2 > ROH1) then EVENT4 = catx('_',ERP4,ROP2) ;
MPRINT(EVENTS):   else if (RE4 = 'HSS' and ROH1 ^= ".") then EVENT4 = catx('_',ERP4,ROP1) ;
MPRINT(EVENTS):   else EVENT4 = ERP4 ;
3950  run ;