Hi all, I was trying to use macro to convert the case of a character variable, but I was confused with the usage of upcase and %upcase. Below is the sample code that I used. The chgCase macro is supposed to convert the input string to upper case and return the converted string to the data step. The following code is always working. However, if I replaced upcase(&input) with %upcase(&input), then the case of the input string won't be converted, newName stored the same string as the original text. And SAS enterprise guide didn't report any error or warning. I am wondering why %upcase won't work within my macro? Thanks!
data name;
input name $;
datalines;
Jones
White
Smith
;
run;
%macro chgCase(input);
%let newText=upcase(&input);
&newText
%mend chgCase;
data name;
set name;
newName=%chgCase(name);
run;
Macro just generates text. So in your example the text it is generating is: upcase(name).
If instead you change the macro to use %upcase then the text it will generate is: NAME.
So the name of the variable is now in uppercase, but that will have no effect on the values of the variable that get assigned to the new dataset variable.
Macro just generates text. So in your example the text it is generating is: upcase(name).
If instead you change the macro to use %upcase then the text it will generate is: NAME.
So the name of the variable is now in uppercase, but that will have no effect on the values of the variable that get assigned to the new dataset variable.
Besides Tom's dead-on point, %upcase() will only work on macro variables, while upcase() will only work on data set variables without stacking other macro functions, so in your case, you HAVE to use upcase().
Also, invoking options like symbolgen, mprint, mlogic will help you troubleshoot macros.
Haikuo
Thanks!
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.
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.