Hello,
I am trying to import csv files named Tran.1_28May17 upto Tran.37_28May17 within a macro:
%macro onetime;
%local i;
%do i=1 %to 37 %by 1;
DATA temp_&i;
INFILE '/folders/myfolders/sasuser.v94/tran.&i_28MAY17.csv' DSD delimiter=',' missover firstobs=2;
informat Date Date. Credit 8.2 Debit 8.2;
FORMAT Date :DATE. Credit dollar.0 Debit dollar.0;
INPUT
Account :$8.
Tran_Code :$4.
Date
Credit
Debit
Desc :$100.
;
RUN;
end;
%mend onetime;
%onetime;
However get the following error message...
This:
ERROR: There were 1 unclosed %DO statements.
says it all. Any %do needs a corresponding %end.
%macro onetime;
%local i;
%do i=1 %to 37 %by 1;
data temp_&i;
infile
'/folders/myfolders/sasuser.v94/tran.&i_28MAY17.csv'
dsd
delimiter=','
missover
firstobs=2
;
informat
Date Date.
Credit 8.2
Debit 8.2
;
format
Date date.
Credit dollar.0
Debit dollar.0
;
input
Account :$8.
Tran_Code :$4.
Date
Credit
Debit
Desc :$100.
;
run;
%end; /* error was here, was "end" without macro trigger % */
%mend onetime;
%onetime;
And note how a little visual formatting makes the code more readable (Maxim 12).
Now, if you restructure your filenames to something that makes hierarchical sense:
tran_2017-05-28_01.csv tran_2017-05-28_02.csv .... tran_2017-05-28_37.csv
you could avoid all the hassle and use wildcards in a single data step (Maxim 33), removing the need for macro programming.
You are missing an % in front of your end. It should be %end below your RUN statement.
This:
ERROR: There were 1 unclosed %DO statements.
says it all. Any %do needs a corresponding %end.
%macro onetime;
%local i;
%do i=1 %to 37 %by 1;
data temp_&i;
infile
'/folders/myfolders/sasuser.v94/tran.&i_28MAY17.csv'
dsd
delimiter=','
missover
firstobs=2
;
informat
Date Date.
Credit 8.2
Debit 8.2
;
format
Date date.
Credit dollar.0
Debit dollar.0
;
input
Account :$8.
Tran_Code :$4.
Date
Credit
Debit
Desc :$100.
;
run;
%end; /* error was here, was "end" without macro trigger % */
%mend onetime;
%onetime;
And note how a little visual formatting makes the code more readable (Maxim 12).
Now, if you restructure your filenames to something that makes hierarchical sense:
tran_2017-05-28_01.csv tran_2017-05-28_02.csv .... tran_2017-05-28_37.csv
you could avoid all the hassle and use wildcards in a single data step (Maxim 33), removing the need for macro programming.
In addition to what everyone else has stated - double quotes are needed on the path.
And just a note, you don't need a macro to import multiple text files.
You can use the approach here or use the filevar to specify the file name
@Reeza wrote:
In addition to what everyone else has stated - double quotes are needed on the path.
Good catch. Totally overlooked that.
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!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.