DATA Step, Macro, Functions and more

Quotes in %sysfunc

Reply
N/A
Posts: 0

Quotes in %sysfunc

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?
Super Contributor
Super Contributor
Posts: 3,174

Re: Quotes in %sysfunc

Posted in reply to deleted_user
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
N/A
Posts: 0

Re: Quotes in %sysfunc

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?
Super Contributor
Super Contributor
Posts: 3,174

Re: Quotes in %sysfunc

Posted in reply to deleted_user
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
SAS Super FREQ
Posts: 8,866

Re: Quotes in %sysfunc

Posted in reply to deleted_user
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
N/A
Posts: 0

Re: Quotes in %sysfunc

Posted in reply to Cynthia_sas
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
Frequent Contributor
Posts: 127

Re: Quotes in %sysfunc

Posted in reply to deleted_user
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
N/A
Posts: 0

Re: Quotes in %sysfunc

Hi Florent,

You right, %qsysfunc change behavior in second case, but do nothing in first case.
Ask a Question
Discussion stats
  • 7 replies
  • 2262 views
  • 0 likes
  • 4 in conversation