LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (August 2006, week 2)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Wed, 9 Aug 2006 10:17:13 -0400
Reply-To:     Venky Chakravarthy <swovcc@HOTMAIL.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Venky Chakravarthy <swovcc@HOTMAIL.COM>
Subject:      Re: how to pick out the last observation of a data set?
Comments: To: Rob.Workman@sorin-na.com
In-Reply-To:  <50B07FD4B1B5304CAE96D25253A2ED79017CECF2@AUSTINMAIL1.us.dmn>
Content-Type: text/plain; format=flowed

Rob Workman has kindly pointed out that the first SET statement is unnecessary.

Thanks for the tip. This is really neat.

Venky

----Original Message Follows---- From: "Workman, Rob" <Rob.Workman@sorin-na.com> To: "Venky Chakravarthy" <swovcc@HOTMAIL.COM> Subject: RE: Re: how to pick out the last observation of a data set? Date: Wed, 9 Aug 2006 08:47:20 -0500

Venky,

Your technique works, but the extra set statement is not necessary. Since variables created using set statement options are initialized at compile time, you can just use:

data lastobs; set test nobs = nobs point = nobs; output; stop; run;

Rob

Rob Workman Sorin-na

-----Original Message----- From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Venky Chakravarthy Sent: Wednesday, August 09, 2006 8:32 AM To: SAS-L@LISTSERV.UGA.EDU Subject: Re: how to pick out the last observation of a data set?

On Wed, 9 Aug 2006 04:55:42 -0400, Wu Shengzhe <sas.shengzhe@GMAIL.COM> wrote:

>Hello, > >When a data set has been read into SAS, how to pick out the last >observation of this data set? > >Thank you, >Shengzhe

If you want only the last observation then you may choose to have random access to it.

data test ; do x = 1 to 10 ; output ; end ; run ;

data lastobs ; if 0 then set test nobs = nobs ; set test point = nobs ; output ; stop ; run ;

This will read just the last observation and output it. The first SET, which is always false loads the header information that has the number of observations in it. The last observation is NOBS. The second SET statement is instructed to directly fetch only this observatiopn. Since we break the rules of the default data step behavior, we also need to instruct the data step to stop processing soon after outputting this observation.

Some may point out that if the data set is modified in place then this logic could fail. Very true. However, in practice, at least in the field that I work, I have seen datasets modified in place very rarely.

Venky Chakravarthy

---------------------------------------------------------------------------------------------- This message contains confidential information intended only for the use of the addressee(s). If you are not the addressee, or the person responsible for delivering it to the addressee, you are hereby notified that reading, disseminating, distributing or copying this message is strictly prohibited. If you have received this message by mistake, please notify us, by replying to the sender, and delete the original message immediately thereafter. Thank you.


Back to: Top of message | Previous page | Main SAS-L page