Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you print the last observation of a SAS data set?

Tags:

sas

I have a data set with 1000 observations. I want to only print out the last observation. Using the following:

proc print data=apple(firstobs = 1000 obs = 1000); 
run;

I can get the last observation. But I have to know in advance that my data set has 1000 observations. How do I do this without knowing this?

like image 567
NebulousReveal Avatar asked Oct 31 '11 17:10

NebulousReveal


2 Answers

There are many ways you could do this. Here are two:

proc sql noprint;
 select n(var1) into :nobs
 from apple;
quit;

proc print data=apple(firstobs=&nobs); run;

This just reads the number of observations into a macro variable, and then use that to specify the first observation. (Note that var1 refers to a variable in your data.)

Another approach would be to create a data view that only keeps the last observation and then print that:

data tmp / view=tmp;
 set apple nobs=nobs;
 if _n_=nobs;
run;

proc print data=tmp; run;
like image 173
itzy Avatar answered Oct 01 '22 11:10

itzy


I think that the end option for the SET, MERGE, MODIFY, or UPDATE statement is very useful.

data x;
  do i = 1 to 1000;
    output;
  end;
run;

data x;
  set x end = _end;
  end = _end;
proc print data = x;
  where end;
run;
like image 35
Triad sou. Avatar answered Oct 01 '22 12:10

Triad sou.