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 |
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.
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 112data 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
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
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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.