DATA Step, Macro, Functions and more

Do loops

Reply
Occasional Contributor
Posts: 7

Do loops

Hi

I’m recently started learning SAS so need help with do loops

A dataset of 1000 observations , use a do loop to output every 100 observations in to new dataset

Thanks
Super User
Posts: 10,600

Re: Do loops

Use the mod() function on the automatic variable _N_ in a subsetting if.

To get more information, do google searches on

SAS mod function

SAS _n_ variable

SAS subsetting if

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 6,935

Re: Do loops

To do this is easy.  But to do it using a DO loop is not as easy, and not really a "beginner's" problem.  For example:

 

data want;

do _n_=100 to _nobs_ by 100;

   set have point=_n_ nobs=_nobs_;

   output;

end;

stop;

run;

 

Accomplishing this without a DO loop is relatively easy and a more appropriate problem:

 

data want;

set have;

if mod(_n_, 100) = 0;

run;

 

Even so, most beginners would find this method challenging.

Occasional Contributor
Posts: 7

Re: Do loops

Thank you
Ask a Question
Discussion stats
  • 3 replies
  • 185 views
  • 0 likes
  • 3 in conversation