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
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
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
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
I would let the FINDC function do the work.
data want;
set have;
z=findc(column,'-','adkt')>0;
run;
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.
@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.
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.
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.