Help using Base SAS procedures

Replacing '_' with ' '(Spaces)

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Replacing '_' with ' '(Spaces)

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


Accepted Solutions
Solution
‎01-28-2015 03:51 AM
Super Contributor
Posts: 305

Re: Replacing '_' with ' '(Spaces)

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


All Replies
Contributor
Posts: 42

Re: Replacing '_' with ' '(Spaces)

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

Super User
Posts: 17,819

Re: Replacing '_' with ' '(Spaces)

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.

New Contributor
Posts: 3

Re: Replacing '_' with ' '(Spaces)

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

Super User
Super User
Posts: 7,401

Re: Replacing '_' with ' '(Spaces)

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;

Solution
‎01-28-2015 03:51 AM
Super Contributor
Posts: 305

Re: Replacing '_' with ' '(Spaces)

Hello,

data _null_;

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

run;

New Contributor
Posts: 3

Re: Replacing '_' with ' '(Spaces)

Hi Loko,

Regular expression worked for me. Smiley Happy

Thanks,

Sai Chaitanya

New Contributor
Posts: 4

Re: Replacing '_' with ' '(Spaces)

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

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

New Contributor
Posts: 3

Re: Replacing '_' with ' '(Spaces)

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.



☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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