Infinite do-while loop

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

Infinite do-while loop

Hi,
I would like to ask for your help we me problem. I created function which I am calling in the data step. This function is dynamically calling macro with different parameters. However, the function seems to stuck in infinite loop. I cannot find the cause of it.
Here is the function:

 

PROC FCMP OUTLIB = work.funcs.sql;
	FUNCTION func_create_train_datasets(output_table $, input_table_tgt1 $, datetgt1 $, input_table_tgt0 $, datetgt0 $, input_table_tgtALL $) $;
									/*"TrainDataHypoOut", "TGT1TrainData", "DateTgtSCO", "TGT0TrainData", "DateDataTRA", "HYPO_OUT_TEST_DATASET_TGT_1"*/
		_test = 1; 	
		_test2 = 1;
		i = 0;		
		j = 0;
		PUT 'in the function befor first loop';
		DO WHILE(_test);
			PUT 'in the first loop befor second loop';
			i + 1;
			DO WHILE(_test2);
				PUT 'in the socond loop ';
				j + 1;
				new_output_table 		= output_table;
				new_input_table_tgt1 	= input_table_tgt1;
				new_datetgt1 			= CATS(datetgt1, j);
				new_input_table_tgt0 	= input_table_tgt0;
				new_datetgt0 			= CATS(datetgt0, i);
				IF j = 1 THEN new_input_table_tgtALL = input_table_tgtALL;
				ELSE 
					DO;
					new_input_table_tgtALL	= CATS(output_table, TRANWRD(datetgt0, '.', '_')); 
					END;
				
				IF SYMEXIST(new_datetgt1) = 1 AND SYMEXIST(new_datetgt0) = 1 THEN 
					DO;
					new_datetgt1 = CATS("&", datetgt1, j);
					new_datetgt0 = CATS("&", datetgt0, i);
					rc = RUN_MACRO('macro_create_train_datasets',new_output_table, new_input_table_tgt1, new_datetgt1, new_input_table_tgt0, new_datetgt0, new_input_table_tgtALL);
					END;
				ELSE IF SYMEXIST(new_datetgt1) = 0 AND SYMEXIST(new_datetgt0) = 1 THEN 
					DO;
					_test2 = 0;
					j = 0;
					END;
				ELSE 
					DO;
					_test = 0;
					_test2 = 0;
					END;
			END;
		END;
	RETURN ;
ENDSUB;
RUN;

Thank you! 


Accepted Solutions
Solution
‎09-25-2015 06:23 AM
Super User
Posts: 11,114

Re: Infinite do-while loop

You only have one place where you address the value of _TEST. But in this code:

ELSE IF SYMEXIST(new_datetgt1) = 0 AND SYMEXIST(new_datetgt0) = 1 THEN 
DO;
	_test2 = 0;
	j = 0;
END;

you set _test2 so that that the inner loop will stop but there is nothing for the outer loop test. So if you ever get into that condition you never get a chance to reset _test.

 

View solution in original post


All Replies
Frequent Contributor
Posts: 144

Re: Infinite do-while loop

This happen to me a time, it was because i use I and J as counters, but are not defined as local, so i rewritte at every call

Occasional Contributor
Posts: 12

Re: Infinite do-while loop

Thank you for your quick reply. Would you be so kind and explain little bit more about the issue? Also could you propose specific solution?
Frequent Contributor
Posts: 144

Re: Infinite do-while loop

I don't know if it's your case, because I have never define a macro like this but, when insite a macro (in macro language) you use macro variables, a good practice is to define as local all your parameter, for example, in the case that you use I and J as counters, you should before use it, defined as local with
%local i j;

because other macro could use also I and J parameters. If they are not defined as local, all macros edit the same values I and J and could do that you don't exit of a loop.

 

Since PROC FCMP execute like in a DATA STEP, could be that the I variable and J are also used inside the macro that you call and overwrite the existing value of I and J. Try to rename the I and J to check if works

Occasional Contributor
Posts: 12

Re: Infinite do-while loop

Thank you for additional explanation and help. I renamed all the counters and also logical (those i am using as boolean variables - _test, _test2) variables. Unfortunately it didn't work out. The same problem occured.
SAS Enterprise Guide got stuck as before. The process could not be stoped through GUI as before. I had to kill the process through ctrl+alt+delete process window as before.
Any other suggestions?
Solution
‎09-25-2015 06:23 AM
Super User
Posts: 11,114

Re: Infinite do-while loop

You only have one place where you address the value of _TEST. But in this code:

ELSE IF SYMEXIST(new_datetgt1) = 0 AND SYMEXIST(new_datetgt0) = 1 THEN 
DO;
	_test2 = 0;
	j = 0;
END;

you set _test2 so that that the inner loop will stop but there is nothing for the outer loop test. So if you ever get into that condition you never get a chance to reset _test.

 

🔒 This topic is solved and locked.

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

Discussion stats
  • 5 replies
  • 489 views
  • 1 like
  • 3 in conversation