BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Can anybody tell me why the following macro errors out:

%macro monthly;
data lobs;
infile datalines;
input LOB $ STATE $;
datalines;
IMR IL
IMD IL
MMD MO
MMR MO
HMR IN
OMD OH
OAD OH
;
run;
%mend;
%monthly;

If the data step is run outside the macro it runs fine, once inside it errors. Is there a solution to this different from pulling the step out of the macro.

Thank you.
1 ACCEPTED SOLUTION

Accepted Solutions
Pavan_SAS
SAS Employee
A macro definition must precede the invocation of that macro in your code. The %MACRO statement can appear anywhere in a SAS program, except within data lines. A macro definition cannot contain a CARDS statement, a DATALINES statement, a PARMCARDS statement, or data lines. Use an INFILE statement instead.



regards,
pavan.

View solution in original post

10 REPLIES 10
Cynthia_sas
SAS Super FREQ
Hi:
I can't find the exact reference in the doc, but the error message is quite clear:
[pre]
ERROR: The macro MONTHLY generated CARDS (data lines) for the DATA step, which could cause incorrect results.
The DATA step and the macro will stop executing.
[/pre]


You could switch to a permanent flat file with your LOB and state info. You could use a user-defined format for this information.
I don't understand why the read for LOBS data file is in the macro program ...does it change? If so, how were you going to update the section following the datalines. If the info is not going to change, then that's where I'd recommend just using a permanent text file or a permanent SAS dataset or a permanent user-defined format.

cynthia
Pavan_SAS
SAS Employee
A macro definition must precede the invocation of that macro in your code. The %MACRO statement can appear anywhere in a SAS program, except within data lines. A macro definition cannot contain a CARDS statement, a DATALINES statement, a PARMCARDS statement, or data lines. Use an INFILE statement instead.



regards,
pavan.
Pavan_SAS
SAS Employee
use this code:

%macro monthly;

data lobs;
infile "C:\Documents and Settings\xisguest\Desktop\lobs.txt";
input LOB $ STATE $;
run;

%mend;
%monthly;


regards,
Pavan.
deleted_user
Not applicable
hi pavan,
have you checked the code submitted by u?
people are talking something called macro,for your information,you can not make a macro simply by putting %macro.
i am sorry to tell u this you have to learn the basics of macro once again.
Pavan_SAS
SAS Employee
hi whyel,

i checked my code twice, before posting in forum.
you are telling that "you can not make a macro simply by putting %macro."
Then tell me, how we can make a macro?
Plz explain.
I want to learn that thing from you !

we can use any macro variables also in a macro depending on our requirement:

%macro monthly(table=);

data &table;
infile "C:\Documents and Settings\xisguest\Desktop\lobs.txt";
input LOB $ STATE $;
run;

%mend;

%monthly(table=lobs);

anyway, plz execute that code on your computer and reply me if there are any errors.
plz check everything, before commenting anyone. :):)


i know macros upto some extend.
i think, i need not learn basics of macros once again !!!!!!!!!

what i posted is the solution for AOrtega's problem.


i got this log, after executing my code with sas9.2


1 %macro monthly;
2
3 data lobs;
4 infile "C:\Documents and Settings\xisguest\Desktop\lobs.txt";
5 input LOB $ STATE $;
6 run;
7
8 %mend;
9
10 %monthly;

NOTE: The infile "C:\Documents and Settings\xisguest\Desktop\lobs.txt" is:
File Name=C:\Documents and Settings\xisguest\Desktop\lobs.txt,
RECFM=V,LRECL=256

NOTE: 7 records were read from the infile "C:\Documents and Settings\xisguest\Desktop\lobs.txt".
The minimum record length was 6.
The maximum record length was 6.
NOTE: The data set WORK.LOBS has 7 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.04 seconds
cpu time 0.01 seconds
deleted_user
Not applicable
Thank you all for your answers.
I was using the data step in a macro because the little data set needed to be created only if a condition was true, I do not need the data set otherwise. I do not see how a format will work on this case because I need to read every one of those lines and generate data form them. The external file is my only option and I did not wanted to go that way because I don't control the production environment and to deploy a simple file is a big head ache but it looks like that's my only option.
Tim_SAS
Barite | Level 11
Instead of an external file, why not do this instead?
[pre]
%macro monthly;
data lobs;
array lobs{7} $3 _TEMPORARY_ ('IMR', 'IMD', 'MMD', 'MMR', 'HMR', 'OMD', 'OAD' );
array states{7} $2 _TEMPORARY_ ('IL', 'IL', 'MO', 'MO', 'IN', 'OH', 'OH' );
drop n;

do n = 1 to 7;
lob = lobs{n};
state = states{n};
output;
end;
run;
%mend;
%monthly;
[/pre]
PGStats
Opal | Level 21

Or alternatively, put this inside your macro:

data test(drop=s);

length lob $3 state $2;

do s = 'IMR IL', 'IMD IL', 'MMD MO', 'MMR MO', 'HMR IN', 'OMD OH', 'OAD OH';

    lob = scan(s,1); state = scan(s,2);

    output;

    end;

run;

PG

PG
loredana_cornea
Obsidian | Level 7

Hi there,

* you write in a new sas file (let's say "data.sas") just your data step;

* then in your main sas file, where you make all manipulations you define a macro, for example;

     %macro example();

          %if (your condition in macro language) %then %INCLUDE "C:\...\...\data.sas";

     %mend();

     %example();

* it should work. i have tried it myself. of course nothing happens it the condition is not met.

* my full tryout code is:

data a;

  input abc;

  datalines;

  1

  2

  3

;run;

proc sql noprint;

  select abc into :abc separated by ' ' from a;

quit;

%put &abc;

%macro example();

%if &abc<=3 %then %INCLUDE "U:\Diane\BODACC\proc_coll_me.sas";

%mend();

%example();

Let me know if it works

mark_alexander_ct_gov
Fluorite | Level 6

Here is another option that works within a macro:

 

213 %macro monthly;

214 proc sql;

215   create table Lobs (Lob char(3), State char(2));

216   insert into Lobs

217     values ('IMR','IL')

218     values ('IMD','IL')

219     values ('MMD','MO')

220     values ('MMR','MO')

221     values ('HMR','IN')

222     values ('OMD','OH')

223     values ('OAD','OH');

224 quit;

225 %mend;

226 %monthly

NOTE: Table WORK.LOBS created, with 0 rows and 2 columns.

NOTE: 7 rows were inserted into WORK.LOBS.

NOTE: PROCEDURE SQL used (Total process time):

real time 0.00 seconds

cpu time 0.00 seconds

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 10 replies
  • 43394 views
  • 6 likes
  • 7 in conversation