Hi everyone,
I have a table that might contain special characters in one of the columns called "DocumentNo".
Based on the characters below, i want to flag it as long as there it exists:
33 21 ! 34 22 " 35 23 # 36 24 $ 37 25 % 38 26 & 39 27 ' 40 28 ( 41 29 )
Here are the data examples:
ID DocumentNo 1 A1 2 AA 3 B1 4 BB 5 C^*(' 6 )(^%$ 7 "AA"xx'vvv'
As you can see, ID from 5 onwards contains special characters. I want to create a flag column called 'Flag' to mark as 'Y' for ID 5, 6, 7.
Is there a code/function for this?
You could use the findc function. e.g.:
data have;
input ID DocumentNo $20.;
cards;
1 A1
2 AA
3 B1
4 BB
5 C^*('
6 kk'
7 )(^%$
8 "AA"xx'vvv'
;
data want;
set have;
flag=findc(DocumentNo,'!"#$%&''()') gt 0;
run;
Art, CEO, AnalystFinder.com
something like this, this will flag anything numeric or character will flag them as Y
data have;
input ID DocumentNo $20.;
if prxmatch("m/[^A-Z0-9]+/oi",trim(DocumentNo)) > 0 then flag = 'Y';
else flag = "N";
cards;
1 A1
2 AA
3 B1
4 BB
5 C^*('
6 kk'
7 )(^%$
8 "AA"xx'vvv'
;
The notalpha() function also does what you want.
In case the list of special characters is not fully defined here a variant to @art297's code which should cover for this.
data have;
input ID DocumentNo $20.;
cards;
1 A1
2 AA
3 B1
4 BB
5 C^*('
6 kk'
7 )(^%$
8 "AA"xx'vvv'
;
run;
data want;
set have;
flag=findc(DocumentNo,' ','kn') gt 0;
run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.