BookmarkSubscribeRSS Feed
tolapa
Fluorite | Level 6

Hi,

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;
run;

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

%next_ret;

 

Thanks

Pawan

4 REPLIES 4
RW9
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.

BrunoMueller
SAS Super FREQ

hi

 

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

%scan(&retailers, &i)

Bruno

Reeza
Super User

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

ballardw
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".

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1118 views
  • 0 likes
  • 5 in conversation