BookmarkSubscribeRSS Feed
Quartz | Level 8

Hi Everyone, 


New to sas and have no idea what I'm doing!


I need to flag a number of obsevations in my data set.


For example, I want to flag all observations that have a description that includes "tx" or "treatment"- I want to find any iteration of these strings (i.e., whether it's CAPS or some letters are caps or others aren't). 


I also want to EXCLUDE the following strings= "supplies-treatment", "Supplies-treamtent" and "Acute/Subacute Treatment - Discharge".


This is the code I have used, but I'm not sure if I'm doing this right at all:


data flagtx;
set flagtxdc;
if prxmatch("m/tx|treatment|treat/oi",description) > 0 then tx=1;
else tx=0;


Is this right and how do I EXCLUDE certain strings.


(If you answer, I would really appreciate it if you could dumb it down! I'm very new to SAS and don't really understand a lot of the terminiology, lol)



Super User

What do you mean by exclude - remove those strings or remove observation?


Can you post sample data and expected output?


if your a SAS beginner and not familiar with perl I would recommend the find/finds/index/indexes functions along with upcase/lowcase. 

Upcase allows you capitalize all your text so you can simplify comparisons. 

Find/index search a string for a specified substring or word. 

Quartz | Level 8

Hi Reeza, 


Thanks for replying.


Here is some example data


ID   Description

1     chronic treatment

2     Treatment

3     acute/subacute tx

4     supplies-treatment

5     Supplies - Treament

6     apples


My expected output it


ID   Description                  flagtx

1     chronic treatment          1

2     Treatment                      1

3     acute/subacute tx          1

4     supplies-treatment         0

5     Supplies - Treament      0

6     apples                            0



As you can see from my expected output, ID 1-3 were flagged in the new variable flagtx because they contain the word treatment or Treatment or tx. ID 4 &5 I want to exclude meaning that they are NOT flagged or are coded as a 0. ID 6 is given a value of 0 as well because it doesn't contain any "treatment" or "tx".


Does that make sense?




Super User

This works for your sample but may not for your full dataset. You may need to expand your data is never fun. 

If you end up with a long list it may be worth setting up an array instead of searching for each term one at a time. But this should get you started.


data have;
input ID   Description $30.;
1     chronic treatment
2     Treatment
3     acute/subacute tx
4     supplies-treatment
5     Supplies - Treatment
6     apples

data want;
set have;
description = upcase(description);

if find(description, 'TX') > 0 
or find(description, 'TREATMENT')>0 
	then flagtx=1;
else flagtx=0;

if find(description, 'SUPPLIES')>0 
or find(description, 'DISCHARGE')>0 
	then flagtx=0;

Quartz | Level 8

Thanks Reeza, 


Why did you use the UPCASE function. Does using this mean that the program will only find the strings in uppercase format?


What if I wanted to find the strings in any variation? For example "TREATMENT" or "Treatment" or "treatment" or TrEATment"?


Thank you for helping me out:)

Super User

Upcase converts everything to capitals. This means you can to a single comparison rather than worry about testing for different cases. 



Quartz | Level 8


Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 6 replies
  • 2 in conversation