BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
learn_SAS_23
Pyrite | Level 9
data have;
    input column $20.;
    datalines;
abc-def1
(123-456
xyz789
!@#$%^&*
;
run;
data check_special_chars;
    set have;

    /* Use PRXMATCH to search for characters that are not alphanumeric or hyphen */
    if prxmatch('/[^[:alnum:]-]/', column) > 0 then
        special_chars_found = 1;
    else
        special_chars_found = 0;
run;

am expecting : 

column   special_chars_found

abc-def1    0
(123-456   1
xyz789      0
!@#$%^&* 1

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

keep it simple:

data have;
    input column $20.;
    datalines;
abc-def1
(123-456
xyz789
!@#$%^&*
;
run;
data want;
set have;
check = 1 & lengthn(compress(column,"-","AD"));
run;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

12 REPLIES 12
learn_SAS_23
Pyrite | Level 9
The above prxmatch is not working as expected
yabwon
Onyx | Level 15

keep it simple:

data have;
    input column $20.;
    datalines;
abc-def1
(123-456
xyz789
!@#$%^&*
;
run;
data want;
set have;
check = 1 & lengthn(compress(column,"-","AD"));
run;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



learn_SAS_23
Pyrite | Level 9
Thanks for your tip , what does this AD mean here ? lengthn(compress(column,"-","AD"));
yabwon
Onyx | Level 15

compress(column,"-","AD") means: delete hyphen, Alphabet letters, and Digits 😉

 

[EDIT:] If there is a space in your text, e.g., "AB CD" and you don't want it too this code won't be enough. But @PaigeMiller 's code with FINDC() will work.

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



PaigeMiller
Diamond | Level 26

I would let the FINDC function do the work.

 

data want;
    set have;
    z=findc(column,'-','adkt')>0;
run;
--
Paige Miller
learn_SAS_23
Pyrite | Level 9
Perfect , thanks so much
Both solutions working as perfect
can you share what does keyword 'adkt' mean ? in below function
findc(column,'-','adkt')
PaigeMiller
Diamond | Level 26

From the FINDC documentation, there are many "modifiers" that alter the search based upon many different criteria.

 

Letter a "adds alphabetic characters to the list of characters" that are in the search.

 

You can look up the other modifiers at that link.

--
Paige Miller
ballardw
Super User

@learn_SAS_23 wrote:
Perfect , thanks so much
Both solutions working as perfect
can you share what does keyword 'adkt' mean ? in below function
findc(column,'-','adkt')

Not a keyword, that is a list of modifiers to the function. 'a' or 'A' adds all alphabetic characters to the list to search for, 'd' or 'D' adds all digits to the list. So Findc(column,'-','ad') searches for the dash and all letters or digits. The 'k' says 'find characters not in the  list. Findc(column,'-','adk') finds all characters not in the list dash, letter or digit. The 't' removes any trailing spaces from the string and the character list. Many of the search functions in SAS will include trailing spaces if the length of a given variable is longer than is "used".  The reason T is added to the modifiers is demonstrated with this:

data example;
   length str1  $ 6;
   str1='a1-';
   result1 =findc(str1,'-','adk');
   result2 =findc(str1,'-','adkt');
run;

Result 1 is 4 because the full length of the variable str1 has been "padded" to the length of 6 characters with trailing spaces and is found by the function at position 4 to have something other than dash, letter or digit.

 

learn_SAS_23
Pyrite | Level 9
Thanks for clear details !!, it helps
Ksharp
Super User
You need to strip the blanks at end of string.
if prxmatch('/[^[:alnum:]-]/', column) > 0 then
---->
if prxmatch('/[^[:alnum:]-]/', strip(column)) > 0 then
learn_SAS_23
Pyrite | Level 9
Thanks ,

data check_special_chars;
set have;

/* Use PRXMATCH to search for characters that are not alphanumeric or hyphen */
if prxmatch('/[^[:alnum:]-]/', strip(column)) > 0 then
special_chars_found = 1;
else
special_chars_found = 0;
run;

Now this method also works

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 12 replies
  • 2653 views
  • 7 likes
  • 5 in conversation