- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks!