DATA Step, Macro, Functions and more

Converting several special characters in the same value

Reply
Contributor
Posts: 20

Converting several special characters in the same value

Hi,

I have a dataset containing the variable "memname". The values of "memname" may contain one or more Danish characters like Æ, Ø and Å. I need to convert these to AE, OE and AA respectively. The new converted values are stored in the variable "memname_dk". Examples of values with Danish characters include "ÅRSOPGØRELSE" and "ANSØGT_VILKÅR".

I have tried to use different loops to make sure that all Danish characters are converted but didn't succeed yet so in the end I decided to simply run three almost identical data steps (see below). However, I am sure there must be a more simple way to do this. Can anybody help me please?

Here's the current solution:
data aa;
set aa;
memname_dk=memname;
if find(memname_dk,'Æ') > 0 then memname_dk=tranwrd(memname,'Æ','AE');
run;
data aa;
set aa
if find(memname_dk,'Ø') > 0 then memname_dk=tranwrd(memname,'Ø','OE');
run;
data aa;
set aa
if find(memname_dk,'Å') > 0 then memname_dk=tranwrd(memname,'Å','AA');
run;

Thanks,

Helle
Valued Guide
Posts: 2,177

Re: Converting several special characters in the same value

did you try embedding the tranwrd functions?
data dealt_with ;
set original ;
memname_dk=
tranwrd(
tranwrd(
tranwrd(memname
,'Æ','AE')
,'Ø','OE')
,'Å','AA');
run;
Frequent Contributor
Posts: 106

Re: Converting several special characters in the same value

First, your design is slightly flawed, since it does not handle strings that contain a combination of these danish characters.

Second, there's no need to spread the function call over three data steps.
Third, there's no actual need for an "if" test.

Just chain the tranword function calls/statements like in e.g.
[pre]
data aa ;
set aa ;
memname_dk = tranwrd(memname,'Æ','AE');
memname_dk = tranwrd(memname_dk,'Ø','OE');
memname_dk = tranwrd(memname_dk,'Å','AA');
run ;
[/pre]
Contributor
Posts: 20

Re: Converting several special characters in the same value

Posted in reply to Robert_Bardos
Hi,

Thanks a lot for your suggestions - they both give me the desired results :-)

Regards,

Helle
Ask a Question
Discussion stats
  • 3 replies
  • 473 views
  • 0 likes
  • 3 in conversation