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.



sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 1546 views
  • 0 likes
  • 7 in conversation