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;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.