Help using Base SAS procedures

help with coding

Reply
Occasional Contributor
Posts: 9

help with coding

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!

PROC Star
Posts: 7,358

help with coding

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;

Occasional Contributor
Posts: 9

help with coding

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

PROC Star
Posts: 7,358

help with coding

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;

Super User
Posts: 9,671

help with coding

Hi .Art .

You can use colon operator like:

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

Ksharp

Occasional Contributor
Posts: 9

help with coding

Fantastic!  Thank you both.

Ask a Question
Discussion stats
  • 5 replies
  • 135 views
  • 6 likes
  • 3 in conversation