This code executes perfectly and creates the string "%let year_hyphen_month = 2021-03; %read_txt()" in the mcalls file.
filename mcalls catalog 'work.mycatalog.mcalls.source' ;
data _null_;
input ;
file mcalls ;
put _infile_ ;
datalines4 ;
%let year_hyphen_month = 2021-03; %read_txt()
;;;;
However, when I try to run it inside a macro and substitute the hard-coded year-month text (here "2021-03") it fails.
%macro get_data(yrmo);
filename mcalls catalog 'work.mycatalog.mcalls.source' ;
data _null_;
input ;
file mcalls ;
put _infile_ ;
datalines4 ;
%let year_hyphen_month = &yrmo; %read_txt()
;;;;
%exit: %mend get_data;
%get_data(2021-03)
ERROR: The macro GET_DATA generated CARDS (data lines) for the DATA step, which could cause incorrect results. The DATA step
and the macro will stop executing.
NOTE: 0 records were written to the file MCALLS.
NOTE: DATA statement used (Total process time):
real time 0.02 seconds
cpu time 0.00 seconds
ERROR: The macro GET_DATA will stop executing.
I believe there may actually be several issues, starting with use of datalines inside a macro.
I tried substituting input for datalines, but get a new error
164 filename mcalls catalog 'work.mycatalog.mcalls.source' ;
165 data _null_;
166 input ;
167 file mcalls ;
168 put _infile_ ;
169 input ;
170 %let year_hyphen_month = 2021-03; %read_txt()
171 ;;;;
172
173 %exit: %mend get_data;
174
175 %get_data(2021-03)
NOTE: Line generated by the invoked macro "GET_DATA".
3 %read_txt() ;;;;
-
180
WARNING: Apparent invocation of macro READ_TXT not resolved.
ERROR 180-322: Statement is not valid or it is used out of proper order.
Thanks
First of all, you are correct, DATALINES can not be used inside a macro, and macro triggers do not work in DATALINES blocks.
To create a variable code line, use a simple DATA step without DATALINES:
%macro get_data(yrmo);
filename mcalls catalog 'work.mycatalog.mcalls.source';
data _null_;
file mcalls ;
length string $100;
string = '%let year_hyphen_month = ' !! "&yrmo" !! '; %read_txt()');
put string;
run;
%mend get_data;
First of all, you are correct, DATALINES can not be used inside a macro, and macro triggers do not work in DATALINES blocks.
To create a variable code line, use a simple DATA step without DATALINES:
%macro get_data(yrmo);
filename mcalls catalog 'work.mycatalog.mcalls.source';
data _null_;
file mcalls ;
length string $100;
string = '%let year_hyphen_month = ' !! "&yrmo" !! '; %read_txt()');
put string;
run;
%mend get_data;
Thanks Kurt (after I fixed the extra ")"
Sorry, that extra ) was from a first try using a concatenating function.
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.