Help using Base SAS procedures

need to create a macro to mport multiple .csv files based on user inputs

Reply
Occasional Learner
Posts: 1

need to create a macro to mport multiple .csv files based on user inputs

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

Super User
Super User
Posts: 7,404

Re: need to create a macro to mport multiple .csv files based on user inputs

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.

SAS Super FREQ
Posts: 683

Re: need to create a macro to mport multiple .csv files based on user inputs

hi

 

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

%scan(&retailers, &i)

Bruno

Super User
Posts: 17,840

Re: need to create a macro to mport multiple .csv files based on user inputs

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

Super User
Posts: 10,500

Re: need to create a macro to mport multiple .csv files based on user inputs

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

Ask a Question
Discussion stats
  • 4 replies
  • 139 views
  • 0 likes
  • 5 in conversation