DATA Step, Macro, Functions and more

Need help on macro debugging

Reply
Contributor
Posts: 20

Need help on macro debugging

Hi,

Here is my macro KCM.

options mlogic mprint;

%macro kcm;

%let __tmpmv_i = 1;

%do %while(%scan(%nrbquote(&__tmpmv_lab_param),&__tmpmv_i,%str( )) ne );

       %let abcd = %scan(%nrbquote(&__tmpmv_lab_param),&__tmpmv_i,%str( ));

       %if %qupcase(&abcd) = "HGB" %then %do;

           %put ^^hemo;

           %let __tmpmv_i = %eval(&__tmpmv_i + 1);

       %end;

       %else %do;

          %let __tmpmv_i = %eval(&__tmpmv_i + 1);

         %put ^^&abcd;

       %end;

%end;

%mend;

%kcm;

The part in red is not working as expected. Here is the log

MLOGIC(KCM):  %DO %WHILE(%scan(%nrbquote(&__tmpmv_lab_param),&__tmpmv_i,   ) ne) condition is TRUE; loop will iterate again.

MLOGIC(KCM):  %LET (variable name is ABCD)

MLOGIC(KCM):  %IF condition %qupcase(&abcd) = "HGB" is FALSE

MLOGIC(KCM):  %LET (variable name is __TMPMV_I)

MLOGIC(KCM):  %PUT ^^&abcd

^^HGB

Clearly it can't recognize the value "HGB". &__tmpmv_lab_param contains 32 parameters including HGB.  For the other parameter the green loop is working okay. Any idea why?

Any help is appreciated.

Kind regards,

Dhruba

SAS Employee
Posts: 7

Re: Need help on macro debugging

Hi. You don't need the double quotes around HGB. If you remove those, your code should work. %qupcase does not add quotes to a value.

Contributor
Posts: 20

Re: Need help on macro debugging

@kaade : I have tried single quotes too, didn't work.

@data _null_ : I need to develop a similar macro but with more complex calculations. I am just testing few steps with this one. A data step won't work because I have to call few other building block macros within the main macro. Long story short, I need to make the red step work.

SAS Employee
Posts: 7

Re: Need help on macro debugging

you don't need any quotes at all. The %if statement should look like:

%if %qupcase(&abcd) = HGB %then %do;

Contributor
Posts: 20

Re: Need help on macro debugging

Thanks, it does the trick.

Respected Advisor
Posts: 3,777

Re: Need help on macro debugging

Are you sure you need this macro?  Seems awfully complicated.  What is it suppose to do?  Maybe a nice data step would be better. :smileygrin:

Super User
Super User
Posts: 6,502

Re: Need help on macro debugging

It looks like you are trying to process lab test codes.

Cleaning text data is better done in a DATA step or SQL coding.

You can always store the values back into macro variables if you need to later.

Super User
Super User
Posts: 7,429

Re: Need help on macro debugging

I totally agree with Tom/data_null_.  You can use other macros in datastep language, no need to default to macro language.  If you are writing a standard macro for others to use then simplifying you code would be beneficial.

Occasional Contributor
Posts: 15

Re: Need help on macro debugging

It looks like you are trying to parse a macro variable that contains a string of lab parameters separated by a space.

Then when you find HGB, you want to do something, otherwise do something else.

If your macro variable that contains the string of lab parameters separated does not have any quotes or special characters, you can simplify this code to look something like this -

%macro kcm;

     %let nparm_=0;
     %if %length(&__tmpmv_lab_param) > 0 %then
          %do;
          %let nparm_=%sysfunc(countw(&__tmpmv_lab_param));

          %do __tmpmv_i = 1 %to &nparm_;

               %let abcd = %scan(&__tmpmv_lab_param,&__tmpmv_i);

               %if %upcase(&abcd) = HGB %then %do;
                    %put ^^hemo;
               %end;
               %else %do;
                    %put ^^&abcd;
               %end;

     %end;
     %end;

%mend;

%kcm;

It will make it easier to debug.

Otherwise, you can ignore me.

Good luck!

Ask a Question
Discussion stats
  • 8 replies
  • 353 views
  • 1 like
  • 6 in conversation