0
votes

I have a character variable called "animid" with values like these:

215298

275899

287796

214896

98154

97856

78-21

213755

21-45

31-457

I want to remove the first digit ("2") only in those numbers that have a length of 6 digits (e.g. 213755, 214896, etc.). I cannot delete the first digit of numbers with a length of 5 or less (e.g. 21-45, 98154).

I used the following code trying to subtract the last 5 digits

data new;
set old;
new_animid =substr (animid,max(1,length(animid)-4),5);
run;

This code effectively keep the last 5 digits for each value. However, it also converts numbers like 31-457 to 1-457 (which is a result that I don't want. I only want to delete the number first digit ONLY if the value has 6 digits AND it starts with "2").

I basically ask if there is a way to state conditions to the "substr" statement (or other method in SAS). Something that will allow me to delete the number "2" but ONLY in those numbers that effectively start with the digit "2" AND that have 6 digits.

2

2 Answers

0
votes

To remove the first digit just use SUBSTR(,2).

 new_animid = animid ;
 if animid =: '2' and length(animid)=6 then new_animid = substr(animid,2);
0
votes

Use regular expression:

_animid=prxchange('s/^2(\d{5})/$1/',-1,animid);