Help using Base SAS procedures

Char Variable All possible Character

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 78
Accepted Solution

Char Variable All possible Character


I have a 8 byte long Character variable example NNNNNNNN  Now This word will replace with "D" at any of the 8 character so for example It can be DNNNNNNN or DDNNNNNN  now my requirement is only select those observation where minimum within this string there are TWO DD so out of three above only with "DDNNNNNN" contain observation should be selected...

I need to consider possibility that D can be at any location so it can be combination like NNDNNNDN  or any combination like that...Can somebody share thoughts how do we select these observations?


Accepted Solutions
Solution
‎07-27-2015 04:05 AM
Contributor
Posts: 65

Re: Char Variable All possible Character

data want;

set have;

if count(var,'D','i') gt 1;    ---  i ignores case

run;

Results a dataset which contains minimum of two D's (either D or d) in a variable Var.

data want;

set have;

if count(var,'D') gt 1;

run;

Results a dataset which contains minimum of two D's (only D) in a variable Var.

View solution in original post


All Replies
Respected Advisor
Posts: 3,900

Re: Char Variable All possible Character

You could use a compress() function and remove anything other than 'D'. Then you check if the remaining string (only Ds) has at least a length of 2.

data have;

  input row string $ select_expected;

  datalines;

1 NNDNNNDN 1

2 DNNNNNNN 0

3 NNNDNNNN 0

4 NNDNDNDN 1

;

run;

data want;

  set have;

  if lengthn(compress(string,'D','k'))>=2;

run;

Frequent Contributor
Posts: 78

Re: Char Variable All possible Character

Thanks Patrick it worked...

Solution
‎07-27-2015 04:05 AM
Contributor
Posts: 65

Re: Char Variable All possible Character

data want;

set have;

if count(var,'D','i') gt 1;    ---  i ignores case

run;

Results a dataset which contains minimum of two D's (either D or d) in a variable Var.

data want;

set have;

if count(var,'D') gt 1;

run;

Results a dataset which contains minimum of two D's (only D) in a variable Var.

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 258 views
  • 0 likes
  • 3 in conversation