DATA Step, Macro, Functions and more

Macro Do loop - Skip a section when value fail a test

Accepted Solution Solved
Reply
Super Contributor
Posts: 420
Accepted Solution

Macro Do loop - Skip a section when value fail a test

Hi,

My Macro has 2 sections. At the end of section 1, I get a mean value for variable, say xyz. If this mean is less than 10, I want SAS skip section 2 to move on a new loop.

Can you please help me with that?

Thank you so much.

HHC

 

%macro comp;
%local i j;
%do i=1 %to 100;
  %do j=1 %to 100;
		
	*PART 1: A lot of code	;

			proc means data=abc;
			var xyz;
			output out=check_value
			mean=mean_xyz;
			run;
	
	
			IF mean_xyz<10 THEN SKIP THE PART2 Code below

	*PART 2: A lot of code	;
		

%end;
%end;
%mend;

Accepted Solutions
Solution
‎03-30-2017 06:02 PM
Valued Guide
Posts: 634

Re: Macro Do loop - Skip a section when value fail a test

@hhchenfxI have added the DATA step suggested by @LinusH and a macro %IF.

%macro comp;
%local i j;
%do i=1 %to 100;
  %do j=1 %to 100;
		
	*PART 1: A lot of code	;
	proc means data=abc;
			var xyz;
			output out=check_value
			mean=mean_xyz;
			run;
	data _null_;
      set check_value;
      call symputx('MEAN_XYZ',mean_xyz);
      run;
	
	%IF %sysevalf(&mean_xyz>=10) %THEN %do;

	*PART 2: A lot of code	;
	%end;	

%end;
%end;
%mend comp;

Notice that in the %IF the %SYSEVALF is used.  This is important because the mean (&MEAN_XYZ) will probably not be an integer.  Non-integers would cause an alphabetic comparison (10<9. = true).  I also reversed the comparison logic so that i could use a %DO block.  There is also a %GOTO (much like the DATA step GOTO), but i am not a fan of it.

 

 

View solution in original post


All Replies
Super User
Posts: 5,441

Re: Macro Do loop - Skip a section when value fail a test

[ Edited ]

You probably need the conditional logic from %if.

To be able to do so, you need the mean_xyz as a macro variable, something like:

data _null_;
   set check_value;
   call symputx('MEAN_XYZ',mean_xyz);
run;

 

Data never sleeps
Solution
‎03-30-2017 06:02 PM
Valued Guide
Posts: 634

Re: Macro Do loop - Skip a section when value fail a test

@hhchenfxI have added the DATA step suggested by @LinusH and a macro %IF.

%macro comp;
%local i j;
%do i=1 %to 100;
  %do j=1 %to 100;
		
	*PART 1: A lot of code	;
	proc means data=abc;
			var xyz;
			output out=check_value
			mean=mean_xyz;
			run;
	data _null_;
      set check_value;
      call symputx('MEAN_XYZ',mean_xyz);
      run;
	
	%IF %sysevalf(&mean_xyz>=10) %THEN %do;

	*PART 2: A lot of code	;
	%end;	

%end;
%end;
%mend comp;

Notice that in the %IF the %SYSEVALF is used.  This is important because the mean (&MEAN_XYZ) will probably not be an integer.  Non-integers would cause an alphabetic comparison (10<9. = true).  I also reversed the comparison logic so that i could use a %DO block.  There is also a %GOTO (much like the DATA step GOTO), but i am not a fan of it.

 

 

Super Contributor
Posts: 420

Re: Macro Do loop - Skip a section when value fail a test

It is so great.
Thank you so much for your help.
HHC
☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 181 views
  • 0 likes
  • 3 in conversation