BookmarkSubscribeRSS Feed
sas_user124
Calcite | Level 5

Hi, I have a treatment variable with categories surgery, medication, other. Each person's treatment in the column looks like 'surgery+other' 'other+medication' etc. and I want to create individual columns for surgery, medication, and other that will mark the patient as having each type of surgery. How do i code this?

1 REPLY 1
ballardw
Super User

One way is to search a text field for key words and if present assign a value to the other value. If you want to "count" there are choices of what values to assign.

If I understand your need and this were my project I might do something like:

data want;
   set have;
   surgery = (find(treatment,'surgery','i')>0);
   medication = (find(treatment,'medication','i')>0);
   other =  (find(treatment,'other','i')>0);
run;

This assumes that your variable name is Treatment, since you weren't explicit about that.

The code will assign a value of 1 to the variables Surgery, Medication or Other if the text in quotes appears in the value and 0 otherwise. The 'i' in the parameters to the find function tell SAS to ignore case, so if the word sometimes appears as "Other" "other" "OTHER" or other case combinations they are set. If the word appears twice (surgery+other+surgery for example surgery is only assigned a value of 1).

The 1/0 coding is very flexible as the SUM of the variable is the count  of times the word appears at least once per observation. The Mean of the variable would be the percent of ones (.75 = 75% for example).

 

Other statistics available in reports could tell if if every observation for a patient (or other grouping variable) has some characteristic. MAX=1 at least one observation had the value in the treatment Max=0 means none, Range=0 means all values the same (all observations has surgery or none id).

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 1 reply
  • 353 views
  • 0 likes
  • 2 in conversation