0

I wrote a proc SQl normally first without any macro variables and it works, now I would like to convert it to macro and it doesnt work, can you see where the problem is ?

%macro macrova(LIB=, DATA=);

proc sql noprint;

/*creating an in memory variable with all char variables from the dataset*/

        select name into :names separated by ' '
        from dictionary.columns where libname="&lib" and memname="&data" and type='char'; 

/*to make sure we have the same order of the variables an in-memory variable ORDER is created*/

        SELECT NAME INTO: ORDER SEPARATED BY ' '
        from sashelp.vcolumn where LIBNAME= "&lib" and memname="&datA" ; 

quit;

%MEND;

OPTIONS MLOGIC SYMBOLGEN;
%macrova(LIB=SASHELP,DATA=CLASS)


%PUT &NAMES;
%PUT ℴ 

LOG:

55   %PUT &NAMES;
WARNING: Apparent symbolic reference NAMES not resolved.
&NAMES
56   %PUT ℴ
WARNING: Apparent symbolic reference ORDER not resolved.
&ORDER
3
  • While it's perfectly legal to do so, it's a bit confusing to see dictionary.columns in one query and sashelp.vcolumn in the second... Commented Sep 12, 2017 at 15:59
  • @Joe ok, it happens when I use both sashelp.vcolumn and as suggested by DomPazz even after writing global statement, now it gives no names for the macro variable. Commented Sep 12, 2017 at 16:05
  • Why are the WHERE clauses different? The results won't like up if they're not the same. Commented Sep 12, 2017 at 16:11

2 Answers 2

4

You need to either define the macro variables before calling the macro or add %GLOBAL statement to the macro. Otherwise the macro variabless will be created as local and disappear when the macro exits.

Note that there is no variable named ORDER in dictionary.columns, I assume you meant to use the VARNUM variable.

Also there is no need to run two queries to generate two macro variables.

%macro macrova(LIB=, DATA=);
%global names order ;
proc sql noprint;
select name
     , varnum
  into :names separated by ' '
     , :order separated by ' '
from dictionary.columns 
where libname=%upcase("&lib")
  and memname=%upcase("&data")
  and type='char'
order by 2
; 
quit;
%mend macrova;

%macrova(LIB=SASHELP,DATA=CLASS)
%put &NAMES;
%put ℴ
Sign up to request clarification or add additional context in comments.

Comments

0

The names are being created as LOCAL values inside the macro and are not available outside it. Try adding

%global NAMES ORDER;

to the macro before the SELECT statements.

2 Comments

Now for the statement %put &order it is blank.
See Tom's answer. The missing %GLOBAL was the reason for the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.