BookmarkSubscribeRSS Feed
wpgnbo
Calcite | Level 5

I'd like to do the following, but I don't know how to code it:

I have 5 character variables (med_1, med_2 ... med_5) and I want to create a new variable, "antibiotic", which will = 1 if ANY of med_1-med_5 contain the name of an antibiotic.

I'd like to create a list of possible antibiotic names: ampicillin, amoxicillin, gentamycin, etc.  and then tell SAS to check the contents of med_1-med_5 and if ANY are equal to a drug name in my list, then make antibiotic=1, otherwise, antibiotic=0.

Thanks in advance for your help!

5 REPLIES 5
art297
Opal | Level 21

There are a number of ways to do what you want.  One way would be:

data have;

  informat med1-med5 $20.;

  input med1-med5;

  cards;

this that something another placebo

this that amoxicillin another placebo

this that something another placebo

this that something another gentamycin

ampicillin amoxicillin gentamycin somethingelse that

;

data want (drop=i);

  set have;

  array meds(*) $20. med1-med5;

  antibiotic=0;

  do i=1 to dim(meds);

    if meds(i) in ("ampicillin", "amoxicillin", "gentamycin")

     then antibiotic=1;

  end;

run;

wpgnbo
Calcite | Level 5

Thank you so much!  Is there a way to check for anything beginning with "amox"?  (so it would capture "amoxil", "amoxicillin", etc.?)

art297
Opal | Level 21

There is probably a more efficient way of coding it, but the following should work:

data have;

  informat med1-med5 $20.;

  input med1-med5;

  cards;

this that something another placebo

this that amoxicillinplus another placebo

this that something another placebo

this that something another gentamycin

ampicillin amoxicillinextra gentamycinplus1 somethingelse that

;

data want;

  set have;

  array meds(*) $20. med1-med5;

  array drugs(3) $20. ("ampici", "amoxici", "gentamyci");

  antibiotic=0;

  do i=1 to dim(meds);

    do j=1 to dim(drugs);

     if strip(drugs(j)) =: meds(i) then antibiotic=1;

    end;

  end;

run;

Ksharp
Super User

Hi .Art .

You can use colon operator like:

   if meds(i)  in:   ("amp", "amox", "gent")

Ksharp

wpgnbo
Calcite | Level 5

Fantastic!  Thank you both.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1720 views
  • 6 likes
  • 3 in conversation