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

Hi,

 

I was stuck with an issue that is related to macro variable to have a single quoted string.

Here is the sample code:

/* Case 1 */
%LET var_a = 'Apple, Orange and Peach (AOP) Garden Fruits';
%PUT &var_a;

/* Case 2 */
%LET var_b = 'Apple, Orange and Peach (AOP) Garden Fruits'|'Apple Pine and Banana';
%LET var_b1 = %scan(&var_b, 1,'|');
%LET var_b2 = %str(&VAR_b1);

%PUT &var_b;
%PUT &var_b1;     
%PUT &var_b2;     

/* Case 3 */
%LET var_c = "Apple, Orange and Peach (AOP) Garden Fruits"|"Apple Pine and Banana";
%LET var_c1 = %scan(&var_c, 1,'|');
%LET var_c2 = %str(&var_c1);

%PUT &var_c.;
%PUT &var_c1;
%PUT &var_c2;

Log:

40
41   /* Case 1 */
42   %LET var_a = 'Apple, Orange and Peach (AOP) Garden Fruits';
43   %PUT &var_a;
'Apple, Orange and Peach (AOP) Garden Fruits'
44
45   /* Case 2 */
46   %LET var_b = 'Apple, Orange and Peach (AOP) Garden Fruits'|'Apple Pine and Banana';
47   %LET var_b1 = %scan(&var_b, 1,'|');
48   %LET var_b2 = %str(&VAR_b1);
49
50   %PUT &var_b;
'Apple, Orange and Peach (AOP) Garden Fruits'|'Apple Pine and Banana'
51   %PUT &var_b1;                                                    
Apple, Orange and Peach (AOP) Garden Fruits         /****** Lost the single quotation, Not expected to have ******/
52   %PUT &var_b2;
Apple, Orange and Peach (AOP) Garden Fruits        /****** Lost the single quotation, Not expected to have ******/
53
54   /* Case 3 */
55   %LET var_c = "Apple, Orange and Peach (AOP) Garden Fruits"|"Apple Pine and Banana";
56   %LET var_c1 = %scan(&var_c, 1,'|');
57   %LET var_c2 = %str(&var_c1);
58
59   %PUT &var_c.;
"Apple, Orange and Peach (AOP) Garden Fruits"|"Apple Pine and Banana"
60   %PUT &var_c1;
"Apple, Orange and Peach (AOP) Garden Fruits"
61   %PUT &var_c2;
"Apple, Orange and Peach (AOP) Garden Fruits"

What I wanted is from:

%LET var_b = 'Apple, Orange and Peach (AOP) Garden Fruits'|'Apple Pine and Banana';

Or from:

%LET var_c = "Apple, Orange and Peach (AOP) Garden Fruits"|"Apple Pine and Banana";

To get the single quoted value for a macro variable on line 51 or 52 as:

'Apple, Orange and Peach (AOP) Garden Fruits'

 

The single quoted value for a macro variable is my purpose as this macro variable is used in a statement within a double quote. A double quoted value will not work.

 

Thank you so much!  

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi @leehsin  If I understand your requirement correctly, you do not need to enclose the delimiter | in quotes '|'  in your scan function %LET var_b1 = %scan(&var_b, 1,'|');

 

So,

%LET var_b1 = %scan(&var_b, 1,|);

should suffice?

View solution in original post

12 REPLIES 12
novinosrin
Tourmaline | Level 20

Hi @leehsin  If I understand your requirement correctly, you do not need to enclose the delimiter | in quotes '|'  in your scan function %LET var_b1 = %scan(&var_b, 1,'|');

 

So,

%LET var_b1 = %scan(&var_b, 1,|);

should suffice?

leehsin
Quartz | Level 8
I tested this simple solution in Base SAS. It works. That's great! However, when I run this method with my real program in linux server, it still has no single quotations.
PaigeMiller
Diamond | Level 26

@leehsin wrote:
I tested this simple solution in Base SAS. It works. That's great! However, when I run this method with my real program in linux server, it still has no single quotations.

We don't know what this means, or what code you ran, or what the incorrect result is. Please show us the code and the LOG and any incorrect output. The LOG needs to have the formatting preserved, you do this by pasting the LOG into the window that appears when you click on the </> icon.

--
Paige Miller
leehsin
Quartz | Level 8
I tested the sample code with the way that novinnosrin suggested in Linux server. I got the single quoted string. However, I didn't get the single quoted string when I change '|' to | in my real code. This code is existed in a macro program within a %DO loop. It's exactly written as %LET var_b1 = %scan(&var_b, &i.,|), and &var_b has a value exactly like the way as 'Apple, Orange and Peach (AOP) Garden Fruits'|'Apple Pine and Banana'. The reason I need a single quoted value for this &var_b is because I can't use '&var_b' to get the value of this macro variable, and I can't use "&var_b" because this part is inside an existing " " sentence. I couldn't understand why it doesn't work.

There is no error or warning message in the log except at the execution step that uses the value of the macro variable which has no quotations. In terms of presenting the real code and log, I need to copy all the contents of log that are related to these several lines of code from different macro programs. I can't do this because of the sensitivities of data controlled by rules in our company.
Tom
Super User Tom
Super User

When you have quotes inside a quoted string they need to be doubled.

charvar="The value ""&mvar"" was saved";

