BookmarkSubscribeRSS Feed
Fluorite | Level 6


I am trying to write a SAS macro that will import multiple .csv files present in the current folder as per the user inputs given. Here is what i have written so far but it is throwing exit 2. I am running SAS9. on unix. In the below example, Meijer and publix will be user provided details. I want to write a macro that will import files titled Meijer and publix from current folder and create data sets of the same name. This list i.e retailers can vary everytime users run this code. I am currently getting exit 2 which says too few arguements for %scan function. Please advise.


%let retailers= Meijer publix;

%macro next_ret;
%let i=1;
%do %while (%scan(&retailers &i) ne );
%let next_retailer = %scan(&retailers &i);

proc import datafile = "&next_retailer" out=&next_retailer_stores dbms=csv replace;

%let i = %eval(&i + 1);





Diamond | Level 26 RW9
Diamond | Level 26

Am going to suggest a far out there idea here, I put "import multiple csv" into the search area at the top, and there was about 100 topics suggesting many different ways of doing it /sarcasm Smiley Happy


To note, you don't need macro - you never need macro.

Also, I would avoid using proc import - this is guessing procedure so you don't really know what your going to get out.  Write a datastep and code in exactly what each column should be, what it looks like, and how to read it.  I.e. you take 100% full control over your data.




Arguments for SAS macro functions are separated with a comma, your scan expression should look like this:

%scan(&retailers, &i)


Super User

Check SAS 9.4 macro appendix documentation for examples of looping through variables and several other useful examples. 

Super User

Are your file names actually going to match  "&next_retailer"? As in does that macro variable have the file extention such as .csv? You may need, at a minimum,  "&next_retailer..csv" and the two . are needed. The first one tells the macro compilier it is the end of the name of the macro variable the second is part of the actual file name.

And it really is a good idea to have the complete path as well as I'm not sure that you will get the correct "current folder".



Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg



Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 4 replies
  • 5 in conversation