<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Using a single quote within a string literal within a macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-a-single-quote-within-a-string-literal-within-a-macro/m-p/887479#M350625</link>
    <description>&lt;P&gt;You don't have to escape (SAS docs call this quoting, many SAS folks refer to it as masking) a period.&amp;nbsp; You do have to mask the apostrophe, and the comma.&amp;nbsp; If you are hard-coding the string, the easiest way to mask these values is with %str().&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let string= John F. Kennedy | John F. Kennedy%str(,) Jr. | Kelly O%str(%')Donnell;

%put &amp;amp;=string;

%let paragraph_length=0;

%macro TEXTPARSE(inset=,outset=,fname_full=,tstr=,ln=) ;
  %put &amp;amp;=tstr ;
%mend TEXTPARSE ;
	
%TEXTPARSE(inset=ncsr1,outset=ncsr2,fname_full=fname2,tstr= &amp;amp;string, ln= &amp;amp;paragraph_length)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that depending on the code inside of your TEXTPARSE macro, it may be unquoting (unmasking) the problematic characters.&amp;nbsp; So it's possible when you run this you will still get errors.&amp;nbsp; If that's the case, please show an example of your TEXTPARSE macro which reproduces the problem.&amp;nbsp; The answer will likely be to add more quoting inside the macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm confused by your comment that the pipe will be used as a logical OR in your macro.&amp;nbsp; I don't see how that could work with your current string, because you cannot have code like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if &amp;amp;name = John F. Kennedy | John F. Kennedy%str(,) Jr. | Kelly O%str(%')Donnel ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Perhaps you are looping over this pipe-delimited list and generating a valid OR expression, such as:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if &amp;amp;name = John F. Kennedy | &amp;amp;name = John F. Kennedy%str(,) Jr. | &amp;amp;name = Kelly O%str(%')Donnel ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 02 Aug 2023 12:32:58 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2023-08-02T12:32:58Z</dc:date>
    <item>
      <title>Using a single quote within a string literal within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-single-quote-within-a-string-literal-within-a-macro/m-p/887449#M350605</link>
      <description>&lt;P&gt;I need to define a macro variable that can take three string literals. The string literals are separated by a pipe character ("|"), which is thus being used as an "OR" operator. The string literals are the full names of three people so present certain challenges when it comes to coding. The code I am having issues with is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%let string= John F\. Kennedy | %quote(John F. Kennedy, Jr.) | Kelly O'Donnell;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As can be seen from the code snippet:&lt;/P&gt;&lt;P&gt;1) The first name has a period (.) so I "escaped" it using a backslash. No issues for me here.&lt;/P&gt;&lt;P&gt;2) The second name has a comma (,) so I enclosed the entire name in %quote(). Again, no issues for me here.&lt;/P&gt;&lt;P&gt;3) The third name has a single quote ('). I tried putting a backslash in front of the quote, or using the %quote() method. Neither of these methods work for me, and all code after the apostrophe is treated as one long string. &lt;STRONG&gt;How can I appropriately "escape" the single quote in the third name?&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code snippet above is a part of the bigger code below. The code is attempting to look for the three names in the corporate filings of companies.&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;PRE&gt;data ncsr1; set wrdssec.wrds_forms; format FNAME2 $100. ;
where form = "N-CSR" and year(FDATE) = 2018;
FNAME2 = cats("/wrds/sec/wrds_clean_filings/",WRDSFNAME);
drop FINDEXDATE LINDEXDATE FNAME WRDSFNAME;
run;

%let string= John F\. Kennedy | %quote(John F. Kennedy, Jr.) | Kelly O'Donnell;

%let paragraph_length=0;
	
