BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BrahmanandaRao
Lapis Lazuli | Level 10
Obs_Sno	Name	Sex	Age	Height	Weight
15	Philip	M	16	72	150
16	Robert	M	12	64.8	128
17	Ronald	M	15	67	133
18	Thomas	M	11	57.5	85
19	William	M	15	66.5	112
data one ; do obs_sno=1 to nobs; set sashelp.class nobs=nobs; if _n_>=nobs-4 ; output; end; run;

 REQUIRED OUTPUT

Obs_SnoNameSexAgeHeightWeight
15PhilipM1672150
16RobertM1264.8128
17RonaldM1567133
18ThomasM1157.585
19WilliamM1566.5112
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User
data want;
   set sashelp.class nobs=totalobs;
   obs_no = _n_;
   if _n_ ge totalobs - 4;
run;

You have to use the NOBS= option on the Set statement to create a variable that holds the number of observations in the input data set. Then you also have to explicitly assign the _n_ value to another variable to keep the observations number if you want that value.

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

And what is your question?

 


@BrahmanandaRao wrote:
Obs_Sno	Name	Sex	Age	Height	Weight
15	Philip	M	16	72	150
16	Robert	M	12	64.8	128
17	Ronald	M	15	67	133
18	Thomas	M	11	57.5	85
19	William	M	15	66.5	112
data one ; do obs_sno=1 to nobs; set sashelp.class nobs=nobs; if _n_>=nobs-4 ; output; end; run;

 REQUIRED OUTPUT

Obs_Sno Name Sex Age Height Weight
15 Philip M 16 72 150
16 Robert M 12 64.8 128
17 Ronald M 15 67 133
18 Thomas M 11 57.5 85
19 William M 15 66.5 112

 

BrahmanandaRao
Lapis Lazuli | Level 10

i want  below  required output

Obs_Sno Name Sex Age Height Weight
15 Philip M 16 72 150
16 Robert M 12 64.8 128
17 Ronald M 15 67 133
18 Thomas M 11 57.5 85
19 William M 15 66.5 112

ballardw
Super User
data want;
   set sashelp.class nobs=totalobs;
   obs_no = _n_;
   if _n_ ge totalobs - 4;
run;

You have to use the NOBS= option on the Set statement to create a variable that holds the number of observations in the input data set. Then you also have to explicitly assign the _n_ value to another variable to keep the observations number if you want that value.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 889 views
  • 1 like
  • 3 in conversation