DATA Step, Macro, Functions and more

How to auto create datasets names as macro variables

Reply
N/A
Posts: 0

How to auto create datasets names as macro variables

Hi there,

I was wondering if anyone knows how to create a macro variable for all the members in a library. I'm wanting to sort 30 datasets by a common variable and are considering using %do loop function, however would like to save time in typing the member names out in full.

Cheers
SAS Super FREQ
Posts: 8,744

Re: How to auto create datasets names as macro variables

Hi:
Several previous forum postings have discussed the use of DICTIONARY.TABLES and other types of DICTIONARY files in order to access information about SAS datasets. With PROC SQL, you can use the INTO clause in order to create macro variables from the results of a particular query. Some of the postings are:
http://support.sas.com/forums/thread.jspa?messageID=14535㣇
http://support.sas.com/forums/thread.jspa?messageID=9171⏓
http://support.sas.com/forums/thread.jspa?messageID=11252⯴

Here are some user group papers that outline what the "dictionary" tables do:
http://www2.sas.com/proceedings/sugi30/070-30.pdf
http://www2.sas.com/proceedings/sugi29/237-29.pdf
http://www.codecraftersinc.com/pdf/DictionaryTablesRefCard.pdf
http://www.lexjansen.com/pharmasug/2006/tutorials/tu03.pdf
http://www.qsl.net/kd6ttl/sas/sqlutilov.pdf

cynthia
N/A
Posts: 0

Re: How to auto create datasets names as macro variables

Thanks for your prompt response Cynthia.
I've not used Proc SQL before. I'll try to work my way out through the threads you've provided.

Cheers
SAS Super FREQ
Posts: 8,744

Re: How to auto create datasets names as macro variables

Hi:
You can certainly use CALL SYMPUT and DATA step to create your macro variables from the SASHELP views of the DICTIONARY tables. I believe some of the papers referenced show both methods.

cynthia
N/A
Posts: 0

Re: How to auto create datasets names as macro variables

I think this will work.

libname xx "c:\test";

data xx.a;
var1 = "testa";
run;

data xx.b;
var2 = "testb";
run;

proc sql noprint;
select memname into :dataset_names separated by ' '
from dictionary.tables
where lowcase(libname) = 'xx';
quit;

%put dataset_names = &dataset_names;


/* or you can use a data step */

data _null_;
set sashelp.vtable end=end;
where lowcase(libname) = 'xx';
length dataset_names $ 20;
retain dataset_names;
if _n_ = 1 then dataset_names = memname;
else dataset_names = strip(dataset_names) || ', ' || strip(memname) ;
if end then
do;
call symputx('dataset_names_2',dataset_names);
end;
run;

%put macro variable created by data step: &dataset_names_2;
Valued Guide
Posts: 2,175

Re: How to auto create datasets names as macro variables

call execute() in a data step might help[pre] data _null_ ;
set sashelp.vtable( where=( libname='YOURLIB' ));
call execute( 'proc sort data=yourlib.' !! memname );
call execute(' out= work.'!! memname ' );
call execute(' ; by BYVAR ; run; ' );
run ;[/pre]
I expect that solution is among those links Cynthia provided.

PeterC
Ask a Question
Discussion stats
  • 5 replies
  • 161 views
  • 0 likes
  • 3 in conversation