DATA Step, Macro, Functions and more

IF THEN macro variable

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

IF THEN macro variable

[ Edited ]

I'm trying to define a macro variable (state) that is defendant on the date. Here's what I have now....

 

DATA _NULL_;

IF "&sysdate."D - 36 IN ("01JAN2018"d,"02JAN2018"d,"15JAN2018"d,"19FEB2018"d,"28MAY2018"d,"04JUL2018"d

,"05JUL2018"d,"03SEP2018"d,"11NOV2018"d,"22NOV2018"d,"25DEC2018"d,"26DEC2018"d) THEN DO;

%LET state = 'LA','MS','AL','UT','RI';

END;

ELSE IF "&sysdate."D - 36 IN ("08OCT2018"d) THEN DO;

%LET state= 'LA','AL','UT','RI';

END;

ELSE IF "&sysdate."D - 36 IN ("13FEB2018"d,"03JUN2018"d) THEN DO;

%LET state= 'LA','AL';

END;

ELSE IF "&sysdate."D - 36 IN ("23APR2018"d) THEN DO;

%LET state= 'MS','AL';

END;

ELSE IF "&sysdate."D - 36 IN ("06NOV2018"d) THEN DO;

%LET state= 'LA','RI';

END;

ELSE IF "&sysdate."D - 36 IN ("08JAN2018"d,"19JAN2018"d,"30MAR2018"d,"01APR2018"d,"30AUG2018"d

,"01NOV2018"d,"23NOV2018"d,"24DEC2018"d,"31DEC2018"d) THEN DO;

%LET state= 'LA';

END;

ELSE IF "&sysdate."D - 36 IN ("13AUG2018"d,"04MAY2018"d) THEN DO;

%LET state= 'RI';

END;

ELSE IF "&sysdate."D - 36 IN ("24JUL2018"d) THEN DO;

%LET state= 'UT';

END;

RUN;

 

 

This is returning state = 'UT' but I want it to return state = 'LA','MS','AL','UT','RI' since "&sysdate."D - 36 = "02JAN2018"d

 

Appreciate any help you can provide

 


Accepted Solutions
Solution
‎02-07-2018 10:51 AM
Super User
Posts: 8,218

Re: IF THEN macro variable

Use call symput rather than %let! I think that the following does what you want:

DATA _NULL_;
  IF "&sysdate."D - 36 IN ("01JAN2018"d,"02JAN2018"d,"15JAN2018"d,"19FEB2018"d,"28MAY2018"d,"04JUL2018"d
   ,"05JUL2018"d,"03SEP2018"d,"11NOV2018"d,"22NOV2018"d,"25DEC2018"d,"26DEC2018"d) THEN DO;
    /* %LET state = 'LA','MS','AL','UT','RI'; */
    call symput('state',"'LA','MS','AL','UT','RI'");
  END;

  ELSE IF "&sysdate."D - 36 IN ("08OCT2018"d) THEN DO;
    /* %LET state= 'LA','AL','UT','RI'; */
    call symput('state',"'LA','AL','UT','RI'");
  END;

  ELSE IF "&sysdate."D - 36 IN ("13FEB2018"d,"03JUN2018"d) THEN DO;
    /* %LET state= 'LA','AL'; */
    call symput('state',"'LA','AL'");
  END;

  ELSE IF "&sysdate."D - 36 IN ("23APR2018"d) THEN DO;
    /* %LET state= 'MS','AL'; */
    call symput('state',"'MS','AL'");
  END;

  ELSE IF "&sysdate."D - 36 IN ("06NOV2018"d) THEN DO;
    /* %LET state= 'LA','RI'; */
    call symput('state',"'LA','RI'");
  END;

  ELSE IF "&sysdate."D - 36 IN ("08JAN2018"d,"19JAN2018"d,"30MAR2018"d,"01APR2018"d,"30AUG2018"d
   ,"01NOV2018"d,"23NOV2018"d,"24DEC2018"d,"31DEC2018"d) THEN DO;
    /* %LET state= 'LA'; */
    call symput('state',"'LA'");
  END;

  ELSE IF "&sysdate."D - 36 IN ("13AUG2018"d,"04MAY2018"d) THEN DO;
    /* %LET state= 'RI'; */
    call symput('state',"'RI'");
  END;

  ELSE IF "&sysdate."D - 36 IN ("24JUL2018"d) THEN DO;
    /* %LET state= 'UT'; */
    call symput('state',"'UT'");
  END;
RUN;
%put &state.;

Art, CEO, AnalystFinder.com

 

View solution in original post


All Replies
Super Contributor
Posts: 359

Re: IF THEN macro variable

Hello,
The lines in your code beginning with %let are macro commands. As such they are executed sequentially and independently of your different tests. You either have to use macro tests %if ... %then (and the data step is then useless but you have to encapsulate your code in a macro) or defining "state" as a dataset column and export it in a macrovariable with a call symput.
Solution
‎02-07-2018 10:51 AM
Super User
Posts: 8,218

Re: IF THEN macro variable

Use call symput rather than %let! I think that the following does what you want:

DATA _NULL_;
  IF "&sysdate."D - 36 IN ("01JAN2018"d,"02JAN2018"d,"15JAN2018"d,"19FEB2018"d,"28MAY2018"d,"04JUL2018"d
   ,"05JUL2018"d,"03SEP2018"d,"11NOV2018"d,"22NOV2018"d,"25DEC2018"d,"26DEC2018"d) THEN DO;
    /* %LET state = 'LA','MS','AL','UT','RI'; */
    call symput('state',"'LA','MS','AL','UT','RI'");
  END;

  ELSE IF "&sysdate."D - 36 IN ("08OCT2018"d) THEN DO;
    /* %LET state= 'LA','AL','UT','RI'; */
    call symput('state',"'LA','AL','UT','RI'");
  END;

  ELSE IF "&sysdate."D - 36 IN ("13FEB2018"d,"03JUN2018"d) THEN DO;
    /* %LET state= 'LA','AL'; */
    call symput('state',"'LA','AL'");
  END;

  ELSE IF "&sysdate."D - 36 IN ("23APR2018"d) THEN DO;
    /* %LET state= 'MS','AL'; */
    call symput('state',"'MS','AL'");
  END;

  ELSE IF "&sysdate."D - 36 IN ("06NOV2018"d) THEN DO;
    /* %LET state= 'LA','RI'; */
    call symput('state',"'LA','RI'");
  END;

  ELSE IF "&sysdate."D - 36 IN ("08JAN2018"d,"19JAN2018"d,"30MAR2018"d,"01APR2018"d,"30AUG2018"d
   ,"01NOV2018"d,"23NOV2018"d,"24DEC2018"d,"31DEC2018"d) THEN DO;
    /* %LET state= 'LA'; */
    call symput('state',"'LA'");
  END;

  ELSE IF "&sysdate."D - 36 IN ("13AUG2018"d,"04MAY2018"d) THEN DO;
    /* %LET state= 'RI'; */
    call symput('state',"'RI'");
  END;

  ELSE IF "&sysdate."D - 36 IN ("24JUL2018"d) THEN DO;
    /* %LET state= 'UT'; */
    call symput('state',"'UT'");
  END;
RUN;
%put &state.;

Art, CEO, AnalystFinder.com

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 109 views
  • 0 likes
  • 3 in conversation