DATA Step, Macro, Functions and more

Difference between upcase and %upcase?

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Difference between upcase and %upcase?

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
Solution
‎09-08-2012 10:41 PM
Super User
Super User
Posts: 6,497

Re: Difference between upcase and %upcase?

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.

View solution in original post


All Replies
Solution
‎09-08-2012 10:41 PM
Super User
Super User
Posts: 6,497

Re: Difference between upcase and %upcase?

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.

Respected Advisor
Posts: 3,124

Re: Difference between upcase and %upcase?

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

New Contributor
Posts: 2

Re: Difference between upcase and %upcase?

Thanks!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 738 views
  • 3 likes
  • 3 in conversation