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

Hello guys,


Could you help me out with the macro bellow? I’m trying to check the variable CPT code within a range, but it’s being done within a macro. Note how I have added MINOPERATOR MINDELIMITER = ',' in my options statement. Note the statement “%let list1 = '59400','59510','59610';”. I know I have these CPT codes in my CPT variable, therefore the line of code: if cpt in ('59400','59510','59610') then….; should work. But since things get a bit hairy within a macro I am not getting the same results, and as you can see bellow from the log the condition is FALSE.

So my question to you is, what am I missing here? Also note that the code works outside the macro, that is, if I were check each condition individually outside the macro things go fine.

Your input would be greatly appreciated. Thank you,


Alex

/*******************************************/

Bellow are the first 2 passes of the macro for values 1 and 2 coming from the log:

1254 %carve_out (1);

MLOGIC(CARVE_OUT):  Beginning execution.

MLOGIC(CARVE_OUT):  Parameter A has value 1

SYMBOLGEN:  && resolves to &.

SYMBOLGEN:  Macro variable A resolves to 1

SYMBOLGEN:  Macro variable CPT_CARVE_OUT1 resolves to YES

MLOGIC(CARVE_OUT):  %IF condition %upcase(&&cpt_carve_out&a..) = YES is TRUE

SYMBOLGEN:  && resolves to &.

SYMBOLGEN:  Macro variable A resolves to 1

SYMBOLGEN:  Macro variable LIST1 resolves to '59400','59510','59610'

MLOGIC(CARVE_OUT):  %IF condition %eval(cpt in (&&list&a..)) is FALSE

SYMBOLGEN:  Macro variable A resolves to 1

MPRINT(CARVE_OUT):   flag_cpt1 = '0';

MLOGIC(CARVE_OUT):  Ending execution.

1255  %carve_out (2);

MLOGIC(CARVE_OUT):  Beginning execution.

MLOGIC(CARVE_OUT):  Parameter A has value 2

SYMBOLGEN:  && resolves to &.

SYMBOLGEN:  Macro variable A resolves to 2

SYMBOLGEN:  Macro variable CPT_CARVE_OUT2 resolves to YES

MLOGIC(CARVE_OUT):  %IF condition %upcase(&&cpt_carve_out&a..) = YES is TRUE

SYMBOLGEN:  && resolves to &.

SYMBOLGEN:  Macro variable A resolves to 2

SYMBOLGEN:  Macro variable LIST2 resolves to 'J7302'

MLOGIC(CARVE_OUT):  %IF condition %eval(cpt in (&&list&a..)) is FALSE

SYMBOLGEN:  Macro variable A resolves to 2

MPRINT(CARVE_OUT):   flag_cpt2 = '0';

MLOGIC(CARVE_OUT):  Ending execution.


etc...

/*******************************************/

And here is the actual code I’m working on:

/*******************************************/

options nocenter mprint mlogic MINOPERATOR MINDELIMITER = ',' symbolgen linesize=80 pagesize=60;

/*Is there a list of carve-out cpts and rates?*/;

                %let cpt_carve_out1 = YES;

                %let list1 = '59400','59510','59610';

                %let rate1 = 2250;

                %let cpt_carve_out2 = YES;

                %let list2 = 'J7302';

                %let rate2 = 770;

                %let cpt_carve_out3 = NO;

                %let list3 = '76815';

                %let rate3 = 770;

                %let cpt_carve_out4 = NO;

                %let list4 = '76802','76805','76811';

                %let rate4 = 2250;

Data temp1;

                Set temp2;

/*there’s some code here in the middle… */

%macro carve_out (a);

      %if %upcase(&&cpt_carve_out&a..) = YES %then %do;

            %if %eval(cpt in (&&list&a..)) %then %do;

                  flag_cpt&a. = cpt;

            %end;

            %else %do;

                  flag_cpt&a. = '0';

            %end;

      %end;

      %else %do;

            flag_cpt&a. = '0';

      %end;

%mend;

%carve_out (1);

%carve_out (2);

%carve_out (3);

%carve_out (4);

/* the code bellow works individually, i.e. outside the macro*/

/*1st set of codes*/

if %eval(%upcase(&cpt_carve_out1.) = YES) then do;

      if cpt in(&list1.) then do;

            flag_cpt1 = cpt;

      end;

      else do;

            flag_cpt1 = '0';

      end;

end;

else do;

      flag_cpt1 = '0';

end;

**************************************************;

/*2nd set of codes*/

/*if %eval(%upcase(&cpt_carve_out2.) = YES) then do;

      if cpt in(&list2.) then do;

            flag_cpt2 = cpt;

      end;

      else do;

            flag_cpt2 = '0';

      end;

end;

else do;

      flag_cpt2 = '0';

end;

/*etc…*/

Run;

/*******************************************/

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

Agree with Art.  From the code you posted, it looks like you may be confusing the macro language and the data step language.

Assuming that CPT and FLAG_CPT& are data step variables, then this macro would need to be called inside a data step.

If that is the case, then I woulde expect you probably want an IF/THEN statement (to test a condition using data step variables), rather than %IF/%THEN (which cannot test values of data step variables).

%macro carve_out (a);
      %if %upcase(&&cpt_carve_out&a..) = YES %then %do;
            if cpt in (&&list&a..)) then do;
                  flag_cpt&a. = cpt;
            end;
            else do;
                  flag_cpt&a. = '0';
            end;
      %end;
      %else %do;
            flag_cpt&a. = '0';
      %end;
%mend;


View solution in original post

3 REPLIES 3
art297
Opal | Level 21

It would help if you provided a testable example.  The example you provided refers to a datastep variable, ctp, but in a context where such a variable doesn't/can't exist.

Quentin
Super User

Agree with Art.  From the code you posted, it looks like you may be confusing the macro language and the data step language.

Assuming that CPT and FLAG_CPT& are data step variables, then this macro would need to be called inside a data step.

If that is the case, then I woulde expect you probably want an IF/THEN statement (to test a condition using data step variables), rather than %IF/%THEN (which cannot test values of data step variables).

%macro carve_out (a);
      %if %upcase(&&cpt_carve_out&a..) = YES %then %do;
            if cpt in (&&list&a..)) then do;
                  flag_cpt&a. = cpt;
            end;
            else do;
                  flag_cpt&a. = '0';
            end;
      %end;
      %else %do;
            flag_cpt&a. = '0';
      %end;
%mend;


avbraga
Calcite | Level 5

That did it, Quentin. I was about to send you guys some test data, because that does help, but once I tried your suggestion, I saw that it does what I want.

Again, thank you for your help. This community is great!

Alex

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 3 replies
  • 1608 views
  • 1 like
  • 3 in conversation