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");
Waiting for your suggestion
Thanks
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.
You would do better to switch from TRANWRD to TRANSLATE:
That does exactly what you are asking for.
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.
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;
Calling @Patrick
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.