BookmarkSubscribeRSS Feed
YW_CA
Calcite | Level 5

hello, I want to replace a substr in a text to some else, somehow the following does not work. Any ideas? If I assign the string values to the temp_str and new_str, the F1 is updated as intended. Thank you,

DATA TEST;

F1 = "9999,'ABC', 110,2,    0.00,    0.00, 14, 17,1.02454,  27.9937,1";

CALL SCAN(F1,5,POS,LEN,',');

TEMP_STR = SUBSTR(F1,1,POS-2);

NEW_STR = CAT(SUBSTR(F1,1,POS-3),'1');

F1 = TRANWRD(F1,TEMP_STR,NEW_STR);

RUN;

3 REPLIES 3
ballardw
Super User

It is a good idea describe the intended output instead of just "doesn't work".

You're not getting the match you expect because both TEMP_STR and NEW_STR are 200 characters long and all of them are used in the comparison.

You may want:

TRANWRD(F1,strip(TEMP_STR),strip(NEW_STR))

YW_CA
Calcite | Level 5

the STRIP worked. so by default, the substr is 200 characters long?

ballardw
Super User

The result of CAT, and many other string functions, is 200 unless you define the variable first. But even with a defined length you would likely have the comparison for some values with the trailing blanks. So it is a good idea to STRIP values. Though you need other approaches if you want leading blanks compared...

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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