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 '

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5 replies
  • 697 views
  • 4 likes
  • 4 in conversation