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-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
  • 1 reply
  • 610 views
  • 0 likes
  • 2 in conversation