I am confused here. Simply the dataset code lines work itself. It does not complain, and the dataset is created.
data _xtemp;
input endrange;
datalines;
-100
-75
-50
-30
-20
-10
0
10
20
30
40
50
60
80
100
125
150
200
250
300
350
400
500
600
;
run;quit;
5709 data _xtemp;
5710 input endrange;
5711 datalines;
NOTE: The data set WORK._XTEMP has 24 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
BUT, if put into a macro, it complains !!! I do need these lines inside a macro .
%macro init_er();
data _xtemp;
input endrange;
datalines;
-100
-75
-50
-30
-20
-10
0
10
20
30
40
50
60
80
100
125
150
200
250
300
350
400
500
600
;
run;quit;
%mend;
%init_er();
5675 %macro init_er();
5676 data _xtemp;
5677 input endrange;
5678 datalines;
5679 -100
5680 -75
5681 -50
5682 -30
5683 -20
5684 -10
5685 0
5686 10
5687 20
5688 30
5689 40
5690 50
5691 60
5692 80
5693 100
5694 125
5695 150
5696 200
5697 250
5698 300
5699 350
5700 400
5701 500
5702 600
5703 ;
5704 run;quit;
5705
5706 %mend;
5707
5708 %init_er();
MLOGIC(INIT_ER): Beginning execution.
MPRINT(INIT_ER): data _xtemp;
MPRINT(INIT_ER): input endrange;
MPRINT(INIT_ER): datalines;
ERROR: The macro INIT_ER generated CARDS (data lines) for the DATA step, which could cause incorrect results. The DATA step and the macro
will stop executing.
NOTE: The data set WORK._XTEMP has 0 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
MPRINT(INIT_ER): - -75 -50 -30 -20 -10 0 10 20 30 40 50 60 80 100 125 150 200 250 300 350 400 500 600 ;
NOTE: Line generated by the invoked macro "INIT_ER".
1 -100 -75 -50 -30 -20 -10 0 10 20 30 40
----
180
1 ! 50 60 80 100 125 150 200 250 300 350 400 500 600 ; run
ERROR 180-322: Statement is not valid or it is used out of proper order.
MPRINT(INIT_ER): run;
MPRINT(INIT_ER): ;
MPRINT(INIT_ER): ;
MPRINT(INIT_ER): ;
ERROR: The macro INIT_ER will stop executing.
MLOGIC(INIT_ER): Ending execution.
You can't use DATALINES; or CARDS; inside a macro.
If you want a loop in a macro, maybe something like this works for you (UNTESTED CODE)
%macro dothis;
%let endranges = -100 -75 -50 -30 -20; /* I'm lazy, you can type the rest */
%do i=1 %to %sysfunc(countw(&endranges,%str( )));
%let this_endrange=%scan(&endranges,&i,%str( ));
/* Put the code here to perform whatever tasks you want on macro variable &this_endrange */
%end;
%mend;
%dothis
I DO need the code lines, to generate the simple dataset, inside a macro.
The dataset has a single column, ENDRANGE. I am to run a loop to collect the impact from endrange on experiments. The endranges will go though excluasion by criteria from other macro variables. So each time the dataset need re-created at the start[then go exclusion, ...].
You can't use DATALINES; or CARDS; inside a macro.
If you want a loop in a macro, maybe something like this works for you (UNTESTED CODE)
%macro dothis;
%let endranges = -100 -75 -50 -30 -20; /* I'm lazy, you can type the rest */
%do i=1 %to %sysfunc(countw(&endranges,%str( )));
%let this_endrange=%scan(&endranges,&i,%str( ));
/* Put the code here to perform whatever tasks you want on macro variable &this_endrange */
%end;
%mend;
%dothis
@hellohere wrote:
...
The endranges will go though excluasion by criteria from other macro variables.
So you do have a rule to create the datalines. Just put this rule into data step code.
Yes. You are unable to include CARDS or DATALINES statement in a macro. This limited thing has been mentioned in sas documentation. You would either include these data in a file or hard code these data like:
%macro init_er(); data _xtemp; endrange=-100 ; output; endrange=-75 ; output; endrange=-50 ; output; ; run; %mend; %init_er();
This usage note explains the issue:
https://support.sas.com/kb/43/902.html
With Notepad++ you can modify your code a bit:
%macro init_er();
data _xtemp;
endrange = -100; output;
endrange = -75 ; output;
endrange = -50 ; output;
endrange = -30 ; output;
endrange = -20 ; output;
endrange = -10 ; output;
endrange = 0 ; output;
endrange = 10 ; output;
endrange = 20 ; output;
endrange = 30 ; output;
endrange = 40 ; output;
endrange = 50 ; output;
endrange = 60 ; output;
endrange = 80 ; output;
endrange = 100 ; output;
endrange = 125 ; output;
endrange = 150 ; output;
endrange = 200 ; output;
endrange = 250 ; output;
endrange = 300 ; output;
endrange = 350 ; output;
endrange = 400 ; output;
endrange = 500 ; output;
endrange = 600 ; output;
run;
%mend;
%init_er();
Bart
You cannot use in-line data inside a macro. Once the macro is compiled into the word token the concept of a LINE of data is gone.
But you don't need the datalines, you need the dataset.
So generate the data some other way.
data _xtemp;
do endrange
=-100
,-75
,-50
,-30
,-20
,-10
,0
,10
,20
,30
,40
,50
,60
,80
,100
,125
,150
,200
,250
,300
,350
,400
,500
,600
;
output;
end;
run;
You could also use the TO and BY keywords to save some typing.
data _xtemp;
do endrange=-100,-75,-50
,-30 to 60 by 10
,80,100,125
,150 to 400 by 50
,500,600
;
output;
end;
run;
Thanks all. Everyone gave the answer.
I found an detour: create the initial dataset somewhere else, and just copy it at the start of iteration.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.