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!
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.