Hi all,
The following macro works good but when I tried to print the macro variables using %put statement gives not found error.
But when I am not using proc sql in the cnts macro and into :tab or :tba , the put statement working fine. why is that error pls somebody help
me with it. Thanks in advance
%macro cnts(dis=, ind=, ovr=, tt=);
proc sql;
select count(distinct &dis.) format=2. into :&ovr.
from &ind.
where trt IN(&tt.);
quit;
%mend cnts;
%cnts(dis=patient, ind=ae, ovr=tab, tt='AB');
%cnts(dis=patient, ind=ae, ovr=tba, tt='BA');
%put " &tab. &tba.";
getting the following error msg
WARNING: Apparent symbolic reference TAB not resolved.
WARNING: Apparent symbolic reference TBA not resolved.
Hi,
You can try making the argument passed to the macro function a global macro variable:
%macro cnts(dis=, ind=, ovr=, tt=);
%global &ovr; /* make available outside of macro function */
proc sql;
select count(distinct &dis.) format=2. into :&ovr.
from &ind.
where trt IN(&tt.);
quit;
%mend cnts;
%cnts(dis=patient, ind=ae, ovr=tab, tt='AB');
%cnts(dis=patient, ind=ae, ovr=tba, tt='BA');
%put " &tab. &tba.";
The %put statement does not require quotes to print the values.
Regards,
Amir.
Hi,
Try this...not tested...
%macro cnts(dis=, ind=, ovr=, tt=);
%global tab;
proc sql;
select count(distinct &dis.) format=2. into :&ovr.
from &ind.
where trt IN(&tt.);
quit;
%mend cnts;
%put &tab;
Thanks,
Shiva
Consider using code like below:
%macro cnts(dis=, ind=, ovr=, tt=);
proc sql;
select count(distinct &dis.) format=2. into :&ovr.
from &ind.
where trt IN(&tt.);
quit;
%put %bquote(&ovr) has value %bquote(&&&ovr);
%mend cnts;
%cnts(dis=patient, ind=ae, ovr=tab, tt='AB');
%cnts(dis=patient, ind=ae, ovr=tba, tt='BA');
Hi...
nothing wrong with your code..
you are trying to print the local macro variable values in outside of the macro.it can not be possible.
Use the put statement with in the Macro,then you can get the result.
Thanks
Sanjeev.K
As per above
Please use below code..
%macro cnts(dis=, ind=, ovr=, tt=);
proc sql;
select count(distinct &dis.) format=2. into :&ovr.
from &ind.
where trt IN(&tt.);
quit;
%put " &&&ovr.";
%mend cnts;
%cnts(dis=patient, ind=ae, ovr=tab, tt='AB');
%cnts(dis=patient, ind=ae, ovr=tba, tt='BA');
Hi,
You can try making the argument passed to the macro function a global macro variable:
%macro cnts(dis=, ind=, ovr=, tt=);
%global &ovr; /* make available outside of macro function */
proc sql;
select count(distinct &dis.) format=2. into :&ovr.
from &ind.
where trt IN(&tt.);
quit;
%mend cnts;
%cnts(dis=patient, ind=ae, ovr=tab, tt='AB');
%cnts(dis=patient, ind=ae, ovr=tba, tt='BA');
%put " &tab. &tba.";
The %put statement does not require quotes to print the values.
Regards,
Amir.
Well, if you really ran the program that you posted, you are trying to %PUT macro variables that don't exist.
There is no macro variable named &TAB.
There is no macro variable named &TBA
Some of the comments about global vs. local would apply if you try to examine &OVR.
Good luck.
@Astounding
I believe you've missed the "select into :&ovr" bit in the macro. Macrovars &TAB and &TBA get created when the macro executes (values passed to OVR when calling the macro). It's about scope of the macro vars like others already said.
Of course you 'could' simply move to put statement into the macro. Does the following do what you are trying to accomplish?:
%macro cnts(dis=, ind=, ovr=, tt=);
proc sql noprint;
select count(distinct &dis.) format=2. into :&ovr.
from &ind.
where sex IN(&tt.);
quit;
%put &&&ovr.;
%mend cnts;
%cnts(dis=age, ind=sashelp.class, ovr=tab, tt='M')
%cnts(dis=age, ind=sashelp.class, ovr=tba, tt='F')
Thank you all for the answer. with in cnt macro Iwas able to print. I needed those variable to be stored as global . I needed those vars in the last step.
I just needed to check if the vars are storing the correct value. %global &ovr. worked fine for me .
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.