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...
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
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?
Posting your code will more likely get you pertinent answers. - PG
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.
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.?
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.
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.
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.
Thats right. I got it. Thank you very much for your time writing for me.
Placing STOP within a conditional block will be a problem whenever conditions do NOT permit the block with STOP to execute
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;
sorry, I am not familiar with mod function yet. Thank you for giving the short cut.
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.
please refer my reply to Mr.PGSTAT.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.