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

Reply
Contributor
Posts: 43

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

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

Respected Advisor
Posts: 4,923

Re: 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

Posted in reply to JVarghese

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
Contributor
Posts: 43

Re: 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

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?

Respected Advisor
Posts: 4,923

Re: 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

Posted in reply to JVarghese

Posting your code will more likely get you pertinent answers. - PG

PG
Super User
Super User
Posts: 7,048

Re: 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

Posted in reply to JVarghese

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.

Contributor
Posts: 43

Re: 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

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

Super User
Super User
Posts: 7,048

Re: 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

Posted in reply to JVarghese

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.

Contributor
Posts: 43

Re: 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

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.

Super User
Super User
Posts: 7,048

Re: 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

Posted in reply to JVarghese

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.

Contributor
Posts: 43

Re: 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

Thats right. I got it. Thank you very much for your time writing for me.

Valued Guide
Posts: 2,177

Re: 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

Posted in reply to JVarghese

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

Occasional Contributor
Posts: 6

Re: 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

Posted in reply to JVarghese

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;

Contributor
Posts: 43

Re: 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

Posted in reply to Patrick_Tan

sorry, I am not familiar with mod function yet. Thank you for giving the short cut.

Super User
Super User
Posts: 7,048

Re: 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

Posted in reply to JVarghese

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.

Contributor
Posts: 43

Re: 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

please refer my reply to Mr.PGSTAT.

Ask a Question
Discussion stats
  • 14 replies
  • 527 views
  • 8 likes
  • 5 in conversation