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.
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!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: