BookmarkSubscribeRSS Feed
JasonNC
Quartz | Level 8

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);

17 REPLIES 17
Reeza
Super User

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;



LaurieF
Barite | Level 11

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.

Reeza
Super User

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

LaurieF
Barite | Level 11

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

JasonNC
Quartz | Level 8

Hmm it's strange it worked like you suggested

Reeza
Super User

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. 

LaurieF
Barite | Level 11

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?

 

 

LaurieF
Barite | Level 11

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!

Reeza
Super User

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;

 

LaurieF
Barite | Level 11

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…

JasonNC
Quartz | Level 8

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

JasonNC
Quartz | Level 8

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

LaurieF
Barite | Level 11

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.

LaurieF
Barite | Level 11

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 17 replies
  • 1178 views
  • 4 likes
  • 3 in conversation