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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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