I have a saved csv_files SAS Dataset with the following entries:
name path
aapl F:\Data\aapl.csv
msft F:\Data\msft.csv
ibm F:\Data\ibm.csv
goog F:\Data\goog.csv
I then use this SAS dataset to locate the csv files and import them into SAS using the following Macro which has a while loop:
options symbolgen mlogic nomprint;
/*Leave options on as SAS will issue a message when Do While Loop is false*/
%let dsname = csv_files;
%let libto = l;
%Macro get_data(n,index);
/*n: is a counting macro variable representing the lower limit of the number of CSV files
to be read into SAS. Index: is a variable that can be thought of as
observation number( row number) representing the upper limit
of the number of CSV files to be read into SAS.*/
%DO %WHILE (&n <=&index);
data _NULL_;
set &libto..&dsname end=last;
if _N_ =&n then Do;
call symput('path',path); /* Get file path from CSV_files*/
call symput('dsn',name); /* Get dataset name from CSV_files*/
if not last then index+1; /* Create macro variable Index */
else if last then call symput ('index',index);
End;
run;
proc import datafile= "&path"
out= &libto..&dsn
dbms=dlm
replace;
delimiter="," ;
getnames=yes;
run;
%End;
%Mend get_data;
/* Macro Call*/
%get_data(1,4);
When I execute this Macro I always iterate over the first observation (in this case AAPL). It simply imports the aapl.csv file over and over....what's wrong?
UPDATE
Following the suggestion of Dmitry Shopin I changed my while for a do loop:
%let dsname = csv_files;
%let libto = l;
%Macro get_data(n,index);
%let dsname = csv_files;
%let libto = l;
%do i=&n %to &index;
data _NULL_;
set &libto..&dsname end=last;
if _N_ =&i then Do;
call symput('path',path); /* Get file path from CSV_files*/
call symput('dsn',name); /* Get dataset name from CSV_files*/
if not last then index+1; /* Create macro variable Index */
else if last then call symput ('index',index);
End;
run;
proc import datafile= "&path"
out= &libto..&dsn
dbms=dlm
replace;
delimiter="," ;
getnames=yes;
run;
%End;
%End;
%Mend get_data;
/* Macro Call*/
%get_data(1,4);
The errors that I get is the following:
ERROR 180-322: Statement is not valid or it is used out of proper order.
I get this for multiple lines in the code but generates the last 3 out of 4 SAS datasets from my CSV files.
I also get the error for the &dsn in the code:
ERROR 22-322: Syntax error, expecting one of the following: ;, (, DATAFILE, DATATABLE, DBMS,
DEBUG, FILE, OUT, REPLACE, TABLE, _DEBUG_.