DATA Step, Macro, Functions and more

Macro

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

Macro

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 Smiley Very HappyATE. 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 Smiley Very HappyATE. 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

Accepted Solutions
Solution
‎06-21-2017 04:22 AM
Super User
Posts: 7,866

Re: Macro

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
PROC Star
Posts: 768

Re: Macro

[ Edited ]

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

Solution
‎06-21-2017 04:22 AM
Super User
Posts: 7,866

Re: Macro

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 19,878

Re: Macro

Posted in reply to KurtBremser

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

Super User
Posts: 7,866

Re: Macro


Reeza wrote:

In addition to what everyone else has stated - double quotes are needed on the path. 

 



Good catch. Totally overlooked that.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
☑ This topic is solved.

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

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