You can use the QUOTE() function to help with this.

%let mvar=A and B;
charvar=%sysfunc(quote(The value "&mvar" was saved));

%let mvar="A and B";
charvar=%sysfunc(quote(The value &mvar was saved));

If you really need the single quotes you can also use QUOTE() by using the optional second parameter. 

charvar="The value %sysfunc(quote(%superq(mvar),%str(%'))) was saved";
leehsin
Quartz | Level 8
Thank you, Tom!
PaigeMiller
Diamond | Level 26

@leehsin wrote:
I tested the sample code with the way that novinnosrin suggested in Linux server. I got the single quoted string. However, I didn't get the single quoted string when I change '|' to | in my real code. This code is existed in a macro program within a %DO loop. It's exactly written as %LET var_b1 = %scan(&var_b, &i.,|), and &var_b has a value exactly like the way as 'Apple, Orange and Peach (AOP) Garden Fruits'|'Apple Pine and Banana'. The reason I need a single quoted value for this &var_b is because I can't use '&var_b' to get the value of this macro variable, and I can't use "&var_b" because this part is inside an existing " " sentence. I couldn't understand why it doesn't work.

There is no error or warning message in the log except at the execution step that uses the value of the macro variable which has no quotations. In terms of presenting the real code and log, I need to copy all the contents of log that are related to these several lines of code from different macro programs. I can't do this because of the sensitivities of data controlled by rules in our company.

I'm not sure what you mean by this: "The reason I need a single quoted value for this &var_b is because I can't use '&var_b' to get the value of this macro variable, and I can't use "&var_b" because this part is inside an existing " " sentence." I have used macro variables with no quotes around their values for decades and I have never run into a problem, and never found a situation where quotes around macro variables were needed and it wouldn't work otherwise. I consider your statement to be incorrect.

 

In my earlier code, I suggested (because it probably doesn't work otherwise) that you use %bquote(&var_b) inside %scan. with no quotes in &var_b. Did you try this?

 

But anyway, we can't help you if we can't see your code. SHOW US THE CODE.

And we can't help you if we can't see your log. SHOW US THE LOG.

--
Paige Miller
Quentin
Super User

Hi,

 

The code @novinosrin works for the examples you've posted, and the example you just described:

5    %let var_b='Apple, Orange and Peach (AOP) Garden Fruits'|'Apple Pine and Banana' ;
6    %let i=1 ;
7    %LET var_b1 = %scan(&var_b, &i.,|) ;
8    %let i=2 ;
9    %LET var_b2 = %scan(&var_b, &i.,|) ;
10
11   %put &var_b1 ;
'Apple, Orange and Peach (AOP) Garden Fruits'
12   %put &var_b2 ;
'Apple Pine and Banana'

I *think* if every item in your pipe-delimited lists is in single quotes, and there are no internal single quotes, it should work fine for any value.

 

I would suggest you try to make a little example like above (or like your initial question, which was well-explained), which shows a circumstance where %scan(&var_b, &i.,|)  is not working.  If we can see those values that are causing the problem, we'll be able to find a solution.

 

(And I do agree with others, generally quotes in the value of a macro variable is more likely to be a problem, than a solution.)

BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
leehsin
Quartz | Level 8

This issue was resolved and I adopted the solution suggested by novinosrin. 

 

When dealing with strings containing words and special characters, a macro variable has some limitations. Since a macro variable is set to be used dynamically, and it is called by various programs including macro programs, it increases the complicacy of usage. Quotation marks are necessary in many functions, and the interpretation of  a macro variable can only be within a pair of double quotation marks. Therefore, to pre-assign the single quotation mark to a text value containing words and special characters is very necessary and not avoidable in some cases. This topic is intended to find a good solution that I am not aware of for this specific technical issue.

 

Thanks to all for valuable suggestions!

 

Regards,

PaigeMiller
Diamond | Level 26

Only in rare cases would you put quotes around the value of a macro variable. SO I would advise you to remove the quotes. Similarly, you would not put character strings in quotes in a macro functions like %SCAN.

 

So, this works

 

%LET var_b = Apple, Orange and Peach (AOP) Garden Fruits | Apple Pine and Banana ;
%LET var_b1 = %scan(%bquote(&var_b), 1,|);
%LET var_b2 = &VAR_b1;

%put &=var_b;
%put &=var_b1;
%put &=var_b2;

The %bquote is needed on line 2, otherwise the comma in &VAR_B is interpreted as separating arguments in the function %SCAN, and this is not what the comma represents, so %BQUOTE forces the comma to be seen as a text comma.

--
Paige Miller
leehsin
Quartz | Level 8
Thanks, PaigeMiller !

My goal is to have a single quoted value for a macro variable, i.e., I need the value exactly to be as this;
'Apple, Orange and Peach (AOP) Garden Fruits'
PaigeMiller
Diamond | Level 26

@leehsin wrote:

My goal is to have a single quoted value for a macro variable, i.e., I need the value exactly to be as this;
'Apple, Orange and Peach (AOP) Garden Fruits'

I think you are making your life difficult by insisting that macro variables have quotes around their values. This could lead to problems further down the line. So, I stick with my advice to not put quotes around your macro variable values.

--
Paige Miller

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 12 replies
  • 1074 views
  • 1 like
  • 5 in conversation