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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 469 views
  • 1 like
  • 3 in conversation