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
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.
@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.
you don't need any quotes at all. The %if statement should look like:
%if %qupcase(&abcd) = HGB %then %do;
Thanks, it does the trick.
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:
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.
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.
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!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.