BookmarkSubscribeRSS Feed
Bloker
Calcite | Level 5
Hi

I have 11 tables that i want to run a query to extract certain data (columns) from.
all tables have same column's names

how can i create a query to extract the columns i need from all tables?

each table has like 100 columns so its hideous to do it 11 times! there should be a smarter way to do it...

(I am a newbie btw)
2 REPLIES 2
1162
Calcite | Level 5
A macro would be a good way to handle this. Put your query into the macro and then just run it 11 times, each time with a new dataset name. In the example below, each loop of the macro creates a tmpfile which is then appended to a master outfile. The tmpfile is then deleted to prepare for the next macro invocation.

Example:
[pre]
%macro runquery (infile=, outfile=);

proc sql;
create tmpfile as
select col1, col3, col5
from &infile;
quit;

proc append base=&outfile data=tmpfile;
run;

proc datasets library=work nolist;
delete tmpfile;
run;
quit;

%mend runquery;


%runquery (infile=work.table1, outfile=work.endtable);
%runquery (infile=work.table2, outfile=work.endtable);
.
.
.
%runquery (infile=work.table11, outfile=work.endtable);
[/pre]
ssas
Calcite | Level 5
i feel you can also do in this way

%let s1=table1; * input table name1;
%let s2=outputtale1; * output table name;
%let s3=ap1; * all your append files dataset;

* if your exporting all your files from the external source you can use &s1, &s2 in proc import;
data libname.&s2; * if you want to save all your specific tables in a library;
set &s1;
keep col1 col2 col3 ......;
run;
* you can stop here if you want seperate tables;

proc appennd base=&s2 data=&s3;
run;
or
data tablename;
set &s2 &s3.....;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2 replies
  • 731 views
  • 0 likes
  • 3 in conversation