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

Hello,

 

I want to create a variable that identifies if a specific patient receives more than one dosage type of a given medication. So if the patient only receives one dosage type than the new variable would be 1 for all given rows for that patient. If a patient receives two different dosage types than the new variable would be 1 for the first dosage type and 2 for the 2nd dosage type. Below is an example of the data and the variable I want to create. (I'm using SAS 9.2).

 

Thank you in advance!

 

IDMedication_doseVariable_wanted
1500mg1
1500mg1
1500mg1
1100mg2
1100mg2
2250mg1
2250mg1
2250mg1
3500mg1
3500mg1
3250mg2
3100mg3
1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20
data have;
input ID Medication_dose $;
datalines;
1 500mg
1 500mg
1 500mg
1 100mg
1 100mg
2 250mg
2 250mg
2 250mg
3 500mg
3 500mg
3 250mg
3 100mg
;

proc sort data=have;
   by ID Medication_dose ;
run;

data want;
   set have;
   by ID Medication_dose;
   if first.ID then variable_wanted=0;
   if first.Medication_dose then variable_wanted+1;
run;

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

Sort by id and medication_dose.

In a data step, use

by id medication_dose;
retain variable_wanted;
if first.id then variable_wanted = 0;
if first.medication_dose then variable_wanted + 1;
PeterClemmensen
Tourmaline | Level 20
data have;
input ID Medication_dose $;
datalines;
1 500mg
1 500mg
1 500mg
1 100mg
1 100mg
2 250mg
2 250mg
2 250mg
3 500mg
3 500mg
3 250mg
3 100mg
;

proc sort data=have;
   by ID Medication_dose ;
run;

data want;
   set have;
   by ID Medication_dose;
   if first.ID then variable_wanted=0;
   if first.Medication_dose then variable_wanted+1;
run;
sk123
Calcite | Level 5
Thank you! This worked perfectly.
PeterClemmensen
Tourmaline | Level 20

Or if you want to avoid sorting. 

 

Also, welcome to the SAS Communities 🙂

 

data have;
input ID Medication_dose $;
datalines;
1 500mg
1 500mg
1 500mg
1 100mg
1 100mg
2 250mg
2 250mg
2 250mg
3 500mg
3 500mg
3 250mg
3 100mg
;

data want;
   if _N_ = 1 then do;
      declare hash h();
      h.defineKey('ID', 'Medication_dose');
      h.defineDone();
   end;

   do until (last.ID);
      set have;
      by ID notsorted;
      if h.check() ne 0 then do;
         Variable_wanted+1;
         h.add();
      end;
      output;
   end;
   Variable_wanted=0;
run;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 5 replies
  • 1477 views
  • 0 likes
  • 3 in conversation