BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
texasmfp
Lapis Lazuli | Level 10

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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;

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

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;
texasmfp
Lapis Lazuli | Level 10

Thanks Kurt (after I fixed the extra ")"

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 604 views
  • 0 likes
  • 2 in conversation