01-28-2015 12:54 AM
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.
01-28-2015 02:37 AM
for me it works well. For example in this context
str = 'a_b_cd___efg_h__ijkl___m';
a=a b cd efg h ijkl m;
b=a b cd efg h ijkl m;
Maybe the problem is elsewhere?
01-28-2015 02:52 AM
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.
01-28-2015 04:01 AM
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.
01-28-2015 04:11 AM
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)=" ";
01-29-2015 10:06 AM
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.
Need further help from the community? Please ask a new question.