Help using Base SAS procedures

How to check if a character variable consists of all same letters

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

How to check if a character variable consists of all same letters

Hi, I have a character variable which is about the surname of people. Many respondents entered the names as AA, BBB or ZZZZ which mean that they are not real names. So these cases should be deleted.

Translating this question into this: how can I check if a character variable consists of all same letters? Note that the name has varying length.

Thanks a lot for help.


Accepted Solutions
Solution
‎01-09-2012 09:08 AM
Super Contributor
Posts: 1,636

Re: How to check if a character variable consists of all same letters

Posted in reply to JamesKauley

another way:

data have;

  input name $20.;

  cards;

AA

BBB

ZZZZ

AABA

;

data want;

  set have;

  flag=compress(name,substr(name,1,1));

  if flag ne ' ';

run;

proc print;

run;

Linlin

View solution in original post


All Replies
PROC Star
Posts: 7,487

How to check if a character variable consists of all same letters

Posted in reply to JamesKauley

Here is one possibility:

data have;

  input name $20.;

  cards;

AA

BBB

ZZZZ

AABA

;

data want;

  set have;

  array chars(20);

  do i=1 to length(name);

    chars(i)=rank(substr(name,i,1));

  end;

  if min(of chars(*)) ne max(of chars(*)) then keepit=1;

  if keepit;

run;

Solution
‎01-09-2012 09:08 AM
Super Contributor
Posts: 1,636

Re: How to check if a character variable consists of all same letters

Posted in reply to JamesKauley

another way:

data have;

  input name $20.;

  cards;

AA

BBB

ZZZZ

AABA

;

data want;

  set have;

  flag=compress(name,substr(name,1,1));

  if flag ne ' ';

run;

proc print;

run;

Linlin

Contributor
Posts: 35

How to check if a character variable consists of all same letters

Posted in reply to JamesKauley

This isn't an answer to your question (since the other two respondents gave solutions), but I just wanted to point out that if there is a possibility of pseudonyms like AAA, BBB, etc., I would assume there is also the possibility of pseudonyms like QWERTY, ASDFGH, 12345, etc. You are going to need a more elaborate filter than that (which can not possibly get rid of all pseudonyms without also potentially getting rid of some real names).

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 435 views
  • 6 likes
  • 4 in conversation