BookmarkSubscribeRSS Feed
Baba9
Obsidian | Level 7

Hello 

I would appreciate some help to advice if the code below is correct to apply the type criteria to both T407 and F12 diagnoses or should the code be written in a different way

Diag=0;

array type diag_type:;
array code diag_code:;

 do i=1 to 25;

 IF SUBSTR(code{i},1,4) in : ('T407') or SUBSTR(code{i}, 1,3) in ('F12') 
 and type{i} in: ('M', '1', '2', '3', 'W', 'X', 'Y'  '9' ) then Diag=1;

  end;

 if Diag=1  then output; ;

run;

 Thankyou 

5 REPLIES 5
ballardw
Super User

Do you want up to 25 records output for if the condition is met multiple times? If you want just one record when any of those is met I might suggest:

The Leave instruction will leave the do loop with the first assignment of diag.

Data want;
   set have;
   array type diag_type:;
    array code diag_code:;

 do i=1 to dim(type);/* use of Dim means if your data changes the number of type
                        variables the loop adjusts with the data
                     */

    IF SUBSTR(code{i},1,4) in : ('T407') or SUBSTR(code{i}, 1,3) in ('F12') 
    and type{i} in: ('M', '1', '2', '3', 'W', 'X', 'Y'  '9' ) then do;
        Diag=1;
        leave;
    end;

  end;

 if Diag=1  then output; ;

run;

 

Tom
Super User Tom
Super User

Your IF condition might be wrong. You have coded a boolean expression in the form of A or B and C.

 

Do you want that to be (A or B) and C

Or do you want that to be A or (B and C)?

 

Whichever it is add in the parentheses to make sure it is going to do what you want.

 

You can save a little time by stopping the loop when you have found the first hit.

diag=0;
do i=1 to 25 until(diag=1);

 

 

 

Baba9
Obsidian | Level 7
IF (SUBSTR(code{i},1,4) in : ('T407') or SUBSTR(code{i}, 1,3) in ('F12'))
and type{i} in: ('M', '1', '2', '3', 'W', 'X', 'Y' '9' ) then Diag=1;
Is the use of parenthese correct above
I want it to be (A or B) and C
Tom
Super User Tom
Super User

@Baba9 wrote:
IF (SUBSTR(code{i},1,4) in : ('T407') or SUBSTR(code{i}, 1,3) in ('F12'))
and type{i} in: ('M', '1', '2', '3', 'W', 'X', 'Y' '9' ) then Diag=1;
Is the use of parenthese correct above
I want it to be (A or B) and C

Looks good. 

 

Note that if you are going to use the : modifier so that the IN operator only compares up to the length of the shortest string you don't need to have the SUBSTR() functions.  You don't even really need two IN operators even though some of the codes are 3 characters and others are 4.

if code{i} in: ('T407' 'F12') and type{i} in: ('M' '1' '2' '3' 'W' 'X' 'Y' '9') then Diag=1;
pink_poodle
Barite | Level 11

Looks right to me, except the colons after first and third "in."

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 5 replies
  • 792 views
  • 3 likes
  • 4 in conversation