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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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