Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro Definition Not Working

Status
Not open for further replies.

jvelez1

Programmer
Aug 20, 2009
3
US
Hello!

I am running into a problem with a macro definition. When I run the sql code with a %let statement it works fine:

options macrogen mlogic symbolgen mprint mfile;

%let num=6;

proc sql ;
select "'"||trim(a.vkont)||"'"
into :nbr_&num. separated by ' '
from armdb2.ext_WO_list2 a
where group=&num.;
quit;

%put &nbr_6.

I'm intentionally omitting the noprint on the proc sql so that I can see what the macro resolves to on the output. The %put statement shows the contents of my macro just fine.

NOTE: Remote submit to SAS50 commencing.
223 %put &nbr_6.;
'000000335266' '000000335291' '000000335332' '000000335366'
'000000335460' '000000335484' '000000335516' '000000335522'
'000000375665' '000000375828' '000000375859' '000000375895'
NOTE: Remote submit to SAS50 complete.

Now I try to do the same thing but with a macro definition:

%macro javi(num);
proc sql ;
select "'"||trim(a.vkont)||"'"
into :nbr_&num. separated by ' '
from armdb2.ext_WO_list2 a
where group=&num.;
quit;
%mend javi;

%javi(num=7);

%put &nbr_7.;

Check out the log:

NOTE: Remote submit to SAS50 commencing.
204 %macro javi(num);
205 proc sql ;
206 select "'"||trim(a.vkont)||"'"
207 into :nbr_&num. separated by ' '
208 from armdb2.ext_WO_list2 a
209 where group=&num.;
210 quit;
211 %mend javi;
212
213 %javi(num=7);
NOTE: The PROCEDURE SQL printed pages 181-200.
NOTE: PROCEDURE SQL used (Total process time):
real time 0.06 seconds
cpu time 0.04 seconds


WARNING: Apparent symbolic reference NBR_7 not resolved.
214
215 %put &nbr_7.;
&nbr_7.
NOTE: Remote submit to SAS50 complete.

Am I going crazy here? What am I missing? And yes, there are values on the table for 7 as there were for 6. The even crazier thing is that on the output (because of the noprint) I can see the values of the macro coming out!

I would appreciate any help I can get. Thanks -Javi

 
The reason for this is your macro variable is only available inside the macro. When you 'call' the macro var outside of your macro it can't find it. If you want the macro to stay around declare it before hand.

KLaz
 
I'm not understanding this. I run the macro definition and then use:

%javi(num=7);

Isn't this calling the macro? When I run %javi(num=7) it should execute the sql code and macro definition and output the new macro &nbr_7. Proof of that is the output that shows the content of my new macro variable.

Once I use the macro variable &num I don't use it for the %put statement or anywhere else in my code. In its place a new macro variable is created &nbr_7 or &nbr_x. Why would the new macro &nbr_7 not stay around? Is it because I'm creating a macro inside of a macro? I've done that before with no problems.

I guess I've been looking at this for too long :) Thanks for your help and prompt response KLaz.

 
KLaz,

I had some time to work on this and I added the following statement:
%global nbr_&num.;

The macro is working now! Here's the full code. Thanks for your help. -Javi

%macro javi(num);
%global nbr_&num.;
proc sql noprint;
select "'"||trim(a.vkont)||"'"
into :nbr_&num. separated by ' '
from armdb2.ext_WO_list2 a
where group=&num.;
quit;
%mend javi;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top