BookmarkSubscribeRSS Feed
vishalrajpoot3
Obsidian | Level 7

 

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. 

 

 

 

11 REPLIES 11
Reeza
Super User

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. 

 

 

 


 

vishalrajpoot3
Obsidian | Level 7

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. 

 

Reeza
Super User

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

 

vishalrajpoot3
Obsidian | Level 7

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

ballardw
Super User

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

 

novinosrin
Tourmaline | Level 20
data want;
do _n_=5 to nobs by 5;
set sashelp.class point=_n_ nobs=nobs;
put _all_;
output;
end;
stop;
run;
Andygray
Quartz | Level 8

@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

vishalrajpoot3
Obsidian | Level 7

 

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. 

 

Reeza
Super User

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. 

 

 

vishalrajpoot3
Obsidian | Level 7

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.

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 11 replies
  • 2262 views
  • 4 likes
  • 5 in conversation