BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
rkumar23
Calcite | Level 5


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?

1 ACCEPTED SOLUTION

Accepted Solutions
MadhuKorni
Quartz | Level 8

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

3 REPLIES 3
Patrick
Opal | Level 21

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;

rkumar23
Calcite | Level 5

Thanks Patrick it worked...

MadhuKorni
Quartz | Level 8

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.

sas-innovate-2024.png

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.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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