BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Selli5
Fluorite | Level 6

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

 

1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
61
62
63 %macro onetime;
64 %local i;
65 %do i=1 %to 38 %by 1;
66
67
68
69 DATA temp_&i;
70 INFILE '/folders/myfolders/sasuser.v94/tran.&i_28MAY17.csv' DSD delimiter=',' missover firstobs=2;
71 informat Date Date. Credit 8.2 Debit 8.2;
72 FORMAT Date :DATE. Credit dollar.0 Debit dollar.0;
73 INPUT
74 Account:$8.
75 Tran_Code :$4.
76 Date
77 Credit
78 Debit
79 Desc :$100.
80 ;
81 RUN;
82 end;
83 %mend onetime;
ERROR: There were 1 unclosed %DO statements. The macro ONETIME will not be compiled.
ERROR: A dummy macro will be compiled.
84 %onetime;
_
180
WARNING: Apparent invocation of macro ONETIME not resolved.
 
ERROR 180-322: Statement is not valid or it is used out of proper order.
 
85
86 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 
How do I solve this?
thanks
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

You are missing an % in front of your end. It should be %end below your RUN statement.

Kurt_Bremser
Super User

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.

Reeza
Super User

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

 

https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-write-a-macro-to-import-multiple-tex...

Kurt_Bremser
Super User

SAS Innovate 2025: Register Now

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1131 views
  • 3 likes
  • 4 in conversation