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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.