BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
hellohere
Pyrite | Level 9

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.

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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

 

 

--
Paige Miller

View solution in original post

9 REPLIES 9
hellohere
Pyrite | Level 9

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, ...].

 

PaigeMiller
Diamond | Level 26

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

 

 

--
Paige Miller
Kurt_Bremser
Super User

@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.

Ksharp
Super User

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();
yabwon
Amethyst | Level 16

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Tom
Super User Tom
Super User

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;
hellohere
Pyrite | Level 9

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. 

 

%macro init_er();
data _xtemp;
set trxtb._xtemp;
run;quit;
 
%mend;
 
%init_er();

 

yabwon
Amethyst | Level 16

If you're doing it this way, do it smarter:

%macro init_er();

%if NOT %sysfunc(exist(trxtb._xtemp)) %then
  %do;
    %put ERROR: trxtb._xtemp does NOT exist! Exiting.;
    %return;
  %end;

data _xtemp;
set trxtb._xtemp;
run;quit;
 
%mend;
 
%init_er()

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



PaigeMiller
Diamond | Level 26

Why so complicated?

 

Why create a temporary data set named _XTEMP?


Why not just use the data set TRXTB._XTEMP in the macro?

--
Paige Miller

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

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
  • 9 replies
  • 203 views
  • 2 likes
  • 6 in conversation