BookmarkSubscribeRSS Feed
eagles_dare13
Obsidian | Level 7

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?

2 REPLIES 2
jwillis
Quartz | Level 8

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.

TomKari
Onyx | Level 15

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 732 views
  • 0 likes
  • 3 in conversation