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

hi all,

i am a beginner in sas and wondering if it is possible to write a macro to read external data like excel tables with similar names (eg. reports_a, reports_b, reports_c etc. ) which i saved in a certain file into sas. I wrote sth. like

%macro imp (path, name, n);

%Do i=1 %To &n;

proc import out=libl.test&i;

Datafile="&path.&name.&i..xls";

sheet="Table1";

run;

%end;

%mend imp

It doesnt look so good. Can someone give me some tips how i can do this? plus, actually i dont want to give n as a macro variable, cause i probabably dont know how many tables are there (of course i can check the file itself and get to know n myself, but that is not so schick, and everytime i got new reports coming in, this number is going to change), is it possible that sas does a loop and read the tables one by one until no tables are left unread? and is it possible that i save my sas dataset with the index from the original excel tables, like in my example a, b, c etc.? so that when i look at the name of the sas data sets, i know already exactly from whom this report comes from, from person a, b oder c and so on.

I am very grateful for any kind of help and suggestions!

BR  Dingdang

1 ACCEPTED SOLUTION
6 REPLIES 6
LinusH
Tourmaline | Level 20

You can first read the the contents of a directory (and stor it in a work table), and "pipe" all Excel file names into your macro instead, i.e. by using the call execute() construct.

Data never sleeps
Dingdang
Fluorite | Level 6

Hi LinusH

thanks for your prompt answer. I cannt really understand your method (have only one week experience with sas Smiley Sad). can you probably give me an example code, how this is going to look like?

would be very thankful for that.

Dingdang

LinusH
Tourmaline | Level 20

Ok, this is maybe the first week kind of logic, but you'll probably learn something anyhow...

You can either use filename pipe to issue a OS dir command - which you can call in a data step.

meta code:

filename myDir pipe 'dir search-path';

data _null_;

infile myDir;

input fileName input_spec;

if subsetting;

call execute('%imp(' || fileName || ');');

run;

Data never sleeps
Dingdang
Fluorite | Level 6

hi LinusH,

thanks for your code. In combination with Oleg's code i think i understand better how directory and call execute works Smiley Happy  very useful tips!

BR  Dingdang


Dingdang
Fluorite | Level 6

hi Oleg,

thanks for your answer. this is exactly what i am looking for Smiley Happy

BR  Dingdang

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 1169 views
  • 3 likes
  • 3 in conversation