BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kfbaker0206
Fluorite | Level 6

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
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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;

View solution in original post

2 REPLIES 2
Reeza
Super User

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;
kfbaker0206
Fluorite | Level 6

Thank you! I had to change the 4 to 6. Other than that it worked! 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

Register Now

Creating Custom Steps in SAS Studio

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 1463 views
  • 0 likes
  • 2 in conversation