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

Good Afternoon, I have a 7 character variable in a dataset. I want to replace 'X' in the 2nd position from left with a '0'  in this variable. And same way in a different dataset, replace '0' in the 2nd position from left with 'X'.

 

1st dataset:

I have: 0X12345

I want: 0012345

 

2nd dataset:

I have: 0012345

I want: 0X12345

 

Thank you in advance!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

For data set 1, this is what you would do

 

 

if substr(variablename,2,1)='X' then substr(variablename,2,1)='0';

 

 

I leave it to you as a homework assignment to write the code needed (as a modification of the above code) for data set 2.

--
Paige Miller

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

For data set 1, this is what you would do

 

 

if substr(variablename,2,1)='X' then substr(variablename,2,1)='0';

 

 

I leave it to you as a homework assignment to write the code needed (as a modification of the above code) for data set 2.

--
Paige Miller
SASMom2
Fluorite | Level 6

Thank you for your reply. Below is what I did.

 

data test2;
set dropnulls;
if substr(pol_nbr,2,1)='X' then rpol_nbr = substr(pol_nbr,2,1)='0';
where pol_nm is missing;
run;

 

I am getting 0 in the field where 2nd position letter is X.

for an example, for a value of 0X04236 I am getting 0 in the field instead of 0004236.

 

Thanks

 

 

SASMom2
Fluorite | Level 6

I think I figured it out. Thank you for all your help. I appreciate it.

quickbluefish
Barite | Level 11

For this particular situation, you can also use the following weird SAS syntax:

 

substr(V,2,1)='X';

...where V is whatever your variable name is.  

SASMom2
Fluorite | Level 6

thank you for your reply.

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

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
  • 5 replies
  • 1233 views
  • 0 likes
  • 3 in conversation