BookmarkSubscribeRSS Feed
christinagting0
Quartz | Level 8

I have a data set and I am trying to flag all variables that have the word "treatment" or "tx" with a 1 else 0. However, there are some observations which have the following description: "Supplies-treatment" or "supplies-tx". For these, I DO NOT want them to be flagged with a 1.

 

Here is my code:

 

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

 

this works perfectly! When I run a proc freq I see that there are 33314 obs flagged with a 1.

 

But then I want to unflag all the descriptions that have the word "supplies" in them. In order to accomplish this I thought I could use a prxmatch as above but on the new dataset I just created with all the words "treatment" flagged with 1, and when it came across any descriptions with "supplies" it would flag it as a 0. I used this code next after running the above code to accomplish the aforementioned:

 

data flagtxsupplies;
set flagtx;
if prxmatch("m/supplies/oi",description) > 0 then tx=0;
else tx=1;
run;

 

However, when I run a proc freq, I see that it has incorrectly flagged my variables somehow. Instead of having LESS than 33314 obs flagged with a 1 (which should be the case because I am eliminating "supplies-treatment") I actually have MORE obs that have been flagged with a 1. 64174 to be exact!

 

I did a proc print on 100 obs but everything looks good..what is going on?? What is my code doing exactly?

 

Thank you

2 REPLIES 2
Reeza
Super User

Rethink your else statement. 

PGStats
Opal | Level 21

Your second step overwrites tx for every obs. So the end result is just the last test, i.e. anything not supplies is flagged. 

 

In a single step:

 

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 783 views
  • 1 like
  • 3 in conversation