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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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

13 REPLIES 13
FriedEgg
SAS Employee

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;

ballardw
Super User

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;

Haikuo
Onyx | Level 15

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

Astounding
PROC Star

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?

michtka
Fluorite | Level 6

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?

Astounding
PROC Star

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'));


Linlin
Lapis Lazuli | Level 10

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

Ksharp
Super User

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

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

michtka
Fluorite | Level 6


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.

Alpay
Fluorite | Level 6

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;

Ksharp
Super User

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

michtka
Fluorite | Level 6

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

Ksharp
Super User

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

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 13 replies
  • 15382 views
  • 7 likes
  • 8 in conversation