BookmarkSubscribeRSS Feed
kingCobra
Obsidian | Level 7

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

8 REPLIES 8
kaade
SAS Employee

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.

kingCobra
Obsidian | Level 7

@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.

kaade
SAS Employee

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

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

kingCobra
Obsidian | Level 7

Thanks, it does the trick.

data_null__
Jade | Level 19

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:

Tom
Super User Tom
Super User

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

BurntDirt
Calcite | Level 5

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!

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
  • 8 replies
  • 1159 views
  • 1 like
  • 6 in conversation