DATA Step, Macro, Functions and more

DATALINES statement inside a macro

Reply
N/A
Posts: 0

DATALINES statement inside a macro

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.
SAS Super FREQ
Posts: 8,740

Re: DATALINES statement inside a macro

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
SAS Employee
Posts: 73

Re: DATALINES statement inside a macro

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.
SAS Employee
Posts: 73

Re: DATALINES statement inside a macro

use this code:

%macro monthly;

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

%mend;
%monthly;


regards,
Pavan.
N/A
Posts: 0

Re: DATALINES statement inside a macro

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.
SAS Employee
Posts: 73

Re: DATALINES statement inside a macro

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. Smiley HappySmiley Happy


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
N/A
Posts: 0

Re: DATALINES statement inside a macro

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.
Super Contributor
Posts: 394

Re: DATALINES statement inside a macro

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]
Respected Advisor
Posts: 4,641

Re: DATALINES statement inside a macro

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
Contributor
Posts: 31

Re: DATALINES statement inside a macro

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

New Contributor
Posts: 4

Re: DATALINES statement inside a macro

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

Ask a Question
Discussion stats
  • 10 replies
  • 12622 views
  • 1 like
  • 7 in conversation