Hi,
I have a data with the following ID's
SGJ604-01-A
AFD345-03-A
I want to change the last alphabet "A" to the number "0"
Can I do this in SAS. please let me know thanks in advance.
Try this:
if char(id,length(id))='A' then substr(id,length(id))='0';
Example:
458 data want; 459 set have; 460 put id= '-> ' @; 461 if char(id,length(id))='A' then substr(id,length(id))='0'; 462 put id; 463 run; id=SGJ604-01-A -> SGJ604-01-0 id=SGJ604-02-B -> SGJ604-02-B id=AFD345-03-A -> AFD345-03-0
Try this:
if char(id,length(id))='A' then substr(id,length(id))='0';
Example:
458 data want; 459 set have; 460 put id= '-> ' @; 461 if char(id,length(id))='A' then substr(id,length(id))='0'; 462 put id; 463 run; id=SGJ604-01-A -> SGJ604-01-0 id=SGJ604-02-B -> SGJ604-02-B id=AFD345-03-A -> AFD345-03-0
Here is a really interesting solution that uses the SUBSTR function on the LEFT side of the = sign. I assume you mean the digit 0 (character) for the last position. Here it is:
data Test;
length String $ 11;
input String;
substr(String,11,1) = '0';
datalines;
SGJ604-01-A
AFD345-03-A
;
title "Listing of Test";
proc print data=Test;
run;
Hope this helps,
Ron
@Ron_Cody wrote:
Here is a really interesting solution that uses the SUBSTR function on the LEFT side of the = sign. I assume you mean the digit 0 (character) for the last position. Here it is:
data Test;
length String $ 11;
input String;
substr(String,11,1) = '0';
datalines;
SGJ604-01-A
AFD345-03-A
;
title "Listing of Test";
proc print data=Test;
run;Hope this helps,
Ron
That unconditionally changes the character to 0. OP only wants the last character to change when it is 'A'
Hi. I didn't notice the specification that the last character in the string had to be 'A', so I modified my solution:
data Test;
length String $ 11;
input String;
if findc(String,'A',lengthn(String)-1) then substr(String,11,1) = '0';
datalines;
SGJ604-01-A
XXX999-090B
AFD345-03-A
;
title "Listing of Test";
proc print data=Test;
run;
The last argument in the FINDC function is a starting position. The LENGTHN function is the same as the older LENGTH function--it returns the length of a string, not counting trailing blanks. The 'N' version returns a 0 when the first argument is a missing value; the non 'N' (older function) returns a 1 in this situation. I recommend always using LENGTHN. Hope this helps and clarifies.
data Test;
length String $ 11;
input String;
datalines;
SGJ604-01-A
AFD345-03-A
;
data want;
set test;
want=prxchange('s/a$/0/i',1,strip(string));
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: