BookmarkSubscribeRSS Feed
JVarghese
Obsidian | Level 7

Why STOP statement cannot be put inside an iterative do loop when used with a POINT= option in SET statement? Ex: While reading every 10th obs to a new data set...

14 REPLIES 14
PGStats
Opal | Level 21

STOP statement CAN be used with SET POINT= Example:

data test;

do i = 1 to 10;

  set sashelp.class point=i;

  output;

  if i = 5 then stop;

  end;

run;

proc print data=test noobs; run;

Dataset test has 5 obs, not 10.

Please post the troublesome code.

PG

PG
JVarghese
Obsidian | Level 7

data test;

do i = 1 to 10 by 2;

       set sashelp.class point=i;

       output;

end;

stop;

run;

can you tell me what happens to the dataset test with the above code and what if i put STOP right after OUTPUT there?

Tom
Super User Tom
Super User

That code with read the 1st,3rd,5th,7th and 9th observation from SASHELP.CLASS and output them to TEST.

If you move the STOP to after the OUTPUT then it will stop after doing that for just the first observation. So you get just one observation.

If you let your point variable range past the end of the input dataset then it will continue to output the last record that it successfully read.

JVarghese
Obsidian | Level 7

when a SET statement is put inside a do loop, Would SET loose its default behaviour ? or is it only controlled by the do loop, for writing the output? And I was just wondering, why even after reaching the end of the input dataset it should loop again continuously if we skip the STOP statement.. Is this also for the same reason that the SET statement was inside do loop for it blocked the communication b/w DATA and SET statements.?

Tom
Super User Tom
Super User

Placing it inside of a DO loop does NOT change how a SET statement works, but it will make a big difference in how the implicit OUTPUT at the end of the data step works.  For example try this one:

data even ;

do i=1 to 2; set sashelp.class; end;

run;

To see better how it is that a normal simple data step works put in some PUT statements.

data _null_;

  put 'Before ' _n_= ;

  set sashelp.class ;

  put 'After ' _n_ = ;

run;

Notice that the data step dies before the second PUT statement on the 20th loop because the SET statement tried to read past the end of the input.

JVarghese
Obsidian | Level 7

please run this code;

data test;

  put 'Before ' _n_= ;

do i=1 to 19 by 2;

put 'Before ' _n_= ;

set sashelp.class point=i;

output;

  put 'After ' _n_ = ;

  end;

  stop;

  run;

My question: As long as the SET statement is within do loop iterations, the data step is being controlled by DO ? Coz, I couldn,t see the _N_ value getting added up...but at the same time the expected dataset was created successfully.

Tom
Super User Tom
Super User

The value the automatic variable _N_ has nothing to do with the SET statement.

It can never be larger than 1 in your example because the data step code will never execute more than once.

Peter_C
Rhodochrosite | Level 12

Placing STOP within a conditional block will be a problem whenever conditions do NOT permit the block with STOP to execute

Patrick_Tan
Fluorite | Level 6

I wouldn't use the stop statement but the MOD function with _n_ .

Example:

data test;

   set sashelp.prdsale;

   if mod(_n_ , 10) eq 0;

run;

Tom
Super User Tom
Super User

The normal way that SAS stops a data step is when you read past the input when executing either a SET or INPUT statement.

If you are using point= option the SET statement will never read past the end of the data set.

So you need to manually tell it when to stop or else it will just continue to loop through the data step code.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of 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
  • 14 replies
  • 2544 views
  • 8 likes
  • 5 in conversation