BookmarkSubscribeRSS Feed
LuisMijares
Calcite | Level 5

Hello,

I have a block of code that is quite repetitive, as it processes similar tasks across multiple datasets. To streamline this, I’m trying to create a macro, medpar_analysis, to loop through the dataset medpar for the years 2019 to 2021.

However, when I run the macro, I encounter the following error: 

ERROR 161-185: No matching DO/SELECT statement. 

 

The error points to a specific line of code within the macro. Interestingly, when I run the same code for an individual dataset (e.g., medpar_2019), it compiles and executes without any issues. This leads me to believe the issue lies in how the macro handles loops or iterates over the datasets.

I’m unsure where the unmatched DO loop or SELECT statement might be causing the error. Could you help identify the problem and suggest a solution?



"127 0; if strip(DGNS[i]) in PSYCH_CODES then ALZH_MEDPAR = 0; if strip(DGNS[i]) in
127 ! OUD_CODES then ALZH_MEDPAR = 0; end; end; do i = 1 to dim(DGNS); if
---
161
127 ! missing(ALZH_MEDPAR) then do; if missing(DGNS[i]) then ALZH_MEDPAR ="



 

%macro medpar_analysis;
%do years=2019 %to 2021;

data output.merged_medpar_mbsf_pdpn_&years._1;
set output.merged_medpar_mbsf_pdpn_&years._1;
array DGNS[25] $ DGNS_1_CD -- DGNS_25_CD;
DEPRESSION_MEDPAR = .;
NONALZH_DEMEN_MEDPAR = .;
ALZH_MEDPAR = .;
PNEUMO_MEDPAR = .;
hf_medpar =.;
PRKNSN_MEDPAR = .;
STROKE_TIA_MEDPAR = .;
ANXI_MEDICARE_MEDPAR =.;
BIPOLAR_MEDPAR = .;
TBI_MEDPAR = .;
DRUGS_MEDPAR = .;
SCHIOT_MEDPAR = .;
OUD_ANY_MEDPAR = .;

array depression_codes[50] $8 _temporary_ ('F0631', 'F0632', 'F310', 'F3110', 'F3111', 'F3112', 'F3113',
'F312', 'F3130', 'F3131', 'F3132', 'F314', 'F315', 'F3160',
'F3161', 'F3162', 'F3163', 'F3164', 'F3171', 'F3173', 'F3175',
'F3176', 'F3177', 'F3178', 'F3181', 'F3189', 'F319', 'F320',
'F321', 'F322', 'F323', 'F324', 'F325', 'F328', 'F3289',
'F329', 'F32A', 'F330', 'F331', 'F332', 'F333', 'F3340',
'F3341', 'F3342', 'F338', 'F339', 'F340', 'F341', 'F4321', 'F4323');

array nonalzhimers_codes[84] $8 _temporary_ ('F0150', 'F0151', 'F01511', 'F01518', 'F0152', 'F0153', 'F0154',
'F01A0', 'F01A11', 'F01A18', 'F01A2', 'F01A3', 'F01A4', 'F01B0',
'F01B11', 'F01B18', 'F01B2', 'F01B3', 'F01B4', 'F01C0', 'F01C11',
'F01C18', 'F01C2', 'F01C3', 'F01C4', 'F0280', 'F0281', 'F02811',
'F02818', 'F0282', 'F0283', 'F0284', 'F02A0', 'F02A11', 'F02A18',
'F02A2', 'F02A3', 'F02A4', 'F02B0', 'F02B11', 'F02B18', 'F02B2',
'F02B3', 'F02B4', 'F02C0', 'F02C11', 'F02C18', 'F02C2', 'F02C3',
'F02C4', 'F0390', 'F0391', 'F03911', 'F03918', 'F0392', 'F0393',
'F0394', 'F03A0', 'F03A11', 'F03A18', 'F03A2', 'F03A3', 'F03A4',
'F03B0', 'F03B11', 'F03B18', 'F03B2', 'F03B3', 'F03B4', 'F03C0',
'F03C11', 'F03C18', 'F03C2', 'F03C3', 'F03C4', 'F05', 'G138',
'G3101', 'G3109', 'G311', 'G312', 'G3183', 'G94', 'R4181');

