BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
knveraraju91
Barite | Level 11

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 

1 ACCEPTED SOLUTION
3 REPLIES 3
yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



PaigeMiller
Diamond | Level 26

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).

--
Paige Miller

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 715 views
  • 3 likes
  • 4 in conversation