BookmarkSubscribeRSS Feed
Gick
Pyrite | Level 9

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

8 REPLIES 8
Gick
Pyrite | Level 9

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

error.PNG

yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Gick
Pyrite | Level 9
Thank you for your proposal but I am trying to recover the apostrophes to replace them with the void.

For example at the level of the "activity" string, scan("activity",1)=d' which I will then do index(d',"'") to know its position. The problem lies in the result of my scan which does not give what I want
yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Gick
Pyrite | Level 9
No, your code compresses the result.

I just wanted for "d'activité" to become "d activité"
Instead of the apostrophe it's empty now
yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Tom
Super User Tom
Super User

@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.

 

Tom
Super User Tom
Super User

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.," "),'&apos;') > 0
  then %scan(&list_debug.,&d.) = tranwrd(%scan(&list_debug.,&d.),'&apos;'," ")
;

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,'&apos;')>0 then A=tranwrd(A,'&apos;',' ');

Which you does not need to IF, if the string is not found then tranwrd() already does nothing.

A=tranwrd(A,'&apos;',' ');

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,'&apos;',' '),"'",' ');

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 8 replies
  • 994 views
  • 0 likes
  • 3 in conversation