## Find and replace a substring

Solved
Occasional Contributor
Posts: 7

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

 Var1 Var2 SIAHMED SIAHMED DANDREÁ DANDREA PELLIÇANO PELLICANO MATÃ MATA DONZELLA DONZELLA DAGOSTINO DAGOSTINO ÔTTINA OTTINA

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");

Thanks

Accepted Solutions
Solution
‎10-05-2017 04:18 AM
Posts: 1,345

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

All Replies
Super User
Posts: 6,785

## 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
Posts: 1,345

## 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: 7

## 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;

Posts: 4,741

## 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: 7

## Re: Find and replace a substring

Thanks Patrick

I solved the problem with your suggestions.

Super User
Posts: 10,787

## Re: Find and replace a substring

Calling @Patrick

☑ This topic is solved.

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