DATA Step, Macro, Functions and more

Find and replace a substring

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Find and replace a substring

I have this problem: some values in VAR1 (see example) have special characters ( À   Á   Â   Ã   Ä   ). I want to find these special characters in a string and replace it with the correct letter (A) as showed in VAR2 of the example.

Var1Var2
SIAHMEDSIAHMED
DANDREÁDANDREA
PELLIÇANOPELLICANO
MATÃMATA
DONZELLADONZELLA
DAGOSTINODAGOSTINO
ÔTTINAOTTINA

 

I tried this instruction

var2 = TRANWRD (var1, "Á ", "A");  and it works but if I include more than one target it doesn't

var2 = TRANWRD (var1, "Á" or "Ã", "A");

 

Waiting for your suggestion

Thanks


Accepted Solutions
Solution
‎10-05-2017 04:18 AM
Trusted Advisor
Posts: 1,022

Re: Find and replace a substring

Check out the TRANSLATE function, which expression like the below, which converts all vowels to X.

 

 

     name=translate(name,'XXXXX','AEIOU');

 

So presumably you could use

 

  var2=translate(var1,'AAAAA','ÀÁÂÃÄ');

 

Just be sure that the second argument has as many iterations of the common value ('A') as there are characters in the 3rd argument.

 

 

View solution in original post


All Replies
Super User
Posts: 5,509

Re: Find and replace a substring

You would do better to switch from TRANWRD to TRANSLATE:

 

http://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.2&docsetId=lefunctionsref&docsetTarge...

 

That does exactly what you are asking for.

Solution
‎10-05-2017 04:18 AM
Trusted Advisor
Posts: 1,022

Re: Find and replace a substring

Check out the TRANSLATE function, which expression like the below, which converts all vowels to X.

 

 

     name=translate(name,'XXXXX','AEIOU');

 

So presumably you could use

 

  var2=translate(var1,'AAAAA','ÀÁÂÃÄ');

 

Just be sure that the second argument has as many iterations of the common value ('A') as there are characters in the 3rd argument.

 

 

Occasional Contributor
Posts: 5

Re: Find and replace a substring

Referring to my previous question, I write two lines together as below but the instruction doesn’t work.
Do I have to split for different dataset one instruction at a time?

Thank you
Stefy67


data tre;
set due;
nomenew3 = translate(nomenew2,'AAAAAAAAAAA','ÀÁÂÃÄÅÆĀĂĄǍ');
cognomenew3 = translate(cognomenew2,'AAAAAAAAAAA','ÀÁÂÃÄÅÆĀĂĄǍ');

run;


Respected Advisor
Posts: 4,173

Re: Find and replace a substring

[ Edited ]

@Stefy67

Below one way to go. The approach works only for single byte character sets and requires one of the more recent SAS versions.

data due;
  nomenew2 = 'À à ÁÂÃÄÅ ÆAAAA è';
  cognomenew2 = 'ÔTTINA ÀÁÂÃÄÅÆAAAA';
run;

proc format;
   invalue $norm_to_A (default=256) 's/[ÀÁÂÃÄÅÆäà]/A/'   (regexpe) = _same_ other=_same_;
   invalue $norm_to_O (default=256) 's/[Ô]/O/'             (regexpe) = _same_ other=_same_;
   invalue $norm_to_E (default=256) 's/[è]/E/'             (regexpe) = _same_ other=_same_;
run;

data tre;
  set due;
  array c_vars {*} nomenew2 cognomenew2;
  do _i=1 to dim(c_vars);
    do _infmt='$norm_to_A','$norm_to_O','$norm_to_E';
  	 c_vars[_i]=inputc(c_vars[_i],_infmt);
    end;
  end;
run;
Occasional Contributor
Posts: 5

Re: Find and replace a substring

Thanks Patrick

I solved the problem with your suggestions.


Super User
Posts: 10,028

Re: Find and replace a substring

Calling @Patrick

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 90 views
  • 0 likes
  • 5 in conversation