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
Hello,
data _null_;
str = 'a_b_cd___efg_h__ijkl___m';
want=prxchange('s/_/ /i',-1,str);
put want=;
run;
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
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.
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
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;
Hello,
data _null_;
str = 'a_b_cd___efg_h__ijkl___m';
want=prxchange('s/_/ /i',-1,str);
put want=;
run;
Hi Loko,
Regular expression worked for me.
Thanks,
Sai Chaitanya
Can you please explain what does 'i' and '-1' stand for in the regexp ?
prxchange('s/_/ /i',-1,str);
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.