BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hello everybody!

My SAS Code:

%macro test;
%let mv=%sysfunc(cats(%str(%'),&var,%str(%')))
%put &mv;

%let mv2=%sysfunc(cats(%str(%%),&var,%str(%%)))
%put &mv2;
%mend test;

%test(blabla);

Result:

''blabla''

%blabla%

My question is: Why I have double single-quotes each side in first case and single percent sing in second?
7 REPLIES 7
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Suggested DOC references about macro variables and quoting.

Scott Barry
SBBWorks, Inc.

Recommended Google advanced search argument, this topic/post:

macro variable quoting site:sas.com
deleted_user
Not applicable
Thanks for your recommendations Scott
I tried to google, but have no success.

I have no idea why quotes are multiplies! Can you explain me please?
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Some characters have special meaning when used with the SAS macro language, particularly with SAS macro variables. Strongly encourage some DOC reading on the macro facility and macro variables.

Scott Barry
SBBWorks, Inc.

SAS Macro Language: Reference: Introduction to the Macro Facility
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a002293969.htm

SAS Macro Language: Reference
Macro Variables
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a002047074.htm


Introduction to Macro Quoting
Masking Special Characters and Mnemonics
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/tw3514-overvwqt.htm
Cynthia_sas
SAS Super FREQ
Hi:
In addition to the reading that Scott has suggested, I suggest that you read about how macro variables are used and referenced:
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a001071889.htm
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a001071910.htm

This bit of documentation on Macro Quoting functions is a good explanation of how macro quoting works by masking the macro variable with a "delta" character:
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a001061351.htm
When the macro processor masks a text string, it masks special characters and mnemonics within the coding scheme, and prefixes and suffixes the string with a hexadecimal character, called a delta character. The prefix character marks the beginning of the string and also indicates what type of macro quoting is to be applied to the string. The suffix character marks the end of the string. The prefix and suffix characters preserve any leading and trailing blanks contained by the string. The hexadecimal characters used to mask special characters and mnemonics and the characters used for the prefix and suffix might vary and are not portable.

There are more hexadecimal combinations possible in each byte than are needed to represent the symbols on a keyboard. Therefore, when a macro quoting function recognizes an item to be masked, the macro processor uses a previously unused hexadecimal combination for the prefix and suffix characters.


I can't think of any reason to use the CATS function to build a macro variable reference because normally, the %LET statement strips leading and trailing blanks from macro variables. In addition, it seems the reason you are using the CATS function is to prefix and append single quotes to a resolved macro variable value so you can build a quoted string for a new macro variable value. This snippet of macro code shows how macro concatenation works. Note how the macro variables &V1 &V2 and &V3 are used to build new macro variables, as shown in the SAS log:
[pre]
14 %let v1 = Kermit;
15 %let v2 = the;
16 %let v3 = Frog;
17
18 %put &v1&v2&v3;
KermittheFrog
19 %put &v1-&v2-&v3;
Kermit-the-Frog
20 %put &v1 &v2 &v3;
Kermit the Frog
21
22
23 %let NewMacVar = &v1 &v2 &v3;
24 %let NewMacVar2 = %str(%')&NewMacVar%str(%');
25 %put &NewMacVar;
Kermit the Frog
26 %put &NewMacVar2;
'Kermit the Frog'

[/pre]

Generally speaking, it is not a good idea to "prequote" macro variable values. The quotes that you use belong to SAS program statements, such as a title statement or an assignment statement:
[pre]
data testit;
var1 = "&NewMacVar";
var2 = "&NewMacVar2";
run;

proc print data=testit;
run;
[/pre]

The above program produces this output in the LISTING window:
[pre]
Obs var1 var2

1 Kermit the Frog 'Kermit the Frog'
[/pre]

If you WANTED the quotes to be part of the value for VAR2, then it would be acceptable to put quotes into the &NewMacVar2 variable value. However, pre-quoting macro variables can lead to syntax errors later in your code when you go to use the macro variables -- not in the above program, but there are usages of &NewMacVar2 that could lead to quoting issues in program code and you might see "open code recursion" error messages or you might see "meaning of an identifier after a quoted string" messages or you might just get lost in unbalanced quote-land if you're not careful in the places where you use the "pre-quoted" macro variable value.

But since you haven't explained the purpose for your macro program, it's hard to figure out whether you really have a need to insert single quotes into a macro variable value. This paper might help explain how Macro Quoting works (as a supplement to the documentation):
http://www2.sas.com/proceedings/forum2007/152-2007.pdf

cynthia
deleted_user
Not applicable
Hi, Cynthia, your reply was very helpful.

>But since you haven't explained the purpose for your macro program, it's hard to figure out >whether you really have a need to insert single quotes into a macro variable value.

This program actually has no practical usage, it is for example only.
I'm interested in difference behavior of %str in different sas functions:

%let mv=%sysfunc(cats(%str(%')));
%put &mv;

%let mv=%sysfunc(trim(%str(%')));
%put &mv;

In first case we have double single-qoutes in second case %str not works at all.



Scott, i;ve already read these documents,thanks
Florent
Quartz | Level 8
Hi Memo,

When replacing the macro '%sysfunc' of second case by the macro '%qsysfunc' then you'll have a single quote displayed in your LOG.

Kr,
Florent
deleted_user
Not applicable
Hi Florent,

You right, %qsysfunc change behavior in second case, but do nothing in first case.

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
  • 7 replies
  • 5178 views
  • 0 likes
  • 4 in conversation