BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
zhouzy99
Calcite | Level 5

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

3 REPLIES 3
Tom
Super User Tom
Super User

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.

Haikuo
Onyx | Level 15

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

zhouzy99
Calcite | Level 5

Thanks!

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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