Help using Base SAS procedures

problem of using macro to import multiple csv files

Accepted Solution Solved
Reply
Contributor
Posts: 38
Accepted Solution

problem of using macro to import multiple csv files

Hi,

There are tens of csv files in folder D:\users\...\desktop\list. I need to import them into SAS.

For illustration purpose, I only take 2 files as the example.

%let country1=abudhabi;

%let country2=argentina;

%macro import;

%do i =1 % to 2;

proc import out = &&country&i

                 datafile="D:\users\...\desktop\list\&&country&i.csv"

                 dbms=csv replace;

run;

%end;;

%mend import;

%import;

But the codes did not work and the error messages are as below:

*WARNING: Apparent symbolic reference D not resolved;

*WARNING: Apparent symbolic reference D not resolved;

*WARNING: Apparent symbolic reference COUNTRY1CSV not resolved;

*ERROR: Physical file does not exist,D:\Users\...\Desktop\list\&&country&i.csv;

*ERROR: Import unsuccessful. See SAS Log for details.;


Can anyone advise me what the problem is and how to resovle it? Thanks.


Accepted Solutions
Solution
‎06-19-2012 02:32 AM
Frequent Contributor
Posts: 95

Re: problem of using macro to import multiple csv files

Posted in reply to comeon2012

You are right. I did not test it before I submitted above code.

First period acts as a delimiter for &i in the first pass.

Second period acts as a delimiter for &Country1 in the second pass.

&&Country&i.csv -> Country1csv

&&Country&i..csv -> abudahbicsv

&&Country&i...csv -> abudhabi.csv

View solution in original post


All Replies
Frequent Contributor
Posts: 95

Re: problem of using macro to import multiple csv files

Posted in reply to comeon2012

&&country&i.csv  will resolve to Country1csv for i=1. && will resolve to & and &i will resolve to 1 in the first pass.

Macro facility will try to resolve &Country1csv macro variable reference which fails.

The single period following macro variable reference will act as a delimiter and won't be part of the resulting text.

You will need to insert another '.' (period) after &&country&i to delimit the value of macro variable from the the file extension.

Replace '&&country&i.csv' to '&&country&i..csv'

I am not sure about the warning 'WARNING: Apparent symbolic reference D not resolved;' I could not see where macro variable D is referenced in the code.

Contributor
Posts: 38

Re: problem of using macro to import multiple csv files

It still did not work when I add another period.

But after I accidentally added another two period, i.e,  '&&country&i...csv', it worked although  'WARNING: Apparent symbolic reference D not resolved' still appeared. Anyways, the csv file were imported to SAS. Thank you Alpay.

Solution
‎06-19-2012 02:32 AM
Frequent Contributor
Posts: 95

Re: problem of using macro to import multiple csv files

Posted in reply to comeon2012

You are right. I did not test it before I submitted above code.

First period acts as a delimiter for &i in the first pass.

Second period acts as a delimiter for &Country1 in the second pass.

&&Country&i.csv -> Country1csv

&&Country&i..csv -> abudahbicsv

&&Country&i...csv -> abudhabi.csv

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 452 views
  • 4 likes
  • 2 in conversation