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

Hi,

I have a String, say str = 'a_b_cd___efg_h__ijkl___m'. I need this string to be translated to 'a b cd    efg h   ijkl    m'. I have tried TRANSLATE(str,' ','_'). But all I see is 'a b cd efg h ijkl m'. The number of spaces placed between the characters is not the same as the number of underscores present, if there are multiple undersocres adjacant.

Any help on this would be great.

Thanks,

Sai Chaitanya

1 ACCEPTED SOLUTION

Accepted Solutions
Loko
Barite | Level 11

Hello,

data _null_;

str = 'a_b_cd___efg_h__ijkl___m';
want=prxchange('s/_/ /i',-1,str);
put want=;

run;

View solution in original post

8 REPLIES 8
chrej5am
Quartz | Level 8

Hi,

for me it works well. For example in this context

data my;

str = 'a_b_cd___efg_h__ijkl___m';

a=TRANSLATE(str,' ','_');

b=TRANWRD(str,'_',' ');

run;

a=a b cd   efg h  ijkl   m;

b=a b cd   efg h  ijkl   m;

Maybe the problem is elsewhere?

Jakub

Reeza
Super User

What version of SAS are you using? SAS studio or SAS UE? The HTML editor may be 'eating' the spaces.

Using a put statement shows the actual variable.

ChaitanyaEmani
Calcite | Level 5

Hi Reeza,

I am using SAS 9.2 on mainframe. There is no HTML used and all the output is sent to a flat file on mainframe. There are many such strings in the input file with '_' characters and all of them have to be replaced with spaces. Unable to decode on why the TRANSLATE/TRANWRD is not working for me unlike Jakub.

Thanks,

Sai Chaitanya

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

2 suggestions.  Firstly post exactly what you are doing code/file so we can see.  Secondly, open the file using a hex editor and check what those characters are in reality, sometimes special characters do odd things.

Also, if you cant get trawrd to work, you cold always parse the string as loop:

do I=1 to length(strip(variable));

     if substr(variable,i,1) in ("_") then substr(variable,i,1)=" ";

end;

Loko
Barite | Level 11

Hello,

data _null_;

str = 'a_b_cd___efg_h__ijkl___m';
want=prxchange('s/_/ /i',-1,str);
put want=;

run;

ChaitanyaEmani
Calcite | Level 5

Hi Loko,

Regular expression worked for me. Smiley Happy

Thanks,

Sai Chaitanya

Lastwish
Calcite | Level 5

Can you please explain what does 'i' and '-1' stand for in the regexp ?

prxchange('s/_/ /i',-1,str);

archcnu
Calcite | Level 5

prxchange('s/_/ /i',-1,str);

The value i indicates to ignore the case of the character in the expression. For this particular example i can be ignored.

The value -1 indicates that the matching pattern replacement should happen till the end of string is reached. If 1 is used the replacement ends when the first match is found.



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

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 8 replies
  • 3014 views
  • 0 likes
  • 7 in conversation