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-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
  • 4 replies
  • 797 views
  • 0 likes
  • 5 in conversation