How to create a macro variable containing a list of variables in a DATA set
Sometimes it is very handy to have a macro variable contanining the variables names of the dataset. Here are the 2 different ways you can create a macro variable with list of variables names ...
*Method1: Using Proc Contents and Proc SQL;
proc contents data=sashelp.class out=class;
run;
proc sql noprint;
select distinct(name) into:vars separated by " " from class;
quit;
%put &vars;
*Method2: Using SASHELP tables and Proc SQL;
data class;
set sashelp.vcolumn(where=(libname="SASHELP" and memname="CLASS"));
keep name;
run;
proc sql noprint;
select distinct(name) into:vars separated by " " from class;
quit;
%put &vars;
*Method1: Using Proc Contents and Proc SQL;
proc contents data=sashelp.class out=class;
run;
proc sql noprint;
select distinct(name) into:vars separated by " " from class;
quit;
%put &vars;
*Method2: Using SASHELP tables and Proc SQL;
data class;
set sashelp.vcolumn(where=(libname="SASHELP" and memname="CLASS"));
keep name;
run;
proc sql noprint;
select distinct(name) into:vars separated by " " from class;
quit;
%put &vars;
%let dsid=%sysfunc(open(&lib..&dsn,i));
ReplyDelete%let numvar=%sysfunc(attrn(&dsid,nvar));
%do j =1 %to &numvar;
%let colname=',';
%let colname1=%sysfunc (varname(&dsid,&j));
%let colname=&colname1||&colname;
%end;
%let rc=%sysfunc (close(&dsid));