array alzhimers_codes[4] $8 _temporary_ ('G300', 'G301','G308', 'G309'); 


do i = 1 to dim(DGNS);
           if not missing(DGNS[i])then do; 
if strip(DGNS[i]) in depression_codes then DEPRESSION_MEDPAR=1; 
     end; 
  end; 
 
 
/* this part of the code will an a 0 to the depression indicator if there are any diagnosis codes*/ 
 
do i = 1 to dim(DGNS); 
if missing(DEPRESSION_MEDPAR) then do; 
       if strip(DGNS[i]) in nonalzhimers_codes then DEPRESSION_MEDPAR =0; 
 
        if strip(DGNS[i]) in alzhimers_codes then DEPRESSION_MEDPAR = 0; 
        if strip(DGNS[i]) in pneumonia_codes then DEPRESSION_MEDPAR= 0; 
        if strip(DGNS[i]) in hf_codes then DEPRESSION_MEDPAR = 0; 
        if strip(DGNS[i]) in prknsn_codes then DEPRESSION_MEDPAR = 0; 
        if strip(DGNS[i]) in stroke_codes then DEPRESSION_MEDPAR = 0; 
        if strip(DGNS[i]) in stroke_exclusion_codes then DEPRESSION_MEDPAR = 0; 
        if strip(DGNS[i]) in anxiety_codes then DEPRESSION_MEDPAR = 0; 
        if strip(DGNS[i]) in bipolar_codes then DEPRESSION_MEDPAR = 0; 
        if strip(DGNS[i]) in TBI_codes then DEPRESSION_MEDPAR = 0;   
        if strip(DGNS[i]) in DRUG_USE_CODES then DEPRESSION_MEDPAR = 0; 
        if strip(DGNS[i]) in PSYCH_CODES then DEPRESSION_MEDPAR = 0; 
        if strip(DGNS[i]) in OUD_CODES then DEPRESSION_MEDPAR = 0; 
end; 
    end; 
 
 
 
 
*assigning -9 to missing DGNS values; 
 
do i = 1 to dim(DGNS);
if missing(DEPRESSION_MEDPAR) then do; 
if missing(DGNS[i]) then DEPRESSION_MEDPAR =-9; 
 
   end; 
 end; 
 
 
 
 
*
end; 
 
/* verifying*/ 
 
   indicator1 = 0;
    indicator2 = 0;
    indicator3 = 0;
    indicator4 = 0;
    indicator5 = 0;
    indicator6 = 0;
    indicator7 = 0;
    indicator8 = 0;
    indicator9 = 0;
    indicator10 = 0;
    indicator11 = 0;
    indicator12 = 0;
    indicator13 = 0;
    indicator14 = 0;
    indicator15 = 0;
    indicator16 = 0;
    indicator17 = 0;
    indicator18 = 0;
    indicator19 = 0;
    indicator20 = 0;
    indicator21 = 0;
    indicator22 = 0;
    indicator23 = 0;
    indicator24 = 0;
    indicator25 = 0;
    indicator_all = 0;
 
 
if strip(DGNS_1_CD) in depression_codes then indicator1 = 1;
if strip(DGNS_2_CD) in depression_codes then indicator2 = 1;
if strip(DGNS_3_CD) in depression_codes then indicator3 = 1;
if strip(DGNS_4_CD) in depression_codes then indicator4 = 1;
if strip(DGNS_5_CD) in depression_codes then indicator5 = 1;
if strip(DGNS_6_CD) in depression_codes then indicator6 = 1;
if strip(DGNS_7_CD) in depression_codes then indicator7 = 1;
if strip(DGNS_8_CD) in depression_codes then indicator8 = 1;
if strip(DGNS_9_CD) in depression_codes then indicator9 = 1;
if strip(DGNS_10_CD) in depression_codes then indicator10 = 1;
if strip(DGNS_11_CD) in depression_codes then indicator11 = 1;
if strip(DGNS_12_CD) in depression_codes then indicator12 = 1;
if strip(DGNS_13_CD) in depression_codes then indicator13 = 1;
if strip(DGNS_14_CD) in depression_codes then indicator14 = 1;
if strip(DGNS_15_CD) in depression_codes then indicator15 = 1;
if strip(DGNS_16_CD) in depression_codes then indicator16 = 1;
if strip(DGNS_17_CD) in depression_codes then indicator17 = 1;
if strip(DGNS_18_CD) in depression_codes then indicator18 = 1;
if strip(DGNS_19_CD) in depression_codes then indicator19 = 1;
if strip(DGNS_20_CD) in depression_codes then indicator20 = 1;
if strip(DGNS_21_CD) in depression_codes then indicator21 = 1;
if strip(DGNS_22_CD) in depression_codes then indicator22 = 1;
if strip(DGNS_23_CD) in depression_codes then indicator23 = 1;
if strip(DGNS_24_CD) in depression_codes then indicator24 = 1;
if strip(DGNS_25_CD) in depression_codes then indicator25 = 1;
 
 
 
