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

Still new to writing arrays.  Looking to extract any records from a larger outpatient emergency discharge file 'entireEDdataset', to a new dataset 'hypertension', where any of the discharge diagnoses (principal or 28 secondary) are listed as the ICD-9 codes for hypertension.  The program below runs; however, I end up with duplicate observations for those records where the criteria is met more than once (example B and D below - duplicates record count = number of times the criteria was met).  How do I get the array to stop outputting once the criteria is met the first time?  Running Base SAS 9.3

Input Dataset 'EntireEDDataset':
ID
principaldx diag1 diag2 diag3       [...] diag28 [...other vars...]
A 401.00       123.91 656.00 789.10      567.00
B 402.00       403.00 123.40 234.50     999.09
C 123.45       678.91 546.20 698.20     546.80
D 403.00       402.00 401.00 405.00       123.45

data hypertension;
set entireEDdataset;
array dx [29] principaldx diag1-diag28;
     do i = 1 to 29;
           if dx(i) in : ('401', '402', '403', '404', '405') then output hypertension;
     end;
drop i;
run;

 

Dataset 'HYPERTENSION':
A, 401.00, 123.91, 656.00, 789.10, [...], 567.00 [...other vars...]
B, 402.00, 403.00, 123.40, 234.50, [...], 999.09 [...other vars...]
B, 402.00, 403.00, 123.40, 234.50, [...], 999.09 [...other vars...]
D, 403.00, 402.00, 401.00, 405.00, [...], 123.45 [...other vars...]
D, 403.00, 402.00, 401.00, 405.00, [...], 123.45 [...other vars...]
D, 403.00, 402.00, 401.00, 405.00, [...], 123.45 [...other vars...]
D, 403.00, 402.00, 401.00, 405.00, [...], 123.45 [...other vars...]

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

This will stop evaluating the IF the first time it finds a match and exit the Do loop.

data hypertension;
set entireEDdataset;
array dx [29] principaldx diag1-diag28;
     do i = 1 to 29;
           if dx(i) in : ('401', '402', '403', '404', '405') then do;
               output hypertension;
               leave;
           end;
     end;
drop i;
run;

View solution in original post

3 REPLIES 3
ballardw
Super User

This will stop evaluating the IF the first time it finds a match and exit the Do loop.

data hypertension;
set entireEDdataset;
array dx [29] principaldx diag1-diag28;
     do i = 1 to 29;
           if dx(i) in : ('401', '402', '403', '404', '405') then do;
               output hypertension;
               leave;
           end;
     end;
drop i;
run;
cgray
Fluorite | Level 6

Thank you so much.  Unfamilar with the leave statement - so, if the condition for i = 1 is met then the internal do loop activates ("then do") and outputs to hypertension; the leave statement then sends it back up to the external/first do loop to start i=2and so forth?

 

 

ballardw
Super User

I don't have any of your data to test but that is the purpose of LEAVE.

 

Note: To make your code a little easier to maintain you should consider using

 

do i = 1 to dim(dx);

 

Next time when they add or remove variables of interest then you only need to change the Array definition. Otherwise if you forget to change the 29 to 31 or 26 you either miss comparisons or get an array index out of range error at run time.

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

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
  • 3 replies
  • 1467 views
  • 1 like
  • 2 in conversation