BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Stefy67
Fluorite | Level 6

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

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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.

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

6 REPLIES 6
Astounding
PROC Star

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.

mkeintz
PROC Star

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.

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Stefy67
Fluorite | Level 6
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;


Patrick
Opal | Level 21

@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;
Stefy67
Fluorite | Level 6
Thanks Patrick

I solved the problem with your suggestions.


Ksharp
Super User

Calling @Patrick

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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