indicator_count=sum(of indicator1-indicator25);
if indicator_count > 0 then indicator_all = 1;
 
 
 
 
 
/****************************************************/ 
/* Nonalzhimers_MEDPAR*/  
 
*this part of the code will assign a 1 to any depression codes; 
 
do i = 1 to dim(DGNS);
           if not missing(DGNS[i])then do; 
if strip(DGNS[i]) in nonalzhimers_codes then NONALZH_DEMEN_MEDPAR=1; 
end; 
end; 
 
/* this part of the code will an a 0 to the depression indicator if there are any diagnosis codes*/
do i = 1 to dim(DGNS); 
if missing(NONALZH_DEMEN_MEDPAR) then do; 
if strip(DGNS[i]) in depression_codes then NONALZH_DEMEN_MEDPAR = 0; 
 
if strip(DGNS[i]) in alzhimers_codes then NONALZH_DEMEN_MEDPAR = 0;
        if strip(DGNS[i]) in pneumonia_codes then NONALZH_DEMEN_MEDPAR = 0;
        if strip(DGNS[i]) in hf_codes then NONALZH_DEMEN_MEDPAR = 0; 
        if strip(DGNS[i]) in prknsn_codes then NONALZH_DEMEN_MEDPAR = 0;
        if strip(DGNS[i]) in stroke_codes then NONALZH_DEMEN_MEDPAR = 0;
        if strip(DGNS[i]) in stroke_exclusion_codes then NONALZH_DEMEN_MEDPAR = 0;
        if strip(DGNS[i]) in anxiety_codes then NONALZH_DEMEN_MEDPAR = 0;
        if strip(DGNS[i]) in bipolar_codes then NONALZH_DEMEN_MEDPAR = 0; 
        if strip(DGNS[i]) in TBI_codes then NONALZH_DEMEN_MEDPAR = 0;  
        if strip(DGNS[i]) in DRUG_USE_CODES then NONALZH_DEMEN_MEDPAR = 0;
        if strip(DGNS[i]) in PSYCH_CODES then NONALZH_DEMEN_MEDPAR = 0;
        if strip(DGNS[i]) in OUD_CODES then NONALZH_DEMEN_MEDPAR = 0;
end; 
end; 
 
*assigning -9 to missing DGNS values; 
do i = 1 to dim(DGNS);
if missing(NONALZH_DEMEN_MEDPAR) then do; 
if missing(DGNS[i]) then NONALZH_DEMEN_MEDPAR =-9; 
 
     end; 
end; 
 
 
 
 
 
/* verifying*/ 
/* Initialize non-alzheimer indicators to 0 */
nonalzhimers_indicator_1 = 0;
nonalzhimers_indicator_2 = 0;
nonalzhimers_indicator_3 = 0;
nonalzhimers_indicator_4 = 0;
nonalzhimers_indicator_5 = 0;
nonalzhimers_indicator_6 = 0;
nonalzhimers_indicator_7 = 0;
nonalzhimers_indicator_8 = 0;
nonalzhimers_indicator_9 = 0;
nonalzhimers_indicator_10 = 0;
nonalzhimers_indicator_11 = 0;
nonalzhimers_indicator_12 = 0;
nonalzhimers_indicator_13 = 0;
nonalzhimers_indicator_14 = 0;
nonalzhimers_indicator_15 = 0;
nonalzhimers_indicator_16 = 0;
nonalzhimers_indicator_17 = 0;
nonalzhimers_indicator_18 = 0;
nonalzhimers_indicator_19 = 0;
nonalzhimers_indicator_20 = 0;
nonalzhimers_indicator_21 = 0;
nonalzhimers_indicator_22 = 0;
nonalzhimers_indicator_23 = 0;
nonalzhimers_indicator_24 = 0;
nonalzhimers_indicator_25 = 0;
nonalzhimers_indicator_all = 0;
 
