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);
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;
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.
@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.
@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.
Hmm it's strange it worked like you suggested
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.
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?
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!
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;
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…
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
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
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.
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.