I am trying to change the 4th character in a string. The 4th character consists of numbers and letters. The code that I used changes the entire string to all x's. Please see code. Any assistance will be greatly appreciated.
Data Want;
set DATASET1;
do _N_=2 TO length(code);
substr(code,_n_,4)=byte(mod(rank(char(code,_n_))+&offset,255));
end;
run;| This is the original data |
| DATASET 1 |
| CODE |
| 0S124 |
| 0R123 |
| 0T435 |
| 0F5F3 |
| 0R4RY |
| This is what I want |
| DATASET 2 |
| CODE |
| 0S1X4 |
| 0R1X3 |
| 0T4X5 |
| 0F5X3 |
| 0R4XY |
| Here are my results |
| CODE |
| XXXXX |
| XXXXX |
| XXXXX |
| XXXXX |
| XXXXX |
This works. Use the Left of = SUBSTR function. Not sure why you're looping anything or using _N_.
data want;
set sashelp.class;
substr(name, 4, 1) = 'X';
run;
This works. Use the Left of = SUBSTR function. Not sure why you're looping anything or using _N_.
data want;
set sashelp.class;
substr(name, 4, 1) = 'X';
run;
Thank you! I had to change the 4 to 6. Other than that it worked!
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.