DATA Step, Macro, Functions and more

Global Variable not Resolving in Macro

Reply
Contributor
Posts: 20

Global Variable not Resolving in Macro

I am generating a Global variable using a SELECT :INTO and it was resolving before using a macro. Now I need to bound a large part of my code inside a macro and use a conditional CALL EXECUTE as a means to bypass or run my code. Now that I am generating this variable inside the macro, it is not resolving. Below is a simplified version of the code. Thanks for the help!

%MACRO FCST_CHK1(IVINC);
%GLOBAL &IVINC; /* INCENTIVE FILE VIN COUNT */

PROC SQL; /* CREATE VIN COUNT FOR INCENTIVE FILE TEST */
	SELECT COUNT(VIN17)
	INTO :&IVINC
	FROM INCV;
	
DATA _NULL_; /* TEST IF INCENTIVE FILE IS EMPTY */
	IF &IVINC = 0 THEN DO;
		FILE RETURNC;
		PUT	@01	'RETURN CODE 01: THE INCENTIVE FILE IS EMPTY';
		ABORT RETURN 01;
	END;

%MEND FCST_CHK1;

DATA _NULL_;
	IF "&FCST_Y" = '0' THEN CALL EXECUTE('%FCST_CHK1(&IVINC)');

 

 

Super User
Posts: 23,998

Re: Global Variable not Resolving in Macro

%GLOBAL IVINC; 

I don't think you need the ampersand, otherwise it resolves to whatever value &ivinc has so you're setting the wrong thing to be global.

 


@DonnieJ wrote:

I am generating a Global variable using a SELECT :INTO and it was resolving before using a macro. Now I need to bound a large part of my code inside a macro and use a conditional CALL EXECUTE as a means to bypass or run my code. Now that I am generating this variable inside the macro, it is not resolving. Below is a simplified version of the code. Thanks for the help!

%MACRO FCST_CHK1(IVINC);
%GLOBAL &IVINC; /* INCENTIVE FILE VIN COUNT */

PROC SQL; /* CREATE VIN COUNT FOR INCENTIVE FILE TEST */
	SELECT COUNT(VIN17)
	INTO :&IVINC
	FROM INCV;
	
DATA _NULL_; /* TEST IF INCENTIVE FILE IS EMPTY */
	IF &IVINC = 0 THEN DO;
		FILE RETURNC;
		PUT	@01	'RETURN CODE 01: THE INCENTIVE FILE IS EMPTY';
		ABORT RETURN 01;
	END;

%MEND FCST_CHK1;

DATA _NULL_;
	IF "&FCST_Y" = '0' THEN CALL EXECUTE('%FCST_CHK1(&IVINC)');

 

 



 

Contributor
Posts: 20

Re: Global Variable not Resolving in Macro

I tried that first, but it still did not resolve.

Super User
Posts: 23,998

Re: Global Variable not Resolving in Macro

Macro variables do not resolve in single quotes. I can't run your code to test it ... so showing what errors you're getting or 'not resolving' would help a lot here.
Super User
Posts: 6,931

Re: Global Variable not Resolving in Macro

This combination just by itself would create an error:

 

%macro FCST_CHK1 (IVINC);

   %global IVINC;

 

Your macro has a local macro variable named IVINC.  You cannot move it to the global table by using a %GLOBAL statement.  In fact, SAS will assume that's what you want to do, will not allow it, and will give you an error message so that you don't think that this actually worked.  You would see that if  you were to try:

 

%macro FCST_CHK1(IVINC);

   %global IVINC;

 

You can easily get around the problem by letting SQL populate the local variable (into : ivinc), and copying the result to a global variable.  For example:

 

%global new_name;

 

Then after the PROC SQL:

 

%let new_name = &ivinc;

Ask a Question
Discussion stats
  • 4 replies
  • 97 views
  • 0 likes
  • 3 in conversation