Recently, I found Proc Sql can allow you to create macro variable with long string values separated by either space or other delimiters directly without extra step for table generation and null data step. Here is the modify codes.
proc sql; select distinct varname into :varlist separated by ' ' from table; quit; %put &varlist.;
Sometime the macro variable is created within a macro, then the macro variable will be just local macro variable and it will not be available beyond the macro statement. To force the macro variable to be available in the global environment, using %Global.
%macro new; proc sql; select distinct varname into :varlist separated by ' ' from table; quit; %global varlist; %mend new; %put &varlist.;
If you would like the varlist items to be wrapped with quotation and separated with comma, then use the following code.
%macro new; proc sql; select distinct '"'||varname||'"' as temp into :varlist separated by ', ' from table; quit; %global varlist; %mend new; %put &varlist.;
If the same macro variable is defined or used in multiple macros, making sure the macro variable is set at the global level, because macro variables are local at default. You might get error message like below if the macro variable is not forced to be global variable.
ERROR: Attempt to %GLOBAL a name (NAME) which exists in a local environment.
Here is an example of use previously created macro variable in another macro.
%macro del; %global varlist; proc datasets nolist; delete &varlist; run; %mend del;
previous reference: http://www2.sas.com/proceedings/sugi30/028-30.pdf
proc sql; create table varlist as select distinct varname from table; quit; %let varlist =; data _null_; set varlist; call symput('varlist', trim(resolve('&varlist'))||' '||trim(varname)); run;
null is a special data set name, although sas doesn’t create a data set called “_null”. It allows sas to carry out commands in the data steps. There is no output data set.
Omitting value in the %let syntax will produce a null value for the macro variable.
resolve function: resolve the value of macro variable in data step.
CALL SYMPUT assigns value produced in a DATA step to macro variable(S).