DATA Step, Macro, Functions and more

simple code not working in macro

Reply
Contributor
Posts: 24

simple code not working in macro

Hi all,

The piece of code works if not in macro. But I got errors when put it in a macro. The code is as following:

%MACRO RDATA;

DATA LIST;
INPUT X Y A $ Z;
DATALINES;
1 2 HELLO 3
4 5 GOODBYE 6
;
PROC PRINT DATA=LIST;
TITLE 'LIST INPUT';
RUN;

%MEND RDATA;

%RDATA;


Thanks in advance
SAS Super FREQ
Posts: 8,864

Re: simple code not working in macro

Hi:
As it explains in this Tech Support note;
http://support.sas.com/kb/40/491.html

"A macro definition cannot contain a CARDS statement, a DATALINES statement, or a PARMCARDS statement because the text of the SAS statements is stored as a text instruction in an object in a macro catalog. The position of the data lines are not preserved and might produce incorrect results. Therefore, the Macro facility is not designed to accept the CARDS, DATALINES, or PARMCARDS statements."


There is more in the note, including one possible workaround. Another workaround is to store your data in a TEXT file and then use the file name in your INFILE statement in the program that is stored in the macro definition. You can always define the name of the file as a macro parameter, so it is possible to change the file from one run of the macro program to another. Your program would need to change a bit to use an INFILE statement and you might want to consider defining the &FILENAME macro variable as a macro parameter. Also, if the character variable, A, could be longer than 8 characters, I'd probably put a LENGTH statement in the program. Also, you might need other options on your INFILE statement, depending on how you create the file c:\temp\mydata.txt -- but a general program idea is shown below -- without using DATALINES. The file c:\temp\mydata.txt contains the 2 data lines as shown in your original post.

cynthia
[pre]
%MACRO RDATA;

DATA LIST;
INFILE "&filename";
INPUT X Y A $ Z;
run;

PROC PRINT DATA=LIST;
TITLE 'LIST INPUT';
RUN;

%MEND RDATA;

ods listing;
%let filename = c:\temp\mydata.txt;
%RDATA;
[/pre]
Ask a Question
Discussion stats
  • 1 reply
  • 116 views
  • 0 likes
  • 2 in conversation