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

I need help with coding ICD9 codes. These codes have four or five digits. For example, it can be E810 or E8100. I want to filter the range of these codes which can be E8100 to E8139 or E810 to E813. By using following code , I am missing out on five digit records starting with E813, E819 , E825. I am also missing records with four digits such as E810, E812 ,E813 or 819. In short, I would like to filter observations starting with first four digits as well as  range starting those digits. Could anyone help with this? Thank you

Data WW.WW2;
set  WW.WW1;
If E_CODE_1 ='.........' Then ECODE_MECH ='........' ;
    IF 'E810%'<= E_CODE_1 <='E813%'  OR   'E815%'<= E_CODE_1<='E819%'  OR  'E822%'<= E_CODE_1<='E825%' OR E_CODE_1 IN ('E9885','E9290')
              THEN ECODE_MECH = '1';
    IF 'E814%'<= E_CODE_1 <= 'E814%'
              THEN ECODE_MECH= '2';
    RUN;
               

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

There are no wildcard filters like you have with %. That can work in SQL and/or using a WHERE clause. 

 

You can use =: to check if the strings are equivalent and that may be what you want but be careful and test it thoroughly. 

 

The most efficient way I've found is to create a master list of the codes I do want - can usually filter from online lists and then use that directly with a PROC FORMAT to select the records of interest. 

 

if E_CODE_1 =: 'E810' or E_CODE_1 =: 'E811' ... etc. 

Or you could convert a portion to a numeric and use that for your conditions. 

 

if substr(e_code_1, 1,1) = 'E' and 810 <= input(substr(e_code_1, 2, 3), 8.)  <=813 

Be very careful with text comparisons using Greater Than or Less Than operators. The algorithm is text so it goes:

0

1

10

11

12

13

...

2

20

21

...

 

 

20

 


@GARYV wrote:

I need help with coding ICD9 codes. These codes have four or five digits. For example, it can be E810 or E8100. I want to filter the range of these codes which can be E8100 to E8139 or E810 to E813. By using following code , I am missing out on five digit records starting with E813, E819 , E825. I am also missing records with four digits such as E810, E812 ,E813 or 819. In short, I would like to filter observations starting with first four digits as well as  range starting those digits. Could anyone help with this? Thank you

Data WW.WW2;
set  WW.WW1;
If E_CODE_1 ='.........' Then ECODE_MECH ='........' ;
    IF 'E810%'<= E_CODE_1 <='E813%'  OR   'E815%'<= E_CODE_1<='E819%'  OR  'E822%'<= E_CODE_1<='E825%' OR E_CODE_1 IN ('E9885','E9290')
              THEN ECODE_MECH = '1';
    IF 'E814%'<= E_CODE_1 <= 'E814%'
              THEN ECODE_MECH= '2';
    RUN;
               

 


 

View solution in original post

2 REPLIES 2
Reeza
Super User

There are no wildcard filters like you have with %. That can work in SQL and/or using a WHERE clause. 

 

You can use =: to check if the strings are equivalent and that may be what you want but be careful and test it thoroughly. 

 

The most efficient way I've found is to create a master list of the codes I do want - can usually filter from online lists and then use that directly with a PROC FORMAT to select the records of interest. 

 

if E_CODE_1 =: 'E810' or E_CODE_1 =: 'E811' ... etc. 

Or you could convert a portion to a numeric and use that for your conditions. 

 

if substr(e_code_1, 1,1) = 'E' and 810 <= input(substr(e_code_1, 2, 3), 8.)  <=813 

Be very careful with text comparisons using Greater Than or Less Than operators. The algorithm is text so it goes:

0

1

10

11

12

13

...

2

20

21

...

 

 

20

 


@GARYV wrote:

I need help with coding ICD9 codes. These codes have four or five digits. For example, it can be E810 or E8100. I want to filter the range of these codes which can be E8100 to E8139 or E810 to E813. By using following code , I am missing out on five digit records starting with E813, E819 , E825. I am also missing records with four digits such as E810, E812 ,E813 or 819. In short, I would like to filter observations starting with first four digits as well as  range starting those digits. Could anyone help with this? Thank you

Data WW.WW2;
set  WW.WW1;
If E_CODE_1 ='.........' Then ECODE_MECH ='........' ;
    IF 'E810%'<= E_CODE_1 <='E813%'  OR   'E815%'<= E_CODE_1<='E819%'  OR  'E822%'<= E_CODE_1<='E825%' OR E_CODE_1 IN ('E9885','E9290')
              THEN ECODE_MECH = '1';
    IF 'E814%'<= E_CODE_1 <= 'E814%'
              THEN ECODE_MECH= '2';
    RUN;
               

 


 

pau13rown
Lapis Lazuli | Level 10

use "=:" for example "if &icdvar{i} =:'030' then neuro=1" [actually @Reezaalready noted this] - i take this snippet from the sas macro at the following link which handles icd9 codes: http://www.seattlechildrens.org/research/child-health-behavior-and-development/mangione-smith-lab/me...

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 2 replies
  • 1193 views
  • 2 likes
  • 3 in conversation