BookmarkSubscribeRSS Feed
robertrao
Quartz | Level 8

 

Hello,

 

How can I also Include the "N" logic belowif the condition is not satisfied??

Thanks

 

data want;

set have;

array abiot(10) antibiotic1-antibiotic10;

do i= 1 to 10;

 

if index(upcase(abiot{i}),'GENTAMICIN') > 0 then GENTAMICIN="Y" ;

drop i;

end;

run;

8 REPLIES 8
PGStats
Opal | Level 21

Add

GENTAMICIN = "N";

before the do loop. It will keep that value when "GENTAMICIN" is not present in the array.

PG
Jagadishkatam
Amethyst | Level 16
Alternatively, an else GENTAMICIN="N" ; within the do loop after the if condition.
Thanks,
Jag
PGStats
Opal | Level 21

That's not the same. An else condition will reset GENTAMICIN to "N" even if it has been met earlier. I assumed OP wants GENTAMICIN = "N" only if it is absent in the array.

PG
robertrao
Quartz | Level 8

Hello PG stats,

 

I tried this and its resetting all the values to "N"

 

data want;

set have;

array abiot(10) antibiotic1-antibiotic10;

do i= 1 to 10;

 

if index(upcase(abiot{i}),'GENTAMICIN') > 0 then GENTAMICIN="Y" ;else GENTAMICIN="N";

drop i;

end;

run;

Reeza
Super User

@robertrao wrote:

Hello PG stats,

 

I tried this and its resetting all the values to "N"

 

data want;

set have;

array abiot(10) antibiotic1-antibiotic10;

do i= 1 to 10;

 

if index(upcase(abiot{i}),'GENTAMICIN') > 0 then GENTAMICIN="Y" ;else GENTAMICIN="N";

drop i;

end;

run;


That wasn't @PGStats solution. Reread his answer. 

Reeza
Super User

@Jagadishkatam wrote:
Alternatively, an else GENTAMICIN="N" ; within the do loop after the if condition.

No, that will reset it if it's been set to Y, which would be incorrect. 

 

Since your searching for a text string you could concatenate them all and search once though. 

 

If index(CATX (',' , of antibiotic1-antibiotic10), 'GENTAMICIN')> 0 then GENTAMICIN='Y';

else ....='N';

RW9
Diamond | Level 26 RW9
Diamond | Level 26

And just to add to that, the two functions IFC(), IFN() can be used for binary choices like this to remove if/else clause:

gentamycin=ifc(index(CATX (',' , of antibiotic1-antibiotic10), 'GENTAMICIN')> 0,'Y','N');

ballardw
Super User

I recomment @PGStats approach and to change this line:

if index(upcase(abiot{i}),'GENTAMICIN') > 0 then GENTAMICIN="Y" ;

 

to

 

if index(upcase(abiot{i}),'GENTAMICIN') > 0 then do;

   GENTAMICIN="Y"

   leave;

end;

 

Which will stop going through the loop the first time the condition is met. Since you are not counting or accumulating how many "Y" are involved then stop when you find the first one.

 

The LEAVE option as above would also resolve the problem of assigning the N if you used the Else approach.

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!

How to Concatenate Values

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.

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
  • 8 replies
  • 1244 views
  • 4 likes
  • 6 in conversation