BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
dber
Calcite | Level 5

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

2 REPLIES 2
gamotte
Rhodochrosite | Level 12
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.
art297
Opal | Level 21

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

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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