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.
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.
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.