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

Assume we have the following two alternatives: 
Alternative 1 (Please note the comment with the dot, is this better practice to use?): 

%let country = USA; 

%if "&country" = "USA" %then %do;  * Is it better to write: %if "&country." = "USA" %then %do ?; 
	%put Note: right; 
%end; 
%else %do; 
	%put Note: Wrong; 
%end; 

Alternative 2 (Please note the comment with the dot, is this better practice to use?): 

%let country = USA; 

%if &country = USA %then %do; * Is it better to write: %if &country. = USA %then %do ?;
	%put Note: right; 
%end; 
%else %do; 
	%put Note: Wrong; 
%end; 

Both alternatives work but it seems to me like the first alternative "should be preferred" since it is text (USA) we are dealing with? Any opinions on this? 


Finally, why would the following produce "wrong"? 

%let country = USA; 

%if "&country" = 'USA' %then %do; 
	%put Note: right; 
%end; 
%else %do; 
	%put Note: Wrong; 
%end; 



Regarding the comments, is it better practice to use a dot? 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

It is never wrong to use the dot, so it is recommended, especially for beginners, to use it. It also makes macro references more visible in the Enhanced Editor.

 

Since the macro processor knows only one "data type" (text), quotes are not necessary; in fact, they become part of the text, and that is why your comparison of single vs. double quotes will always return false.

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

It is never wrong to use the dot, so it is recommended, especially for beginners, to use it. It also makes macro references more visible in the Enhanced Editor.

 

Since the macro processor knows only one "data type" (text), quotes are not necessary; in fact, they become part of the text, and that is why your comparison of single vs. double quotes will always return false.

SasStatistics
Pyrite | Level 9

If I understand it correctly, text is NOT the same as string?

Example 1 (Text): This is some text
Example 2 (String): "This is a string"

PaigeMiller
Diamond | Level 26

@SasStatistics wrote:

If I understand it correctly, text is NOT the same as string?

Example 1 (Text): This is some text
Example 2 (String): "This is a string"


That's not it. Macro variables are always text. You do not need to use quotes around the value of macro variables, because they are always text.

 

When you put macro variables in quotes, then the quotes are part of the macro variable value. In the code below &a1 is a character string that has six characters, a double-quote, then a capital T and then an e and then an x and then a t and then a double quote. In the code below &a2 is a character string of four characters, and none of the four characters is a double quote.

 

%macro nothing;

%let a1 = "Text";
%let a2 = Text;

%if &a1=Text %then %put TRUE; %else %put FALSE;
%if &a2=Text %then %put TRUE; %else %put FALSE;
%if &a1=&a2 %then %put TRUE; %else %put FALSE;
%mend;

%nothing

As you can see in the results, &a1 does not equal Text because &a1 has quotes.

 

DO NOT PUT QUOTES around the values of your macro variable. Generally, quotes are simply not need when defining or evaluating macro variables.

--
Paige Miller
Tom
Super User Tom
Super User

The reason you might want to include quote characters in the strings you are comparing to protect against values to look like operators to the implied %EVAL() function call you are generating.

 

For example when testing two letter state abbreviation codes.  Try testing for the code for Oregon.

 

The reason the last test is false is pretty obvious, the first character of the strings being compared are different.  " does not equal '

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 883 views
  • 4 likes
  • 4 in conversation