Create a macro with a list of variables

Accepted Solution Solved
Reply
Contributor AD
Contributor
Posts: 31
Accepted Solution

Create a macro with a list of variables

I have a dataset called "Data" and I want to put all the variable names into a macro variable called "varlist".  One way I know how to do this is using the following code:

PROC CONTENTS DATA = Data OUT=varnames (KEEP = NAME) NOPRINT;
RUN;

DATA _NULL_; 
  FILE "C:\folder\Varlist.txt";
  SET varnames END=lastvar;
  IF _N_ = 1 THEN PUT '%LET varlist = ';
                  PUT NAME;
  IF lastvar THEN PUT ';';
RUN;

%include "C:\folder\Varlist.txt";

Is there another way to do this that doesn't require me to create an external text file?

Let me know if more information is needed.  Thanks,

Andy


Accepted Solutions
Solution
‎01-17-2013 01:02 PM
Super User
Posts: 5,509

Re: Create a macro with a list of variables

Posted in reply to AncaTilea

A couple of tweaks to one line:

select trim(name) into : var_list separated by " "

TRIM will get rid of extra blanks, and the separator should be a blank, not a null.  If one of your variable names is 32 characters long, you'll need to have a blank after it.  This is one of the few cases within the software where "" and " " are different.

View solution in original post


All Replies
Super Contributor
Posts: 543

Re: Create a macro with a list of variables

Here it goes.

Please note the UPPERCASE;

proc sql ;

    select name into: var_list separated by ""

    from dictionary.columns

    where memname = "CLASS" & libname = "SASHELP";

quit;

Good luck.

Anca.

Solution
‎01-17-2013 01:02 PM
Super User
Posts: 5,509

Re: Create a macro with a list of variables

Posted in reply to AncaTilea

A couple of tweaks to one line:

select trim(name) into : var_list separated by " "

TRIM will get rid of extra blanks, and the separator should be a blank, not a null.  If one of your variable names is 32 characters long, you'll need to have a blank after it.  This is one of the few cases within the software where "" and " " are different.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 191 views
  • 4 likes
  • 3 in conversation