DATA Step, Macro, Functions and more

I want every 5th observation value in output using data step.

Reply
Occasional Contributor
Posts: 9

I want every 5th observation value in output using data step.

 

Hello There,

 

Will anyone please help me to get the solution for this problem,

 

From sashelp.class data set I want 5th 10th and 15th observation (or observations in multiple of 5) in output and in Log window it should read only the given output. 

 

 

 

Super User
Posts: 23,774

Re: I want every 5th observation value in output using data step.

Posted in reply to vishalrajpoot3

Look up the MOD() function. 

 


@vishalrajpoot3 wrote:

 

Hello There,

 

Will anyone please help me to get the solution for this problem,

 

From sashelp.class data set I want 5th 10th and 15th observation (or observations in multiple of 5) in output and in Log window it should read only the given output. 

 

 

 


 

Occasional Contributor
Posts: 9

Re: I want every 5th observation value in output using data step.

I think using mod() will give me remainder. Could you please help to advise how we can use to get the every 5th observation in output. 

 

Super User
Posts: 23,774

Re: I want every 5th observation value in output using data step.

Posted in reply to vishalrajpoot3

@vishalrajpoot3 wrote:

I think using mod() will give me remainder. Could you please help to advise how we can use to get the every 5th observation in output. 

 


Yes it does. And if you divide by 5 what's the remainder?

 

Occasional Contributor
Posts: 9

Re: I want every 5th observation value in output using data step.

it will give 0(zero), but this was not the question. 

Super User
Posts: 23,774

Re: I want every 5th observation value in output using data step.

Posted in reply to vishalrajpoot3
Then what is the question?
Super User
Posts: 13,583

Re: I want every 5th observation value in output using data step.

Posted in reply to vishalrajpoot3

@vishalrajpoot3 wrote:

I think using mod() will give me remainder. Could you please help to advise how we can use to get the every 5th observation in output. 

 


data want;
   set sashelp.class;
   if mod(_n_,5)=0;
   line=_n_;
run;

Line to show which line the record comes from.

 

PROC Star
Posts: 1,836

Re: I want every 5th observation value in output using data step.

[ Edited ]
Posted in reply to vishalrajpoot3
data want;
do _n_=5 to nobs by 5;
set sashelp.class point=_n_ nobs=nobs;
put _all_;
output;
end;
stop;
run;
Frequent Contributor
Posts: 75

Re: I want every 5th observation value in output using data step.

Posted in reply to vishalrajpoot3

@Reeza and @novinosrin both have provided you with answers. Welcome to SAS forum as it seems you are new.

 

First off, it seems you are unable to apply the mod function with a condition. This is where quantitative skills  are imperative to be successful in any programming. Try to think through why Reeza suggested mod, if mod results in zero for every fifth obs and not in zero for others. What do you have to do next?

 

With some effort, you will get the hang of it, but you will have to do that thinking by yourself since the directions have been provided

Occasional Contributor
Posts: 9

Re: I want every 5th observation value in output using data step.

 

Thanks Everyone.

 

Yes, I am new in any of the programming, SAS is the very first platform to write code. 

 

Let me try that answer first. 

 

Super User
Posts: 23,774

Re: I want every 5th observation value in output using data step.

Posted in reply to vishalrajpoot3

This is a contrived problem to try and learn with. I wouldn't recommend starting here...I've only seen this type of question in practice tests or interview questions and it's rarely used in practice. 

 

If you're trying to learn the first SAS Programming e-course is free, as well as the tutorials on UCLA which are quite helpful. 

 

 

Occasional Contributor
Posts: 9

Re: I want every 5th observation value in output using data step.

Thank you everyone for providing you valuable inputs to solve the subjected problem.

 

 

Here is one more solution -->

 

 

data abc;
num + 5;
set sashelp.class nobs = k point = num;
if num ge k then stop;
output;
run;

 

 

Note - This code will allow pointer to jump to every 5th observation and will save the processing time in case of huge data set.

 

Ask a Question
Discussion stats
  • 11 replies
  • 135 views
  • 4 likes
  • 5 in conversation