BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi

is there any option to get last observation in a dataset( using firstobs , obs dataset options)

ex:
data ex;
input a;
datalines;
66
77
88
;

output:
88
7 REPLIES 7
deleted_user
Not applicable
This would be the syntax, however it will not work when data is read from datalines. It will work when reading from a file, and if you put it on a set statement for a dataset:


data ex;
infile datalines END=LAST;
input a;
IF LAST;
datalines;
66
77
88
;
deleted_user
Not applicable
thank u very much.

is there any possibility using with firstobs, obs dataset options for creating that dataset.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
In SAS 9.1.3, the SET stmt FIRSTOBS=MAX parameter generates an error, unfortunately, for what appears to be an incorrect "last observation" calculation (based on the error message).


Scott Barry
SBBWorks, Inc.


data x;
do i=1 to 21;
output;
end;
stop;
run;
data _null_;
set x (firstobs=max);
putlog _all_;
stop;
run;
venkatesh
Calcite | Level 5
Hi SaiRam,

If u know the number of Observations(firstobs or obs).

Try this.,

data test;
infile cards
firstobs=3;
input a;
cards;
76
56
98
;
proc print;
run;

Venky.
BPD
Obsidian | Level 7 BPD
Obsidian | Level 7
Sairam,

The following utility macro might solve your problem:

%MACRO obs(lib=,mem=);
%LET ds = %SYSFUNC(OPEN(&lib..&mem));
%LET noobs = %SYSFUNC(ATTRN(&ds,NOBS));
%LET ds = %SYSFUNC(CLOSE(&ds));
&noobs;
%MEND obs;

If we generate the following dataset - a1

data a1;
do val = 1 to 100;
output;
end;
run;

Use the macro statement - %LET to assign a macro variable (obs_in_dset in this example) the output of the %obs macro. In the macro call name the sas library in which your dataset resides (lib=work) and the sas dataset name (mem=a1)

e.g.
%let obs_in_dset = %obs(lib=work,mem=a1);

Now when you want to read the last obs in the a1 dataset use

data lastina1;
set a1(firstobs=&obs_in_dset);
run;

I hope you find this useful.

Regards,

BPD
deleted_user
Not applicable
when your data are in a SAS data set, you can point read the last row (without reading the other rows):[pre] data last_row ;
set your_dataset nobs=nobs point= nobs;
output ;
stop ;
run ;[/pre]

was that what was wanted?


PeterC
bentleyj1
Quartz | Level 8
_Exactly_ what I needed. Thanks PeterC wherever you are.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 7 replies
  • 6705 views
  • 0 likes
  • 5 in conversation