How to set a flag if a string has nonEnglish character?
Is a non-english character a character not in a-z?
Yes. It has some chinese charcters
Do something like this
data have;
string="abc";output;
string="ab人物";output;
string="xyz";output;
run;
data want;
set have;
flag=ifn(notalpha(string),1,0);
run;
flag is set to 1 for all values
Ok. Does this work for you?
data have;
string="abc";output;
string="ab人物";output;
string="xyz";output;
run;
data want;
set have;
flag=ifn(lengthn(compress(string, "abcdefghijklmnopqrstuvwxyz", "i")),1,0);
run;
EDIT: I added an IFN Function to the code.
I cannot extend it to below, it fails:
flag=lengthn(catx("-",string1, string2),"abcdefghijklmnopqrstuvwxyz", "i");
@PeterClemmensen , The code of yours can be tweaked to
data have;
string="abc";output;
string="ab人物";output;
string="xyz";output;
run;
data want;
set have;
flag=lengthn(compress(string, " ", "ai"))>0;
/* flag=ifn(lengthn(compress(string, "abcdefghijklmnopqrstuvwxyz", "i")),1,0);*/
run;
This solution however, relies on your OPTIONS LOCALE= System Option
Let me know if it does not meet your needs.
I think regular expression would be the easiest way to do it. Here is how I would do this:
data want;
length mytext $100.;
input mytext $;
flag = ifn(prxmatch('/[^a-zA-Z0-9 ]/', mytext) > 0, 1, 0);
datalines;
Hello
Sébastien
;
run;
You need to be careful which string function you're using as soon as it comes to dealing with multi byte characters.
The PRX...() functions are unfortunately only good for single byte.
Try this if you have National Language Support
data want;
set have;
flag= string ne basechar(string);
run;
Try it on a real sample of your data, not datalines text.
This
if length(CHAR) ne klength(CHAR);
will detect any character that doesn't use single-byte encoding.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.