/* Check DGNS_X_CD variables against nonalzhimers_codes */
if strip(DGNS_1_CD) in nonalzhimers_codes then nonalzhimers_indicator_1 = 1;
if strip(DGNS_2_CD) in nonalzhimers_codes then nonalzhimers_indicator_2 = 1;
if strip(DGNS_3_CD) in nonalzhimers_codes then nonalzhimers_indicator_3 = 1;
if strip(DGNS_4_CD) in nonalzhimers_codes then nonalzhimers_indicator_4 = 1;
if strip(DGNS_5_CD) in nonalzhimers_codes then nonalzhimers_indicator_5 = 1;
if strip(DGNS_6_CD) in nonalzhimers_codes then nonalzhimers_indicator_6 = 1;
if strip(DGNS_7_CD) in nonalzhimers_codes then nonalzhimers_indicator_7 = 1;
if strip(DGNS_8_CD) in nonalzhimers_codes then nonalzhimers_indicator_8 = 1;
if strip(DGNS_9_CD) in nonalzhimers_codes then nonalzhimers_indicator_9 = 1;
if strip(DGNS_10_CD) in nonalzhimers_codes then nonalzhimers_indicator_10 = 1;
if strip(DGNS_11_CD) in nonalzhimers_codes then nonalzhimers_indicator_11 = 1;
if strip(DGNS_12_CD) in nonalzhimers_codes then nonalzhimers_indicator_12 = 1;
if strip(DGNS_13_CD) in nonalzhimers_codes then nonalzhimers_indicator_13 = 1;
if strip(DGNS_14_CD) in nonalzhimers_codes then nonalzhimers_indicator_14 = 1;
if strip(DGNS_15_CD) in nonalzhimers_codes then nonalzhimers_indicator_15 = 1;
if strip(DGNS_16_CD) in nonalzhimers_codes then nonalzhimers_indicator_16 = 1;
if strip(DGNS_17_CD) in nonalzhimers_codes then nonalzhimers_indicator_17 = 1;
if strip(DGNS_18_CD) in nonalzhimers_codes then nonalzhimers_indicator_18 = 1;
if strip(DGNS_19_CD) in nonalzhimers_codes then nonalzhimers_indicator_19 = 1;
if strip(DGNS_20_CD) in nonalzhimers_codes then nonalzhimers_indicator_20 = 1;
if strip(DGNS_21_CD) in nonalzhimers_codes then nonalzhimers_indicator_21 = 1;
if strip(DGNS_22_CD) in nonalzhimers_codes then nonalzhimers_indicator_22 = 1;
if strip(DGNS_23_CD) in nonalzhimers_codes then nonalzhimers_indicator_23 = 1;
if strip(DGNS_24_CD) in nonalzhimers_codes then nonalzhimers_indicator_24 = 1;
if strip(DGNS_25_CD) in nonalzhimers_codes then nonalzhimers_indicator_25 = 1;
 
