I have a string like this:
ABCD 0101 (only 1 space in middle)
I want ABCD_0101
I tried:
TRANWRD (trim("ABCD 0101")," ","_")
And also:
TRANSLATE (trim(("ABCD 0101"),"_"," ")
And the output is:ABCD0101 (both cases)
What did i do wrong?
Eagles,
The to and from locations are not always the same in every function. The only difference I see in our code is the "trim" function and I placed my translate result into a new variable.
9445 data _null;
9446 RSK_SEG_ID = 'ABCD 0101';
9447 rsk_seg_id_trans = translate(rsk_seg_id,'_',' ');
9448 put rsk_seg_id = rsk_seg_id_trans=;
9449 run;
RSK_SEG_ID=ABCD 0101 rsk_seg_id_trans=ABCD_0101
also:
9477
9478 data _null;
9479 RSK_SEG_ID = ' ABCD 0101 ';
9480 rsk_seg_id_trans = translate(rsk_seg_id,'_',' ');
9481 rsk_seg_id_trans2 = translate(trim(left(rsk_seg_id)),'_',' ');
9482 rsk_seg_id_trans3 = translate(' ABCD 0101 ','_',' ');
9483 rsk_seg_id_trans4 = translate(trim(left(' ABCD 0101 ')),'_',' ');
9484 put rsk_seg_id = rsk_seg_id_trans= rsk_seg_id_trans2= rsk_seg_id_trans3= rsk_seg_id_trans4=;
9485 run;
RSK_SEG_ID=ABCD 0101 rsk_seg_id_trans=__ABCD_0101__ rsk_seg_id_trans2=ABCD_0101 rsk_seg_id_trans3=__ABCD_0101__
rsk_seg_id_trans4=ABCD_0101
Message was edited by: James Willis. I added spaces before and after and I added the trim(left()) functions.
The code you've posted has two parentheses after TRIM in the TRANSLATE example, which will result in a syntax error. It's very important to always check the log!
Other than that, your TRANSLATE example should work perfectly; in this use case, TRANSLATE is more appropriate than TRANWRD.
Here's the example that worked for me:
data have;
SourceString = "ABCD 0101";
TargetString = TRANSLATE (trim("ABCD 0101"),"_"," ");
output;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.