Concatenation of quoted macro variables

I'm just trying to combine two macro commands with quotation marks, but it seems like this is not an easy path.

Let's say that we have:

%LET VAR1="This is not the greatest song in the world"; %LET VAR2="this is just a tribute."; %LET TRIBUTE=%SYSFUNC(CATX(%STR( ),&VAR1,&VAR2)); %PUT &TRIBUTE; 

I really want:

  "This is not the greatest song in the world this is just a tribute." 

But the above code really gives:

 "This is not the greatest song in the world" "this is just a tribute." 

So I'm trying to put %QUOTE() , %BQUOTE , etc. around &VAR1 and %VAR2 in the hope of exposing quotes, but I get the same result.

The only thing that works for me is:

  %LET TRIBUTE="%SUBSTR(&VAR1.,2,%LENGTH(&VAR1.)-2) %SUBSTR(&VAR2.,2,%LENGTH(&VAR2.)-2)"; 

But it is ugly and can be very fast. Isn't there a better way to do this?

+5
source share
2 answers

You can use COMPRESS for this.

 %LET VAR1="This is not the greatest song in the world"; %LET VAR2="this is just a tribute."; %let VAR3=%sysfunc(compress(&VAR1,%str(%"))); %put &=var1 &=var3; 

It's a little tricky to remove quotes, but it works.

You can also do this in an FCMP function or function style macro; here is one example.

 %macro unquote_string(string=); %sysfunc(compress(&string.,%str(%'%"))) %mend unquote_string; %let VAR3="%unquote_string(string=&var1.) %unquote_string(string=&var2.)"; %put &=var3.; 

Please note: you should not use CAT functions to concatenate macro variables. They are just text, so entering them one by one automatically combines them.

However, the real answer to your question “Is there a better way” is not to store quotation marks in a macro variable. Most of the time you should store the macro without quotes and use it in quotes when necessary. SAS macros do not respect quotes as something special - they are just a character in a string - so they don’t have a special tool to solve this problem.

+2
source

I'm going to rephrase Joe's “real answer”, which is - don't store quotes in a macro variable. Single and double quotes in the macro language are no different from any other character. What you need to do is to delay the introduction of quotes until they need you. This will lead to much cleaner, more flexible, easier to read and error free.

The code:

Notice that I removed the quotes and concatenated the strings, which I just list them one by one:

 %LET VAR1=This is not the greatest song in the world; %LET VAR2=this is just a tribute.; %LET TRIBUTE=&VAR1 &VAR2; 

Example 1

Quotation marks are not required to print the desired line, since we use the %put operator in this first example - for this reason I left the quotation marks:

 %PUT &TRIBUTE; 

Exit:

 This is not the greatest song in the world this is just a tribute. 

Example 2

Quotes are required because we are now on earth with data:

 data _null_; put "&TRIBUTE"; run; 

Exit:

 This is not the greatest song in the world this is just a tribute. 

Note that both of these examples assume that you really do not want to print quotation marks on the screen.

+4
source

Source: https://habr.com/ru/post/1201264/


All Articles