BookmarkSubscribeRSS Feed
buckeye
Obsidian | Level 7
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
1 REPLY 1
Cynthia_sas
SAS Super FREQ
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]

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 764 views
  • 0 likes
  • 2 in conversation