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
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
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;
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;
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
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?
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?
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'));
tested code:
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
There are lots of modifiers in SCAN(), You don't need a extra compress any more.
newcode = scan(code, -1,,'kd');
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.
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;
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
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
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
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!
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.
Ready to level-up your skills? Choose your own adventure.