DATA Step, Macro, Functions and more

Using arrays with hospital discharge data/ duplicate output

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Using arrays with hospital discharge data/ duplicate output

[ Edited ]

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...]

 


Accepted Solutions
Solution
‎04-19-2016 03:50 PM
Super User
Posts: 10,543

Re: Using arrays with hospital discharge data/ duplicate output

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


All Replies
Solution
‎04-19-2016 03:50 PM
Super User
Posts: 10,543

Re: Using arrays with hospital discharge data/ duplicate output

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;
New Contributor
Posts: 3

Re: Using arrays with hospital discharge data/ duplicate output

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?

 

 

Super User
Posts: 10,543

Re: Using arrays with hospital discharge data/ duplicate output

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 216 views
  • 1 like
  • 2 in conversation