%TEXTPARSE(inset=ncsr1,outset=ncsr2,fname_full=fname2,tstr= &amp;amp;string, ln= &amp;amp;paragraph_length);&lt;/PRE&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 02 Aug 2023 09:03:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-single-quote-within-a-string-literal-within-a-macro/m-p/887449#M350605</guid>
      <dc:creator>Tasneem</dc:creator>
      <dc:date>2023-08-02T09:03:09Z</dc:date>
    </item>
    <item>
      <title>Re: Using a single quote within a string literal within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-single-quote-within-a-string-literal-within-a-macro/m-p/887471#M350620</link>
      <description>&lt;P&gt;Obviously, the result you want needs to be sent through macro %TEXTPARSE in order to work, and you don't provide the code in that macro, so we can't help directly with that part unless you provide us with that code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Nevertheless, this works:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let string= John F\. Kennedy | John F. Kennedy, Jr. | Kelly O%str(%')Donnell;

%put &amp;amp;=string;

%let substr1 = %qscan(%bquote(&amp;amp;string),1,|); %put &amp;amp;=substr1;
%let substr2 = %qscan(%bquote(&amp;amp;string),2,|); %put &amp;amp;=substr2;
%let substr3 = %qscan(%bquote(&amp;amp;string),3,|); %put &amp;amp;=substr3;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It also works without the backslash&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Aug 2023 12:04:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-single-quote-within-a-string-literal-within-a-macro/m-p/887471#M350620</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-08-02T12:04:33Z</dc:date>
    </item>
    <item>
      <title>Re: Using a single quote within a string literal within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-single-quote-within-a-string-literal-within-a-macro/m-p/887476#M350624</link>
      <description>&lt;P&gt;Why do you think that the period would cause any trouble?&amp;nbsp; That is not one of the special characters to the SAS macro language.&lt;/P&gt;
&lt;P&gt;Why do you think that using a backslash "escapes" the period?&amp;nbsp; &amp;nbsp;Are you planning to use that string to generate a Regular Expression at some point down the line?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why don't you just add actual quotes around the whole thing and then the macro processor will ignore all of the characters that are special to it.&amp;nbsp; If the goal is to use the values to generate SAS code then you will need quotes around the strings anyway.&amp;nbsp; For the string with the single quote you an either use double quotes on the outside&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let string='John F. Kennedy'|'John F. Kennedy, Jr.'|"Kelly O'Donnell";&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or when using single quotes double them up.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let string='John F. Kennedy'|'John F. Kennedy, Jr.'|'Kelly O''Donnell';
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you really cannot add actual quotes you can use the %BQUOTE() function to add macro quoting instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let string=%bquote(John F. Kennedy|John F. Kennedy, Jr.|Kelly O'Donnell);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And if you are going to use the string in some regular expression then add the backslashes where RegEx needs them.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let string=%bquote(John F\. Kennedy|John F\. Kennedy\, Jr\.|Kelly O\'Donnell);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 02 Aug 2023 12:17:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-single-quote-within-a-string-literal-within-a-macro/m-p/887476#M350624</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-02T12:17:40Z</dc:date>
    </item>
    <item>
      <title>Re: Using a single quote within a string literal within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-single-quote-within-a-string-literal-within-a-macro/m-p/887479#M350625</link>
      <description>&lt;P&gt;You don't have to escape (SAS docs call this quoting, many SAS folks refer to it as masking) a period.&amp;nbsp; You do have to mask the apostrophe, and the comma.&amp;nbsp; If you are hard-coding the string, the easiest way to mask these values is with %str().&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let string= John F. Kennedy | John F. Kennedy%str(,) Jr. | Kelly O%str(%')Donnell;

%put &amp;amp;=string;

%let paragraph_length=0;

%macro TEXTPARSE(inset=,outset=,fname_full=,tstr=,ln=) ;
  %put &amp;amp;=tstr ;
%mend TEXTPARSE ;
	
%TEXTPARSE(inset=ncsr1,outset=ncsr2,fname_full=fname2,tstr= &amp;amp;string, ln= &amp;amp;paragraph_length)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that depending on the code inside of your TEXTPARSE macro, it may be unquoting (unmasking) the problematic characters.&amp;nbsp; So it's possible when you run this you will still get errors.&amp;nbsp; If that's the case, please show an example of your TEXTPARSE macro which reproduces the problem.&amp;nbsp; The answer will likely be to add more quoting inside the macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm confused by your comment that the pipe will be used as a logical OR in your macro.&amp;nbsp; I don't see how that could work with your current string, because you cannot have code like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if &amp;amp;name = John F. Kennedy | John F. Kennedy%str(,) Jr. | Kelly O%str(%')Donnel ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Perhaps you are looping over this pipe-delimited list and generating a valid OR expression, such as:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if &amp;amp;name = John F. Kennedy | &amp;amp;name = John F. Kennedy%str(,) Jr. | &amp;amp;name = Kelly O%str(%')Donnel ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 02 Aug 2023 12:32:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-single-quote-within-a-string-literal-within-a-macro/m-p/887479#M350625</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-08-02T12:32:58Z</dc:date>
    </item>
    <item>
      <title>Re: Using a single quote within a string literal within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-single-quote-within-a-string-literal-within-a-macro/m-p/887603#M350663</link>
      <description>&lt;P&gt;You were correct. I was incorrectly assuming the pipe was being used as an OR operator. I am working around that issue myself. As for my original question, it appears using %str() to mask commas and apostrophes in the names helps. Cheers!&lt;/P&gt;</description>
      <pubDate>Thu, 03 Aug 2023 01:10:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-single-quote-within-a-string-literal-within-a-macro/m-p/887603#M350663</guid>
      <dc:creator>Tasneem</dc:creator>
      <dc:date>2023-08-03T01:10:20Z</dc:date>
    </item>
  </channel>
</rss>

