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

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;
1 ACCEPTED SOLUTION

Accepted Solutions
ArtC
Rhodochrosite | Level 12

@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

3 REPLIES 3
LinusH
Tourmaline | Level 20

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
ArtC
Rhodochrosite | Level 12

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

 

 

hhchenfx
Rhodochrosite | Level 12
It is so great.
Thank you so much for your help.
HHC

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