/* Sum the indicators and check for at least one match */
nonalzhimers_indicator_count = sum(of nonalzhimers_indicator_1-nonalzhimers_indicator_25);
if nonalzhimers_indicator_count > 0 then nonalzhimers_indicator_all = 1; 
 
 
 
 
****************************************************************************************************
/* Alzhimers diagnosis*
 
/*Assign 1 to ALZH_MEDPAR for matches in alzhimers_codes;
 
do i = 1 to dim(DGNS);
    if not missing(DGNS[i]) then do;
        if strip(DGNS[i]) in alzhimers_codes then ALZH_MEDPAR = 1;
    end;
end;
 
/* Assign 0 to ALZH_MEDPAR for other diagnosis codes */
do i = 1 to dim(DGNS);
    if missing(ALZH_MEDPAR) then do;
        if strip(DGNS[i]) in depression_codes then ALZH_MEDPAR = 0;
        if strip(DGNS[i]) in nonalzhimers_codes then ALZH_MEDPAR = 0;
        if strip(DGNS[i]) in pneumonia_codes then ALZH_MEDPAR = 0;
        if strip(DGNS[i]) in hf_codes then ALZH_MEDPAR = 0;
        if strip(DGNS[i]) in prknsn_codes then ALZH_MEDPAR = 0;
        if strip(DGNS[i]) in stroke_codes then ALZH_MEDPAR = 0;
        if strip(DGNS[i]) in stroke_exclusion_codes then ALZH_MEDPAR = 0;
        if strip(DGNS[i]) in anxiety_codes then ALZH_MEDPAR = 0;
        if strip(DGNS[i]) in bipolar_codes then ALZH_MEDPAR = 0;
        if strip(DGNS[i]) in TBI_codes then ALZH_MEDPAR = 0;
        if strip(DGNS[i]) in DRUG_USE_CODES then ALZH_MEDPAR = 0;
        if strip(DGNS[i]) in PSYCH_CODES then ALZH_MEDPAR = 0;
        if strip(DGNS[i]) in OUD_CODES then ALZH_MEDPAR = 0;
         end;
end;
 
/* Assign -9 to ALZH_MEDPAR if all DGNS values are missing */
do i = 1 to dim(DGNS);
    if missing(ALZH_MEDPAR) then do;
        if missing(DGNS[i]) then ALZH_MEDPAR = -9;
    end;
end;
 
 
 
/* verifying*/ 
/* Initialize alzheimer indicators to 0 */
alzh_indicator_1 = 0;
alzh_indicator_2 = 0;
alzh_indicator_3 = 0;
alzh_indicator_4 = 0;
alzh_indicator_5 = 0;
alzh_indicator_6 = 0;
alzh_indicator_7 = 0;
alzh_indicator_8 = 0;
alzh_indicator_9 = 0;
alzh_indicator_10 = 0;
alzh_indicator_11 = 0;
alzh_indicator_12 = 0;
alzh_indicator_13 = 0;
alzh_indicator_14 = 0;
alzh_indicator_15 = 0;
alzh_indicator_16 = 0;
alzh_indicator_17 = 0;
alzh_indicator_18 = 0;
alzh_indicator_19 = 0;
alzh_indicator_20 = 0;
alzh_indicator_21 = 0;
alzh_indicator_22 = 0;
alzh_indicator_23 = 0;
alzh_indicator_24 = 0;
alzh_indicator_25 = 0;
alzh_indicator_all = 0;
 
/* Check DGNS_X_CD variables against alzhimers_codes */
if strip(DGNS_1_CD) in alzhimers_codes then alzh_indicator_1 = 1;
if strip(DGNS_2_CD) in alzhimers_codes then alzh_indicator_2 = 1;
if strip(DGNS_3_CD) in alzhimers_codes then alzh_indicator_3 = 1;
if strip(DGNS_4_CD) in alzhimers_codes then alzh_indicator_4 = 1;
if strip(DGNS_5_CD) in alzhimers_codes then alzh_indicator_5 = 1;
if strip(DGNS_6_CD) in alzhimers_codes then alzh_indicator_6 = 1;
if strip(DGNS_7_CD) in alzhimers_codes then alzh_indicator_7 = 1;
if strip(DGNS_8_CD) in alzhimers_codes then alzh_indicator_8 = 1;
if strip(DGNS_9_CD) in alzhimers_codes then alzh_indicator_9 = 1;
if strip(DGNS_10_CD) in alzhimers_codes then alzh_indicator_10 = 1;
if strip(DGNS_11_CD) in alzhimers_codes then alzh_indicator_11 = 1;
if strip(DGNS_12_CD) in alzhimers_codes then alzh_indicator_12 = 1;
if strip(DGNS_13_CD) in alzhimers_codes then alzh_indicator_13 = 1;
if strip(DGNS_14_CD) in alzhimers_codes then alzh_indicator_14 = 1;
if strip(DGNS_15_CD) in alzhimers_codes then alzh_indicator_15 = 1;
if strip(DGNS_16_CD) in alzhimers_codes then alzh_indicator_16 = 1;
if strip(DGNS_17_CD) in alzhimers_codes then alzh_indicator_17 = 1;
if strip(DGNS_18_CD) in alzhimers_codes then alzh_indicator_18 = 1;
if strip(DGNS_19_CD) in alzhimers_codes then alzh_indicator_19 = 1;
if strip(DGNS_20_CD) in alzhimers_codes then alzh_indicator_20 = 1;
if strip(DGNS_21_CD) in alzhimers_codes then alzh_indicator_21 = 1;
if strip(DGNS_22_CD) in alzhimers_codes then alzh_indicator_22 = 1;
if strip(DGNS_23_CD) in alzhimers_codes then alzh_indicator_23 = 1;
if strip(DGNS_24_CD) in alzhimers_codes then alzh_indicator_24 = 1;
if strip(DGNS_25_CD) in alzhimers_codes then alzh_indicator_25 = 1;
 
