BookmarkSubscribeRSS Feed
imdickson
Quartz | Level 8

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?

4 REPLIES 4
art297
Opal | Level 21

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

 

kiranv_
Rhodochrosite | Level 12

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'
;
ChrisNZ
Tourmaline | Level 20

The   notalpha()   function also does what you want.

Patrick
Opal | Level 21

@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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 12683 views
  • 0 likes
  • 5 in conversation