BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mimi
Calcite | Level 5

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Amir
PROC Star

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

9 REPLIES 9
shivas
Pyrite | Level 9

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

Patrick
Opal | Level 21

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

kuridisanjeev
Quartz | Level 8

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

yaswanthj
Calcite | Level 5

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

Amir
PROC Star

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.

Astounding
PROC Star

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.

Patrick
Opal | Level 21

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

art297
Opal | Level 21

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

mimi
Calcite | Level 5

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 .

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
  • 9 replies
  • 15154 views
  • 0 likes
  • 8 in conversation