BookmarkSubscribeRSS Feed
Angmar
Obsidian | Level 7

Hello,

I've been using the code below to identify records that have one or both diagnoses (t67 or r55) and output them to LIB.NBHR05. Someone informed me that my code would output the same observations multiple times, so I wouldn't have the actual number of records with one or both diagnoses. Is there a work-around without creating a new variable?

Thanks. 

 
26 DATA LIB.NBHR05;
27 SET RED.RED05 (WHERE=(CITY='3'));
28 ARRAY DIAGNOSIS (25) DIAG_CODE_1-DIAG_CODE_25;
29 DO i =1 TO 25;
30 IF DIAGNOSIS(i) =: 'T67' OR DIAGNOSIS(i) =: 'R55' THEN OUTPUT;
31 END;
32 RUN;

1 REPLY 1
hashman
Ammonite | Level 13

@Angmar: You need to break out of the loop once the diagnosis searched for has been found:

data lib.nbhr05 (drop = i) ;                                                                                                                                                                                                                                    
  set red.red05 (where=(city='3')) ;                                                                                                                                                                                                                            
  array diag [25] diag_code_1-diag_code_25 ;                                                                                                                                                                                                                    
  do i = 1 to dim (diag) ;                                                                                                                                                                                                                                      
    if diag[i] in: ("T67", "R55") then do ;                                                                                                                                                                                                                     
      output ;                                                                                                                                                                                                                                                  
      leave ;                                                                                                                                                                                                                                                   
    end ;                                                                                                                                                                                                                                                       
  end ;                                                                                                                                                                                                                                                         
run ;        

However, the fact is that you don't need any looping at all:

data lib.nbhr05 ;                                                                                                                                                                                                                                    
  set red.red05 (where=(city='3')) ;                                                                                                                                                                                                                            
  array diag [25] diag_code_1-diag_code_25 ;                                                                                                                                                                                                                    
  if "T67" in diag or "R55" in diag ;                                                                                                                                                                                                                           
run ; 

Kind regards

Paul D.

 

 

 

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—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
  • 1 reply
  • 566 views
  • 0 likes
  • 2 in conversation