Suppose you want to create a macro variable that puts all the variable names from a data set.
1. Get all the variable names from a data set
To see the variable names, use the following code :
*Selecting all the variables;LIBNAME : Library Name
proc sql noprint;
select name into : vars separated by " "
from dictionary.columns
where LIBNAME = upcase("work")
and MEMNAME = upcase("predata");
quit;
MEMNAME : Dataset Name
Note : Make sure library and dataset names in CAPS. Or you can use UPCASE function to make it in caps.
To see the variable names, use the following code :
%put variables = &vars.;2. Get all the numeric variable names from a data set
*Selecting numeric variables;3. Get all the character variable names from a data set
proc sql noprint;
select name into : numvar separated by " "
from dictionary.columns
where LIBNAME = "WORK"
and MEMNAME = "PREDATA"
and type = 'num';
quit;
*Selecting character variables;4. Get all the variable names except ID variable
proc sql noprint;
select name into : charvar separated by " "
from dictionary.columns
where LIBNAME = "WORK"
and MEMNAME = "PREDATA"
and type = 'char';
quit;
proc sql noprint;
select name into : vars separated by " "
from dictionary.columns
where LIBNAME = upcase("work")
and MEMNAME = upcase("predata")
and upcase(name) ne upcase("id");
quit;
GREAT EXPLANATION
ReplyDeleteI always liked the way you simplify the syntax with examples throughout in your website. Great work!!
ReplyDeleteHowever, examples are missing here making it a little hard for me to understand.