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!
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.