Dear
I need to create a macro with all name in a data set. Please suggest
data one;
input a b c;
datalines;
1 2 3
3 4 4
;
proc sql;
select name into: var separated by ""
from one;
quit;
output needed:
var=a b c
Thank you
Use dictionary.columns as source, where libname = 'WORK' and memname = 'ONE'.
Use dictionary.columns as source, where libname = 'WORK' and memname = 'ONE'.
Hi,
two other approaches (avoiding dictionary.columns):
/* one way: */
data _null_;
length n $ 32000;
do di=open('sashelp.class', 'I') while(di ne 0);
do i=1 to attrn(di,'NVARS');
n=catx(" ", n, varname(di,i));
end;
end;
call symputx('var', n);
run;
%put &=var;
/* second way */
proc transpose data = sashelp.cars(obs=0) out = _tmp(keep = _name_);
var _all_;
run;
proc sql noprint;
select _name_
into :var separated by " "
from _tmp;
drop table _tmp;
quit;
%put &=var;
All the best
Bart
Yet another approach is to use PROC CONTENTS to determine the column names in your data set, and then PROC SQL to create the macro variable. I find this preferable to using dictionary tables, as the dictionary tables can be very slow if you have databases with lots of tables/variables opened, or if you have many LIBNAMEs active (or both).
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.