BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Flowerbud
Calcite | Level 5
Every time I am using proc contents to view total number of obervations in a dataset, then I am using dataset option to create secondary dataset of last 50 observations.
What is the pice of code to make my code dynamic for this everyday task.
1 ACCEPTED SOLUTION

Accepted Solutions
Bala_Saladi
Fluorite | Level 6

Try this code:-

 

data second_dataset;
set sashelp.shoes nobs=the_count;
if _N_ > the_count -50;
run;

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

First find the number of observations in the data set, put it into a macro variable called NOBS.

https://communities.sas.com/t5/SAS-Communities-Library/Determining-the-number-of-observations-in-a-S...

 

Then

 

data want;
     set have(firstobs=%eval(&nobs-49));
run;
--
Paige Miller
Flowerbud
Calcite | Level 5
Thank you for your solution! I am new to SAS software, still learning advanced programming skills on macros.
Tom
Super User Tom
Super User

Note that FIRSTOBS= option expects an integer larger than 0.

So if you would need a little more logic to get it right.

Something like:

%let nobs=19;
%let n=10;
%let firstobs=%sysfunc(max(1,&nobs-&n+1));
%put &=nobs &=n &=firstobs;
data want;
  set sashelp.class(firstobs=&firstobs);
run;
Bala_Saladi
Fluorite | Level 6

Try this code:-

 

data second_dataset;
set sashelp.shoes nobs=the_count;
if _N_ > the_count -50;
run;

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
  • 4 replies
  • 1237 views
  • 3 likes
  • 4 in conversation