BookmarkSubscribeRSS Feed
SAS1918
Calcite | Level 5

I have two variables patid ( patient ID) and dx1 (code). 

For all patient id (patid) I want only those dx1 where dx1 start with with '380' or '470' OR dx1 is equal to '98006' 

and replace any other dx1 with missing. 

 

Please find below the SAS code - 

data abc;
infile datalines ;
input patid dx1 $ ;
datalines;
123 38000
122 47001
123 38010
124 78011
125 47021
126 47031
127 38005
128 98006
;
run;

data abc1;
set abc;
if  upcase(substr(dx1,1,3)) not in ('380','470')  
or
 upcase(dx1) not in ('98006')  then dx1 = '';    
run;

The result is -

 

dx1 missing for all patid. 

Why is that happening?

 

 

4 REPLIES 4
Kurt_Bremser
Super User

The combined condition is true for all observations.

In all cases but the last, dx1 is not equal to '98006', so the second part of the condition is true, and in the last case, the first three digits are not in your list, so the first part of the condition is true. Did you intend to use "and" instead of "or"?

Jagadishkatam
Amethyst | Level 16

Please try the below code

 

data abc;
infile datalines ;
input patid dx1 $ ;
if not (prxmatch('m/^380/i',dx1) or prxmatch('m/^470/i',dx1) or prxmatch('m/^98006/i',dx1)) then dx1='';
datalines;
123 38000
122 47001
123 38010
124 78011
125 47021
126 47031
127 38005
128 98006
;
run;
Thanks,
Jag
novinosrin
Tourmaline | Level 20

Hello @SAS1918   Are you sure that you are after "NOT IN"

 

You have stated  " I want only those dx1 where dx1 start with with '380' or '470' OR dx1 is equal to '98006' "

 

Anyways

 


data abc;
infile datalines ;
input patid dx1 $ ;
datalines;
123 38000
122 47001
123 38010
124 78011
125 47021
126 47031
127 38005
128 98006
;
run;
/*if you want*/
data want;
set abc;
/*if you want*/
where dx1 in :('380','470') or dx1='98006' ;
run;

/*if you DO NOT want*/
data want;
set abc;
/*if you DO NOT want*/
where dx1 not in :('380','470') and dx1^='98006' ;
run;

 

Ksharp
Super User

You are almost there.

 

data abc;
infile datalines ;
input patid dx1 $ ;
datalines;
123 38000
122 47001
123 38010
124 78011
125 47021
126 47031
127 38005
128 98006
;
run;

data abc1;
set abc;
if  dx1 not in: ('380','470','98006')  then dx1 = ' ';    
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1108 views
  • 0 likes
  • 5 in conversation