%put to print macro variable

Accepted Solution Solved
Reply
Contributor
Posts: 25
Accepted Solution

%put to print macro variable

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.


Accepted Solutions
Solution
‎07-31-2012 08:27 AM
Super Contributor
Posts: 282

Re: %put to print macro variable

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.

View solution in original post


All Replies
Super Contributor
Posts: 349

Re: %put to print macro variable

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

Respected Advisor
Posts: 4,173

Re: %put to print macro variable

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

Super Contributor
Posts: 276

Re: %put to print macro variable

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

Contributor
Posts: 70

Re: %put to print macro variable

Posted in reply to kuridisanjeev

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

Solution
‎07-31-2012 08:27 AM
Super Contributor
Posts: 282

Re: %put to print macro variable

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.

Super User
Posts: 5,518

Re: %put to print macro variable

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.

Respected Advisor
Posts: 4,173

Re: %put to print macro variable

Posted in reply to Astounding

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

PROC Star
Posts: 7,492

Re: %put to print macro variable

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

Contributor
Posts: 25

Re: %put to print macro variable

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 .

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 879 views
  • 0 likes
  • 8 in conversation