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

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
Barite | Level 11
It is so great.
Thank you so much for your help.
HHC

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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