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!
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.
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.
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
I think I figured it out. Thank you for all your help. I appreciate it.
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.
thank you for your reply.
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.
Ready to level-up your skills? Choose your own adventure.