<?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: Extract string with single quotes in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Extract-string-with-single-quotes/m-p/862655#M340756</link>
    <description>&lt;P&gt;ok, so looks like the translate() instead compress():&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let list_debug = %nrbquote(ADRESSE Adresse structure d'activité Catégorie PS Civilité Civilité d'exercice Nom prénom d'usage);

%put BEFORE: &amp;amp;=list_debug;

data _null_;
  length x $ 32767;
  x = symget('list_debug');
  x = translate(x," ","'");
  call symputx('list_debug',x);
run;

%put AFTER: &amp;amp;=list_debug;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
    <pubDate>Tue, 07 Mar 2023 13:15:41 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2023-03-07T13:15:41Z</dc:date>
    <item>
      <title>Extract string with single quotes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-string-with-single-quotes/m-p/862636#M340745</link>
      <description>&lt;P&gt;&lt;SPAN class="jCAhz ChMk0b"&gt;&lt;SPAN class="ryNqvb"&gt;Hello,&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="jCAhz ChMk0b"&gt;&lt;SPAN class="ryNqvb"&gt;I have a macro variable.&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;SPAN class="jCAhz ChMk0b"&gt;&lt;SPAN class="ryNqvb"&gt;I want to replace the apostrophes (') with the voids () but I have an error message.&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;SPAN class="jCAhz ChMk0b"&gt;&lt;SPAN class="ryNqvb"&gt;I don't know why and yet my code is perfect.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="jCAhz ChMk0b"&gt;&lt;SPAN class="ryNqvb"&gt;Can someone help me please.&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;SPAN class="jCAhz ChMk0b"&gt;&lt;SPAN class="ryNqvb"&gt;A proposal will also be welcome.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="jCAhz ChMk0b"&gt;&lt;SPAN class="ryNqvb"&gt;I apologize for not presenting a table.&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;SPAN class="jCAhz ChMk0b"&gt;&lt;SPAN class="ryNqvb"&gt;It's a macro program that I'm building but at this code level, I get this message 49 in the log.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="jCAhz ChMk0b"&gt;&lt;SPAN class="ryNqvb"&gt;Cordially,&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="jCAhz ChMk0b"&gt;&lt;SPAN class="ryNqvb"&gt;Gick&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let list_debug = ADRESSE Adresse structure d'activité Catégorie PS Civilité Civilité d'exercice Nom prénom d'usage;
%let debug_code=%sysfunc(countw(&amp;amp;list_debug.));

	   %do d = 1 %to &amp;amp;debug_code.;
			if index(%scan(&amp;amp;list_debug.,&amp;amp;d.," "),'&amp;amp;apos;') &amp;gt; 0 then %scan(&amp;amp;list_debug.,&amp;amp;d.) = tranwrd(%scan(&amp;amp;list_debug.,&amp;amp;d.),'&amp;amp;apos;'," ");
			if index(%scan(&amp;amp;list_debug.,&amp;amp;d.),'&amp;amp;amp;') &amp;gt; 0 then %scan(&amp;amp;list_debug.,&amp;amp;d.) = tranwrd(%scan(&amp;amp;list_debug.,&amp;amp;d.),'&amp;amp;amp;',"et");
		%end; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN class="jCAhz ChMk0b"&gt;&lt;SPAN class="ryNqvb"&gt;Gick&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Mar 2023 10:51:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-string-with-single-quotes/m-p/862636#M340745</guid>
      <dc:creator>Gick</dc:creator>
      <dc:date>2023-03-07T10:51:56Z</dc:date>
    </item>
    <item>
      <title>Re: Extract string with single quotes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-string-with-single-quotes/m-p/862638#M340746</link>
      <description>&lt;P&gt;Example of this code. I want to retrieve "d" but it doesn't work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let list_debug= ADRESSE Adresse structure d'activité Catégorie PS Civilité Civilité exercice Classe_age d'exercice Nationalité;

scan(&amp;amp;list_debug.,4);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Here is the error message&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="error.PNG" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/81139i4117CA8C2254AB3D/image-size/large?v=v2&amp;amp;px=999" role="button" title="error.PNG" alt="error.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Mar 2023 11:43:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-string-with-single-quotes/m-p/862638#M340746</guid>
      <dc:creator>Gick</dc:creator>
      <dc:date>2023-03-07T11:43:52Z</dc:date>
    </item>
    <item>
      <title>Re: Extract string with single quotes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-string-with-single-quotes/m-p/862642#M340747</link>
      <description>&lt;P&gt;If you want to "compress" apostrophes, maybe instead complex macrocode, try data step:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let list_debug = %nrbquote(ADRESSE Adresse structure d'activité Catégorie PS Civilité Civilité d'exercice Nom prénom d'usage);

%put BEFORE: &amp;amp;=list_debug;

data _null_;
  length x $ 32767;
  x = symget('list_debug');
  x = compress(x,"'");
  call symputx('list_debug',x);
run;

%put AFTER: &amp;amp;=list_debug;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;BTW. to create macrovariable with not matching single quotes use&amp;nbsp;&lt;CODE class=" language-sas"&gt;%nrbquote()&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 07 Mar 2023 11:48:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-string-with-single-quotes/m-p/862642#M340747</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-03-07T11:48:53Z</dc:date>
    </item>
    <item>
      <title>Re: Extract string with single quotes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-string-with-single-quotes/m-p/862646#M340750</link>
      <description>Thank you for your proposal but I am trying to recover the apostrophes to replace them with the void.&lt;BR /&gt;&lt;BR /&gt;For example at the level of the "activity" string, scan("activity",1)=d' which I will then do index(d',"&amp;amp;apos;") to know its position. The problem lies in the result of my scan which does not give what I want</description>
      <pubDate>Tue, 07 Mar 2023 12:26:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-string-with-single-quotes/m-p/862646#M340750</guid>
      <dc:creator>Gick</dc:creator>
      <dc:date>2023-03-07T12:26:52Z</dc:date>
    </item>
    <item>
      <title>Re: Extract string with single quotes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-string-with-single-quotes/m-p/862650#M340751</link>
      <description>&lt;P&gt;You wrote: "&lt;SPAN&gt;I am trying to recover the apostrophes to replace them with the void&lt;/SPAN&gt;" - but that is exactly what my example is doing.&lt;/P&gt;
&lt;P&gt;It removes all apostrophes from the text so the&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;d'activité&lt;/LI-CODE&gt;
&lt;P&gt;became&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;dactivité&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or am I missunderstanding you task?&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 07 Mar 2023 12:44:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-string-with-single-quotes/m-p/862650#M340751</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-03-07T12:44:00Z</dc:date>
    </item>
    <item>
      <title>Re: Extract string with single quotes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-string-with-single-quotes/m-p/862651#M340752</link>
      <description>No, your code compresses the result.&lt;BR /&gt;&lt;BR /&gt;I just wanted for "d'activité" to become "d activité"&lt;BR /&gt;Instead of the apostrophe it's empty now</description>
      <pubDate>Tue, 07 Mar 2023 12:48:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-string-with-single-quotes/m-p/862651#M340752</guid>
      <dc:creator>Gick</dc:creator>
      <dc:date>2023-03-07T12:48:27Z</dc:date>
    </item>
    <item>
      <title>Re: Extract string with single quotes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-string-with-single-quotes/m-p/862655#M340756</link>
      <description>&lt;P&gt;ok, so looks like the translate() instead compress():&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let list_debug = %nrbquote(ADRESSE Adresse structure d'activité Catégorie PS Civilité Civilité d'exercice Nom prénom d'usage);

%put BEFORE: &amp;amp;=list_debug;

data _null_;
  length x $ 32767;
  x = symget('list_debug');
  x = translate(x," ","'");
  call symputx('list_debug',x);
run;

%put AFTER: &amp;amp;=list_debug;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 07 Mar 2023 13:15:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-string-with-single-quotes/m-p/862655#M340756</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-03-07T13:15:41Z</dc:date>
    </item>
    <item>
      <title>Re: Extract string with single quotes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-string-with-single-quotes/m-p/862663#M340758</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/416124"&gt;@Gick&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;No, your code compresses the result.&lt;BR /&gt;&lt;BR /&gt;I just wanted for "d'activité" to become "d activité"&lt;BR /&gt;Instead of the apostrophe it's empty now&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So that is a job for TRANSLATE().&amp;nbsp; That function can replace a single byte character with another single byte characters.&amp;nbsp; So it will work to replace the ' but not the accented e (unless you are using a single byte encoding).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use the fact that it allows you to specify a list of multiple character replacements in one call to make it easier to pass in the space and the single quote by enclosing them in double quotes in both lists.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let want=%qsysfunc(translate(&amp;amp;have," ","'"));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that to the macro language everything is already a string. So the double quotes are part of the strings. So the TRANSLATE() call is going to replace the single quotes with spaces and replace the double quotes with double quotes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Mar 2023 14:16:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-string-with-single-quotes/m-p/862663#M340758</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-03-07T14:16:49Z</dc:date>
    </item>
    <item>
      <title>Re: Extract string with single quotes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-string-with-single-quotes/m-p/862669#M340761</link>
      <description>&lt;P&gt;I cannot figure out what you are trying to do.&lt;/P&gt;
&lt;P&gt;You are running a DO loop based on the number of words in the string using COUNTW() with the default set of delimiters:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;If your computer uses ASCII characters, the default delimiters are as follows:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="xisDoc-paragraph"&gt;blank ! $ % &amp;amp; ( ) * + , - . / ; &amp;lt; ^ |&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;But later you are calling the %SCAN() with just two characters as delimiter, space and double quote.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also why are you having the macro generate SAS statements?&amp;nbsp; Are you calling this macro in the middle of a DATA step or some other context where an IF statement makes sense?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And the IF statement you are generating also looks a little strange.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if index(%scan(&amp;amp;list_debug.,&amp;amp;d.," "),'&amp;amp;apos;') &amp;gt; 0
  then %scan(&amp;amp;list_debug.,&amp;amp;d.) = tranwrd(%scan(&amp;amp;list_debug.,&amp;amp;d.),'&amp;amp;apos;'," ")
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It seems to be assuming that the next word in the list is a valid SAS variable name.&amp;nbsp; (but notice that the variable using the IF condition is potentially different than the variable used in the generated assginment statement because using different delimiters in the %SCAN() calls).&lt;/P&gt;
&lt;P&gt;So I think you are trying to generate a statement like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if index(A,'&amp;amp;apos;')&amp;gt;0 then A=tranwrd(A,'&amp;amp;apos;',' ');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which you does not need to IF, if the string is not found then tranwrd() already does nothing.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;A=tranwrd(A,'&amp;amp;apos;',' ');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If also want to convert actual single quotes instead of just the HTML command to generate a single quote you can use:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;A=tranwrd(tranwrd(A,'&amp;amp;apos;',' '),"'",' ');&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 07 Mar 2023 14:56:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-string-with-single-quotes/m-p/862669#M340761</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-03-07T14:56:44Z</dc:date>
    </item>
  </channel>
</rss>

