BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
braam
Quartz | Level 8

Dear All, I was wondering if I can repeat a certain part of my SAS code, as described below. Is there something like goto statement I can make use of? To be clear, I don't want to make a formal macro code for this, but I want to repeat the MAIN STATEMENTS by using %let statement.

 

MAIN SAS STATEMENTS (including %let var=;) from line 1 to 100

 

%let var= abc;

Run the MAIN STATEMENTS.

 

%let var= def;

Run the MAIN STATEMENTS.

 

%let var= ghi;

Run the MAIN STATEMENTS.

 

So, the code would be less than 110 lines. Hope that there is such a way to implement this. Of course, I now I can use the macro, but then all statements in the macro have the same color, so it's not so convenient to debug. This is perhaps my taste, but I really prefer this way. In addition, I have to revise my code from time to time. This way makes it easier for me. Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Suppose you have a main.sas program like this:

/* some steps */

/* start block 1 */

/* steps you want to selectively run */

/* end block 1 */

/* rest of code */

now run this step:

data _null_;
infile "main.sas";
file "select.sas";
retain flag 0;
input;
if index(_infile_,"start block 1") then flag = 1;
if index(_infile_,"end block 1") then flag = 0;
if flag then put _infile_;
run;

%include "select.sas";

View solution in original post

18 REPLIES 18
SASKiwi
PROC Star

Store your MAIN statements in a separate SAS program called perhaps MAIN.sas then do this:

 

%let var= abc;
%include 'MAIN.sas';

%let var= def;
%include 'MAIN.sas';

%let var= ghi;
%include 'MAIN.sas';
braam
Quartz | Level 8
Thanks. But can it be possible within one SAS code?
Second, do you know how I can let my SAS run just a part of the external code file (rather than the entire code)?
PaigeMiller
Diamond | Level 26

@braam wrote:
Thanks. But can it be possible within one SAS code?


Do you mean you don't want MAIN.SAS as a separate file and then the code with %include in a separate file? Then I don't think this is possible without using macros; or if whatever you have in MAIN.SAS is just purely data step code in a single data step, with no PROCs, then you could do this via the LINK statement, which creates a "subroutine" inside the data step that can be used many times within the same data step.

 

Second, do you know how I can let my SAS run just a part of the external code file (rather than the entire code)?

 

Macros

 

Of course, I now I can use the macro, but then all statements in the macro have the same color, so it's not so convenient to debug. 

 

The advice is ALWAYS to get the code working without macros and without macros variables for one or two situations, then the debugging features of SAS on the base code work fine. Then you can turn it into a macro with much more ease, because you won't have to be debugging the underlying code.

 

 

--
Paige Miller
braam
Quartz | Level 8

 

@PeterClemmensen Thanks. I actually tried code like this. But the thing is that the numbers following %include (4-6 in your example) are the line numbers in the log window, not in the code file, if I understood correctly. I'll try this way a little further. By the way, what do you mean by call execute? Could you give a little more hint?

 

@PaigeMiller You are right. So, users have to be careful with that. Thanks for your answer as well.

 

PeterClemmensen
Tourmaline | Level 20

Just an alternative to creating dynamic code. Like this

 

data _null_;
    do suffix='abc', 'def', 'ghi';
        call execute(cat(
           "data ", suffix, ";
               a=1;
           run;"
        ));
    end;
run;

You can read more in the article CALL EXECUTE made easy for SAS data-driven programming

 

 

braam
Quartz | Level 8

Thanks. The link looks very helpful to me. I'll read this carefully today.

 

Fortunately, I've got a hint from one of the old posts: https://communities.sas.com/t5/General-SAS-Programming/Recalling-only-a-portion-of-data-lines-from-a...

 

 

data _null_;
      infile '"C:\PATH\FILE1.sas"' firstobs= 108 obs=200;
      file '"C:\PATH\FILE2.sas"';
      input;
      put _infile_;
      run;

 

 

Based on what I found in the posting, I made this code, which works for me. In the posting, one mentioned that firstobs option can be replaced by something else more intuitive, user-friendly (A similar issue has already been discussed in this thread).

 

Perhaps I can add "BEGINNING" and "ENDING" tags in my file1 to indicate the first line and the ending line of the part I would like to take in file2. I'm just wondering if you know any way to solve this issue.  Thanks again for your advice.

 

 

Kurt_Bremser
Super User

Add comment lines to your code that mark starts and ends. Then you can skip lines (no put) until a certain start comment is detected, and write lines until the corresponding end comment is found.

braam
Quartz | Level 8
Sorry but, I don't know how to implement what you desribed...(skipping until the start comment is detected...) Could you please give me some hints or example codes?
Kurt_Bremser
Super User

Suppose you have a main.sas program like this:

/* some steps */

/* start block 1 */

/* steps you want to selectively run */

/* end block 1 */

/* rest of code */

now run this step:

data _null_;
infile "main.sas";
file "select.sas";
retain flag 0;
input;
if index(_infile_,"start block 1") then flag = 1;
if index(_infile_,"end block 1") then flag = 0;
if flag then put _infile_;
run;

%include "select.sas";
braam
Quartz | Level 8
That works for me perfectly. Thanks a lot.
Kurt_Bremser
Super User

@braam wrote:
Thanks. But can it be possible within one SAS code?
Second, do you know how I can let my SAS run just a part of the external code file (rather than the entire code)?

This all falls into the realm of "dynamic code", and that's what the macro language is for.

PeterClemmensen
Tourmaline | Level 20

You can not submit specific lines of code from external files using %include. I believe you can do something like this, though I have not tested it

 

options spool;

%let var= abc;
data test_&var.;
    a=1;
run;

%let var=def;
%include 4-6;

%let var=ghi;
%include 4-6;  

 

PaigeMiller
Diamond | Level 26

@PeterClemmensen wrote:

Yout can not submit specific lines of code from external files using %include. I believe you can do something like this, though I have not tested it

 

options spool;

%let var= abc;
data test_&var.;
    a=1;
run;

%let var=def;
%include 4-6;

%let var=ghi;
%include 4-6;

I suppose this works, but it's not very clear code to anyone looking at this, it reminds me of Excel references; and don't ever change the code so that lines 4-6 move up or down in the code, because then you also have to change all of the %include statements. 

--
Paige Miller
PeterClemmensen
Tourmaline | Level 20

@PaigeMiller I agree. A macro or Call execute logic is the answer here, this just popped up in my head since @braam asked if it is possible to execute parts of - and not the entire code from an external .sas file 🙂

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 18 replies
  • 1787 views
  • 1 like
  • 7 in conversation