/* Sum the indicators and check for at least one match */
alzh_indicator_count = sum(of alzh_indicator_1-alzh_indicator_25);
if alzh_indicator_count > 0 then alzh_indicator_all = 1;

 

2 REPLIES 2
quickbluefish
Barite | Level 11

Not sure if you're still looking for a solution, but if so, I would suggest putting this code in a code block (using the little SAS icon in the formatting tools when you're submitting a question here) or in an actual text editor that allows block indentation and then making all the indentation correct - in particular, the do-end blocks:

if x>3 then do;
    if y>7 then do;
        * stuff ;
    end;
end;

This will really help reveal where you might be missing a DO -- the error message you're getting seems to indicate that you have a stray END somewhere.  I looked through your code (I'm very familiar with MedPAR) and I don't see one -- there's the one that looks out of place, but it appears to be commented out - right above the first /* verifying */ - so it shouldn't be causing a problem.  

 

A few suggestions for making your code less repetitive / more efficient:

  -- inside the loop where you keep doing strip(dgns[i]) --- instead of this, first thing inside the loop, just do this:

dgns[i]=strip(dgns[i]);

...after that, you don't need strip anymore.

I'm not sure I understand the point of the indicator steps.  Are you just trying to preserve the location in the array where you found the code of interest?  If so, you can skip that entire thing and instead modify your main loop for a given condition like this:


do i=1 to dim(DGNS);
  if missing(dgns[i]) then leave;
  dgns[i]=strip(dgns[i]);  * i do not think this is even needed ;
  if dgns[i] in depression_codes then do;
    DEPRESSION_MEDPAR=1;
    depression_pos=i;  * save the location in the array where this occurred ;
    leave;
  end;
end;

Note the two LEAVE statements -- the first says, if we're reached the point in the array where we find a missing dgns, we can exit the loop - there is no need to check further.  MedPAR does not have blanks followed by more codes.  The second leave says, we've found the condition (depression) of interest -- set our flag DEPRESSION_MEDPAR to 1, store the position (i) as depression_pos, and leave (no need to keep checking unless for some reason you care if there is more than 1 depression code for the same visit - in general, this is not useful).  If you do for whatever reason want to count all the positions where depression is present, then do this instead:

depression_count=0;
do i=1 to dim(DGNS);
  if missing(dgns[i]) then leave;
  dgns[i]=strip(dgns[i]);  * i do not think this is even needed ;
  if dgns[i] in depression_codes then do;
    DEPRESSION_MEDPAR=1;
    depression_count+1;  * keep track of the number of depression codes found ;
  end;
end;
depression_count_all=(depression_count>0);

Note the 2nd leave statement is now gone, so the loop will continue until it finds the first missing dgns.

Tom
Super User Tom
Super User

Didn't we already figure this out on your first posting?

You have a statement comment in the code that was missing its semicolon. That turned one of the DO statements into part of the comment.  Hence you ended up with an extra END statement.

 

https://communities.sas.com/t5/SAS-Programming/Troubleshooting-a-Macro-Error-Resolving-No-Matching-D...

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 517 views
  • 0 likes
  • 3 in conversation