howard
howard

Reputation: 23

SAS - Removing zeros from a character

I have a character column with values that look like the following:

PQRS_XY12345678
PQRS_XY23456789
PQRS_XY34567890
PQRS_XY45678901
PQRS_XY005678
PQRS_XY000123456789
PQRS_XY00012340002

I would like to just retrieve number from this column without the leading zeros. I have used anydigit function to fetch the number, however the number of zeros vary for diff records, is there any quick way to ignore/remove any number of zeros before a valid number(non zero)

Upvotes: 0

Views: 742

Answers (1)

data _null_
data _null_

Reputation: 9109

Here are 2 ways. One produces a number the other string.

45         data _null_;
46            input string:$32.;
47            n = input(substrn(string,anydigit(string)),32.);
48            n2 = substrn(string,anydigit(string));
49            n3 = substrn(n2,verify(n2,'0'));
50            put (_all_)(=);
51            cards;

string=COUN_IN10346597 n=10346597 n2=10346597 n3=10346597
string=COUN_IN10420787 n=10420787 n2=10420787 n3=10420787
string=COUN_IN0009876543 n=9876543 n2=0009876543 n3=9876543
string=COUN_IN000710008976 n=710008976 n2=000710008976 n3=710008976
string=COUN_IN000710009 n=710009 n2=000710009 n3=710009

Upvotes: 1

Related Questions