How do i search for special characters and flag it?

Reply
Frequent Contributor
Posts: 94

How do i search for special characters and flag it?

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?

PROC Star
Posts: 8,163

Re: How do i search for special characters and flag it?

Posted in reply to imdickson

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

 

PROC Star
Posts: 508

Re: How do i search for special characters and flag it?

Posted in reply to imdickson

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'
;
PROC Star
Posts: 2,321

Re: How do i search for special characters and flag it?

Posted in reply to imdickson

The   notalpha()   function also does what you want.

Respected Advisor
Posts: 4,736

Re: How do i search for special characters and flag it?

Posted in reply to imdickson

@imdickson

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;
Ask a Question
Discussion stats
  • 4 replies
  • 136 views
  • 0 likes
  • 5 in conversation