I need help to exclude records with a variable that has values other than numbers and characters. Below is an example.
I have a variable 'test' which has values like below.
Test |
00*915174 |
00*915174 |
(00)-1234 |
00(9151.74) |
00_915_174 |
00-9151-74 |
001234 |
In the above column, I would like to exclude all the values with special character such as (, ), -, ., * and space. I want to keep values such as '001234' or 'ABC123'.
Thank you
data want;
set have;
retain re_pattern;
if _N_ = 1 then re_pattern = prxparse('/^[A-Za-z0-9]+$/');
if prxmatch(re_pattern, strip(test)) then output;
run;
or
data want;
set have;
if not prxmatch('/^[a-zA-Z0-9]+$/', strip(test)) then delete;
run;
It sounds to me like you could re-phrase the requirements to keep strings which contain digits or numbers, but not anything else.
And so this is a job for the FINDC function.
if findc(test,,'kad') then delete;
I feel so dumb asking this but still asking...what is 'kad' in your statement.
Thanks
@SASMom2 - Click on the FINDC documentation link provided by @PaigeMiller and you will find what 'kad' means.
data have;
input test $20.;
cards;
00*915174
00*915174
(00)-1234
00(9151.74)
00_915_174
00-9151-74
001234
ABC123
;
data want;
set have;
if notalnum(strip(test)) then delete;
run;
data want;
set have;
retain re_pattern;
if _N_ = 1 then re_pattern = prxparse('/^[A-Za-z0-9]+$/');
if prxmatch(re_pattern, strip(test)) then output;
run;
or
data want;
set have;
if not prxmatch('/^[a-zA-Z0-9]+$/', strip(test)) then delete;
run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.