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

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

View solution in original post

5 REPLIES 5
Tom
Super User Tom
Super User

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
Ron_Cody
Obsidian | Level 7

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

ballardw
Super User

@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'

Ron_Cody
Obsidian | Level 7

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.

 

Ksharp
Super User
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;

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 709 views
  • 1 like
  • 5 in conversation