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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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