0

Assume I have this program:

1 data temp;
2   set _null_;
3 run;
4 
5 %put Hello world;

and I want to add two lines to it, one that runs lines 1-3 of the program, and another that runs line 5.

The second example here suggests that %include may be what I'm looking for, but %include 1-3 and %include 5 do not work. %include [path] 1-3 gets me into an infinite loop, which is undesirable.

What is the best way to accomplish this? Thanks!

2
  • %include generally references an external file. I'm not sure there's a way to do what you're trying to do. Can you explain the use case? Commented Mar 28, 2016 at 16:02
  • There is a large chunk of code that I need to re-run several times and was looking for a easier way of running it than highlighting everything (loops do not work in this scenario). I can imagine this having other uses as well, but that was my intention. Commented Mar 29, 2016 at 16:50

3 Answers 3

3

EDIT: this only works for lines that have previously been submitted.

You need SPOOL option. I used RESETLINE statement to reset the line number useful when using SAS/EG. I would like to know how you intend to use it.

options spool=1;
resetline;
data temp;
   set _null_;
   run;

%put Hello world;

%include 1-3 / source2;
%include 5 / source2;

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

resetline statement - nice!
Ah okay, so maybe this is only a SAS EG capability. Any idea if something can be worked out for base SAS?
@superfluous It is base SAS running in batch. EG is just a submitter. The reason I mentioned EG is because it submits lines of code which is why I did the resetline.
0

Macros, perhaps?

%macro one(datasetname= );
data &datasetname;
set _null_;
run;
%mend one;

%macro two(textstring= ); 
%put &textstring;
%mend two;

%one(datasetname= temp1);
%two(textstring= Hello world);

%one(datasetname= temp2);
%two(textstring= Hello new world);

You could feed macro variables into the process from a dataset rather than multiple macro calls. See the examples starting on p 11 here: First & Ronk, SGI 130-30, SAS® Macro Variables and Simple Macro Programs

Comments

0

A single extra return could screw up your line references...If you need to add a header, or a new line of code somewhere or something are you willing to go back and fix all your line number references?

I recommend using the macro or %include options.

%macro repeat_code();
     ***sas code goes here;

%mend;

%repeat_code

For the %include you can create the lines inside a new file and then reference them in your code with a %include.

Depending on what those lines are actually doing, you may have other options. For example if it's a lookup or re-coding a variable I would use a format instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.