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;
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))
the STRIP worked. so by default, the substr is 200 characters long?
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...
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.