DATA Step, Macro, Functions and more

Strange Macro behavior

Reply
Regular Contributor
Posts: 155

Strange Macro behavior

Hi,

I am passing two parameters for the condition but the condition using id parameter highlighted in below is not being executed

 

%Macro Anlsys (tab,id);

%put &tab;
%put &id;

 

%if &id=T %then %do;

Data &tab._New;
%if &tab=C13 %then %do;
set S04_&tab.;
%end;
%else %do;
set S02_&tab.;
%end;
If Cd='4' then delete;
If Pcd in ('S','C') then delete;


%if &tab=C7 %then %do;
If INFO_CD in ('','Q') then delete;
%end;
run;

proc sql noprint;
create table &tab._mtx_mrg as
select t1.*,t2.Flg
from &tab._New as t1 left join &tab._EQ as t2 on
%If &tab=C13 %then %do;
(t1.Bk_1=t2.Bk_P;
%end;
%else %do;
(t1.Bk_1=t2.Bk_P);
%end;
quit;

Data &tab._Req &tab._Rsch;
set &tab._mtx_mrg;
If flg = 'No' then delete;
If flg= 'Yes' then output &tab._Req;
else output &tab._Rsch;
run;
%end;

%else %do;

Data &tab._&id.;

%if &tab=C13 %then %do;
set S04_&tab.;
%end;
%else %do;
set S02_&tab.;
%end;

 

%if &id=E %then %do;
if ACCT_TYPE in ('AN','AU','BO','HM','MO','TN','TR','TU','VN','VU') then delete;
%end;

 

if ACCT_TYPE in ('CD','NC','HI','PB') then delete;
run;

 

%Mend;

%Anlsys(C13,T);

%Anlsys(C7,E);

Super User
Posts: 17,829

Re: Strange Macro behavior

Because this is a data step you don't the macro conditions:

 

What happens if you replace it with data step code:

 

 

if "&id" = "E" and ACCT_TYPE in ('AN','AU','BO','HM','MO','TN','TR','TU','VN','VU') then delete;



Super Contributor
Posts: 251

Re: Strange Macro behavior

But @Reeza, the code is being generated as part of a data step within the macro. Theoretically the line should be generated on the second call.

Super User
Posts: 17,829

Re: Strange Macro behavior

@LaurieF I agree it should work, just positing a work around. Personally, I find working within data step code whenever possible makes it easier on me in the long run, but that seems to be personal preference.

 

I think I've had this discussion with someone on here before and using the macro's makes it slightly more efficient due to how/when the macro variables resolve. 

Super Contributor
Posts: 251

Re: Strange Macro behavior

[ Edited ]

@Reeza, I love macros. They're my thing. But they're only my thing to stop me doing repetative work.

 

I'd still love to see the log with the debugging stuff turned on though.

Regular Contributor
Posts: 155

Re: Strange Macro behavior

Hmm it's strange it worked like you suggested

Super User
Posts: 17,829

Re: Strange Macro behavior

If you want to fix the macro, I would do @LaurieF suggestion and run the second macro with MPRINT/SYMBOLGEN on and see what happens at that step. Post the log if you can't figure it out. 

Super Contributor
Posts: 251

Re: Strange Macro behavior

Without having access to the datasets, it's hard to see. Can you do this:

option mprint notes symbolgen mlogic;

then rerun and show the log?

 

 

Super Contributor
Posts: 251

Re: Strange Macro behavior

[ Edited ]

Hang on - your macro isn't compiling - you've got a missmatch between your %dos and %ends. That'll make a difference.

 

And this ain't good:

%If &tab=C13 %then %do;
(t1.Bk_1=t2.Bk_P;
%end;
%else %do;
(t1.Bk_1=t2.Bk_P);
%end;

The two conditional lines are identical except the first one is missing a closing parenthesis. Oops!

Super User
Posts: 17,829

Re: Strange Macro behavior

Formatted code for legibility

%Macro Anlsys (tab,id);
	%put &tab;
	%put &id;

	%if &id=T %then
		%do;

			Data &tab._New;
				%if &tab=C13 %then
					%do;
						set S04_&tab.;
					%end;
				%else
					%do;
						set S02_&tab.;
					%end;

				If Cd='4' then
					delete;

				If Pcd in ('S','C') then
					delete;

				%if &tab=C7 %then
					%do;
						If INFO_CD in ('','Q') then
							delete;
					%end;
			run;

			proc sql noprint;
				create table &tab._mtx_mrg as
					select t1.*,t2.Flg
						from &tab._New as t1 left join &tab._EQ as t2 on
							%If &tab=C13 %then

					%do;
						(t1.Bk_1=t2.Bk_P;
					%end;
				%else
					%do;
						(t1.Bk_1=t2.Bk_P);
					%end;
			quit;

			Data &tab._Req &tab._Rsch;
				set &tab._mtx_mrg;

				If flg = 'No' then
					delete;

				If flg= 'Yes' then
					output &tab._Req;
				else output &tab._Rsch;
			run;

		%end;
	%else
		%do;

			Data &tab._&id.;
				%if &tab=C13 %then
					%do;
						set S04_&tab.;
					%end;
				%else
					%do;
						set S02_&tab.;
					%end;

				%if &id=E %then
					%do;
						if ACCT_TYPE in ('AN','AU','BO','HM','MO','TN','TR','TU','VN','VU') then
							delete;
					%end;

				if ACCT_TYPE in ('CD','NC','HI','PB') then
					delete;
			run;

%mend;

 

Super Contributor
Posts: 251

Re: Strange Macro behavior

[ Edited ]

This is what you need to fix up to make the macro compile (the last line needs adding)

%else %do;
Data &tab._&id.;
%if &tab=C13 %then %do;
set S04_&tab.;
%end;
%else %do;
set S02_&tab.;
%end;
%end;

You missed out the last %end - but you have other problems in the code as well…

Regular Contributor
Posts: 155

Re: Strange Macro behavior

Hey @LaurieF i am running multiple conditions in the datastep id i use %end in the datastep then other conditions won't execute.The program is running woth out errors but without parenthesis the macro is not taking the value

Regular Contributor
Posts: 155

Re: Strange Macro behavior

I ran it with options i custmozied the code while posting as it's confidential but when i checked the log with original code

 

When i made modification as reeza suggested it saying it's true and when in the same code with out suggestion it says false

 

SYMBOLGEN: Macro variable ID resolves to EQ
MLOGIC(TDR_EQ_NONEQ): %IF condition "&id"="EQ" is TRUE
MPRINT(TDR_EQ_NONEQ): if RT_ACCT_TYPE in ('AN','AU','BO','HM','MO','TN','TR','TU','VN','VU')
then delete;

 

SYMBOLGEN: Macro variable TAB resolves to CH13
SYMBOLGEN: Macro variable ID resolves to EQ
MPRINT(TDR_EQ_NONEQ): Data CH13_EQ_cln;
SYMBOLGEN: Macro variable TAB resolves to CH13
SYMBOLGEN: Macro variable ID resolves to EQ
MPRINT(TDR_EQ_NONEQ): set CH13_EQ;
SYMBOLGEN: Macro variable ID resolves to EQ
MLOGIC(TDR_EQ_NONEQ): %IF condition &id=EQ is FALSE

Super Contributor
Posts: 251

Re: Strange Macro behavior

I suspect when you modified the code, you missed some stuff out. When I fixed up the two bugs I showed, it did compile, and it did appear to generate the right stuff.

Super Contributor
Posts: 251

Re: Strange Macro behavior

By George, I've got it - and it was, as it so often is, macro quoting, as hinted at by @Reeza.

 

If you replace the macro comparison with

%if %nrquote(&id) = %nrquote(EQ) %then %do;

which effectively does the same thing as @Reeza's suggestion, it all falls into place. Without the quoting, it's interpreted logically along the lines of

%if EQ EQ EQ %then %do;

which will parse at execution time, after a fashion, but it's meaningless.

 

Because eq is a reserved operand in macro code, it has to be quoted so that it isn't interpreted as an operand.

Ask a Question
Discussion stats
  • 17 replies
  • 205 views
  • 4 likes
  • 3 in conversation