Help using Base SAS procedures

verify function to extract number for a string

Accepted Solution Solved
Reply
Super Contributor
Posts: 301
Accepted Solution

verify function to extract number for a string

Hi guys, can anyone help me to use the function verify to extract the number for the character variable code?

data new;

input code $;

datalines;

agsadg1

iuyasd2

flgdfe3

gttret4

hgtmgt5

gftfrg6

frgdf11

ewtdf54

ltrgr230

tyuf546

ffr1345

fgh5890

lko9999

;

run;

the idea is, generate a variable coden with  the values:

1

2

3

4

5

6

11

54

230

546

1345

5890

9999

and I would like to use the function verify to do this.

Thanks in advance.

                                   

V


Accepted Solutions
Solution
‎05-21-2012 06:26 PM
Respected Advisor
Posts: 3,124

Re: verify function to extract number for a string

Compress() can do the trick as well:

data new;

input code $;

n_code=compress(code,,'kd');

datalines;

agsadg1

iuyasd2

flgdfe3

gttret4

hgtmgt5

gftfrg6

frgdf11

ewtdf54

ltrgr230

tyuf546

ffr1345

fgh5890

lko9999

;

proc print;run;

Haikuo

View solution in original post


All Replies
Trusted Advisor
Posts: 1,300

Re: verify function to extract number for a string

data new;

input code $;

/* substr + verify */

want=substr(code,verify(lowcase(code),'abcdefghijklmnopqrstuvwxyz'));

/* compress */

want2=compress(code,,'kd');

/* prx (there are plenty of other ways too */

want3=prxchange('s/\D//o',-1,code); *remove any non-digit;

want4=prxchange('s/[A-Z]+//io',1,code); *remove a single string of alphabetic characters;

/* scan - this would be my preferred method */

want5=scan(code,-1,,'a');

datalines;

agsadg1

iuyasd2

flgdfe3

gttret4

hgtmgt5

gftfrg6

frgdf11

ewtdf54

ltrgr230

tyuf546

ffr1345

fgh5890

lko9999

;

run;

Super User
Posts: 10,497

Re: verify function to extract number for a string

if you want a numeric coden:

 

check = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

coden = input(substr (code,verify((upcase(code)),check)),best12.);

drop check;

if a string with numbers only:

 

check = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

coden = substr (code,verify((upcase(code)),check));

drop check;

Solution
‎05-21-2012 06:26 PM
Respected Advisor
Posts: 3,124

Re: verify function to extract number for a string

Compress() can do the trick as well:

data new;

input code $;

n_code=compress(code,,'kd');

datalines;

agsadg1

iuyasd2

flgdfe3

gttret4

hgtmgt5

gftfrg6

frgdf11

ewtdf54

ltrgr230

tyuf546

ffr1345

fgh5890

lko9999

;

proc print;run;

Haikuo

Super User
Posts: 5,081

Re: verify function to extract number for a string

Is it possible you will have an embedded digit that you wish to ignore?  For example, could this appear in your data:

hgt3mgt5

If so, would you like "5" as the result?

Super Contributor
Posts: 301

Re: verify function to extract number for a string

Thanks guys, It works.

Astoundind raised a good question....how i can ignore an embedded digit?...it can be a rare case, but how can I get

then 5 instead of 35?

any suggestion?

Super User
Posts: 5,081

Re: verify function to extract number for a string

There might be an easier way, especially when specifying the third parameter, but here's what comes to mind (untested):

length newcode $ 8;

newcode = scan(code, -1, compress(code,,'d'));


Super Contributor
Posts: 1,636

Re: verify function to extract number for a string

tested codeSmiley Happy:

data new;

input code $;

datalines;

agsadg1

2uyasd2

flgdfe3

gttret4

hgtmgt5

gftfrg6

frgdf11

ewtdf54

ltrgr230

tyuf546

ffr1345

fgh5890

lko9999

hgt3mgt5

;

run;

data wnat;

set new;

n_code=substr(code,anyalpha(code,-length(code))+1);

proc print;run;

                                   Obs      code      n_code

                                        1    agsadg1      1
                                        2    2uyasd2      2
                                        3    flgdfe3      3
                                        4    gttret4      4
                                        5    hgtmgt5      5
                                        6    gftfrg6      6
                                        7    frgdf11      11
                                        8    ewtdf54      54
                                        9    ltrgr230     230
                                       10    tyuf546      546
                                       11    ffr1345      1345
                                       12    fgh5890      5890
                                       13    lko9999      9999
                                       14    hgt3mgt5     5

Super User
Posts: 9,676

Re: verify function to extract number for a string

There are lots of modifiers in SCAN(), You don't need a extra compress any more.

newcode = scan(code, -1,,'kd');

Super Contributor
Posts: 301

Re: verify function to extract number for a string


Sorry Ksharp, I just type your code but it doesnt work.

At the moment:

newcode = scan(code, -1, compress(code,,'d'));

n_code=substr(code,anyalpha(code,-length(code))+1);

works fine.

Have you tested it in my new dataset?

cheers,

V.

Frequent Contributor
Posts: 95

Re: verify function to extract number for a string

The way I understood the question is that you are looking for digits only placed at the end of a string.

If there is a digit between any two characters those digits are excluded.

Regular expressions might do the trick here as follows:

If there is a pattern of 1 or more characters, "[a-z]+", followed by 0 or more digits, "\d*", followed by 1 or more characters, "[a-z]+", replace them with null space, "//".

Zafer

data new;

input code $;

n_code=prxchange('s/[a-z]+\d*[a-z]+//',-1,code);

datalines;

agsadg1

iuyasd2

flgdfe3

gttret4

hgtmgt5

gftfrg6

frgdf11

ewtdf54

ltrgr230

tyuf546

ffr1345

fgh5890

lko9999

;

proc print;run;

Super User
Posts: 9,676

Re: verify function to extract number for a string

Yes. My code is also right.

data new;
input code $;
newcode = scan(code, -1,,'kd');
datalines;
agsadg1
iuyasd2
flgdfe3
gttret4
hgtmgt5
gftfrg6
frgdf11
ewtdf54
ltrgr230
tyuf546
ffr1345
fgh5890
lko9999
;
run;

Ksharp

Super Contributor
Posts: 301

Re: verify function to extract number for a string

newcode = scan(code, -1,,'kd');

                             -

                             159

ERROR 159-185: Null parameters for SCAN are invalid.

Hi Ksharp, I got that error when I run your code using SAS 9.1.3 Smiley Sad

Super User
Posts: 9,676

Re: verify function to extract number for a string

OU, I know the reason why you can't run it.

You are using SAS9.1.

You can't use my code ,then recommend to HaiKuo's code which is good.

Ksharp

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 13 replies
  • 4816 views
  • 6 likes
  • 8 in conversation