Hello,
I have a macro variable. I want to replace the apostrophes (') with the voids () but I have an error message. I don't know why and yet my code is perfect.
Can someone help me please. A proposal will also be welcome.
I apologize for not presenting a table. It's a macro program that I'm building but at this code level, I get this message 49 in the log.
Cordially,
Gick
%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(&list_debug.));
%do d = 1 %to &debug_code.;
if index(%scan(&list_debug.,&d.," "),''') > 0 then %scan(&list_debug.,&d.) = tranwrd(%scan(&list_debug.,&d.),'''," ");
if index(%scan(&list_debug.,&d.),'&') > 0 then %scan(&list_debug.,&d.) = tranwrd(%scan(&list_debug.,&d.),'&',"et");
%end;
Gick
Example of this code. I want to retrieve "d" but it doesn't work.
%let list_debug= ADRESSE Adresse structure d'activité Catégorie PS Civilité Civilité exercice Classe_age d'exercice Nationalité;
scan(&list_debug.,4);
Here is the error message
If you want to "compress" apostrophes, maybe instead complex macrocode, try data step:
%let list_debug = %nrbquote(ADRESSE Adresse structure d'activité Catégorie PS Civilité Civilité d'exercice Nom prénom d'usage);
%put BEFORE: &=list_debug;
data _null_;
length x $ 32767;
x = symget('list_debug');
x = compress(x,"'");
call symputx('list_debug',x);
run;
%put AFTER: &=list_debug;
BTW. to create macrovariable with not matching single quotes use %nrbquote()
Bart
You wrote: "I am trying to recover the apostrophes to replace them with the void" - but that is exactly what my example is doing.
It removes all apostrophes from the text so the
d'activité
became
dactivité
Or am I missunderstanding you task?
Bart
ok, so looks like the translate() instead compress():
%let list_debug = %nrbquote(ADRESSE Adresse structure d'activité Catégorie PS Civilité Civilité d'exercice Nom prénom d'usage);
%put BEFORE: &=list_debug;
data _null_;
length x $ 32767;
x = symget('list_debug');
x = translate(x," ","'");
call symputx('list_debug',x);
run;
%put AFTER: &=list_debug;
Bart
@Gick wrote:
No, your code compresses the result.
I just wanted for "d'activité" to become "d activité"
Instead of the apostrophe it's empty now
So that is a job for TRANSLATE(). That function can replace a single byte character with another single byte characters. So it will work to replace the ' but not the accented e (unless you are using a single byte encoding).
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.
%let want=%qsysfunc(translate(&have," ","'"));
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.
I cannot figure out what you are trying to do.
You are running a DO loop based on the number of words in the string using COUNTW() with the default set of delimiters:
If your computer uses ASCII characters, the default delimiters are as follows:
blank ! $ % & ( ) * + , - . / ; < ^ |
But later you are calling the %SCAN() with just two characters as delimiter, space and double quote.
Also why are you having the macro generate SAS statements? Are you calling this macro in the middle of a DATA step or some other context where an IF statement makes sense?
And the IF statement you are generating also looks a little strange.
if index(%scan(&list_debug.,&d.," "),''') > 0
then %scan(&list_debug.,&d.) = tranwrd(%scan(&list_debug.,&d.),'''," ")
;
It seems to be assuming that the next word in the list is a valid SAS variable name. (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).
So I think you are trying to generate a statement like this:
if index(A,''')>0 then A=tranwrd(A,''',' ');
Which you does not need to IF, if the string is not found then tranwrd() already does nothing.
A=tranwrd(A,''',' ');
If also want to convert actual single quotes instead of just the HTML command to generate a single quote you can use:
A=tranwrd(tranwrd(A,''',' '),"'",' ');
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.