Date: Thu, 29 Jan 2009 12:28:06 +0000
Reply-To: karma <dorjetarap@GOOGLEMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: karma <dorjetarap@GOOGLEMAIL.COM>
Subject: Re: Can one manipulate the value of first.whatever?
In-Reply-To: <200901290059.n0SNRLKJ016255@malibu.cc.uga.edu>
Content-Type: text/plain; charset=ISO-8859-1
Art,
I think an interesting point for newer members that hasn't been
clarified in your code below, is that you can use the first. and last.
variables as temporary stores. The same applies to _n_ and _error_
(although maybe the latter is not advisible for readability.
An example for your problem:
data new;
do _n_=1 by 1 until(last.id);
set have;
by id x;
first.id =(_n_=2);
if _n_ > 1 then put _all_;
end;
run;
log:
last.id=0 id=1 x=2 FIRST.id=1 FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=2
last.id=0 id=1 x=3 FIRST.id=0 FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=3
last.id=1 id=1 x=4 FIRST.id=0 FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=4
last.id=0 id=2 x=2 FIRST.id=1 FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=2
last.id=1 id=2 x=3 FIRST.id=0 FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=3
last.id=0 id=3 x=3 FIRST.id=1 FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=2
last.id=1 id=3 x=4 FIRST.id=0 FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=3
2009/1/29 Arthur Tabachneck <art297@netscape.net>:
> Joe,
>
> Much appreciated. I thought of another way, during dinner, but also
> not "quite" perfect:
>
> data want;
> set test;
> by id;
> if lag(first.id) eq 1 then first.id=1;
> deleted=0;
> if first.id then do;
> if x eq 1 then do;
> delete;
> end;
> end;
> first_id=first.id;
> run;
>
> Art
> -------
> On Wed, 28 Jan 2009 18:45:42 -0600, Joe Matise <snoopy369@GMAIL.COM> wrote:
>
>>You certainly can't manipulate it the way you suggest directly, as that
>>would be inconsistent with how a data step works.
>>
>>The ways I could imagine doing something like that:
>>
>>-- Use a counter:
>>data
>>have;
>>
>> input id
>>x;
>>
>> cards;
>>
>>1
>>1
>>
>>1
>>1
>>
>>1
>>3
>>
>>1
>>4
>>
>>2
>>1
>>
>>2
>>2
>>
>>2
>>3
>>
>>3
>>2
>>
>>3
>>3
>>
>>3
>>4
>>
>>;
>>
>>run;
>>
>>
>>
>>proc sort
>>data=have;
>>
>>by
>>id;
>>
>>run;
>>
>>
>>
>>data
>>have2;
>>
>>set
>>have;
>>
>>by
>>id;
>>
>>if first.id then
>>counter=0;
>>
>>counter+1;
>>
>>run;
>>
>>
>>
>>
>>
>>data
>>want;
>>
>> set
>>have2;
>>
>> by
>>id;
>>
>> retain
>>firstid;
>>
>> if first.id then
>>firstid=1;
>>
>> if counter = firstid then
>>do;
>>
>> if x eq 1 then
>>do;
>>
>>
>>firstid=firstid+1;
>>
>>
>>delete;
>>
>>
>>end;
>>
>> end;
>>
>>run;
>>*yields 7 records, deleting the first two records in id=1 and the first
>>record in id=2.
>>
>>-- Use a retained tracker (same HAVE dataset):
>>
>>
>>data
>>want;
>>
>> set
>>have;
>>
>> by
>>id;
>>
>> retain
>>nodelete;
>>
>> if first.id then
>>nodelete=0;
>>
>> if nodelete=0 then
>>do;
>>
>> if x eq 1 then
>>do;
>>
>>
>>delete;
>>
>>
>>end;
>>
>> else
>>nodelete=1;
>>
>> end;
>>
>>run;
>>
>>-- DOW loop:
>>
>>
>>
>>
>>
>>
>>data
>>want;
>>
>> do y=1 by 1 until (last.id or x ne
>>1);
>>
>> set
>>have;
>>
>> by
>>id;
>>
>> end;
>>
>> drop
>>y;
>>
>>run;
>>
>>though I'm not 100% sure why this works, I was expecting it to not work at
>>all, but it at least works for the example (and also for the example where
>>x=1 further down from first.id, it keeps that record). But it does seem
> to
>>work.
>>
>>-- Perhaps use LAG, though I can't come up with a functional example.
>>
>>-Joe
>>
>>
>>
>>On Wed, Jan 28, 2009 at 6:06 PM, Arthur Tabachneck
> <art297@netscape.net>wrote:
>>
>>> Akshaya asked a question earlier today that made me think of a solution
>>> that required a manipulative first.variable feature. However, I don't
>>> know how one could accomplish that. For example, given the data file:
>>>
>>> data have;
>>> input id x;
>>> cards;
>>> 1 1
>>> 1 2
>>> 1 3
>>> 1 4
>>> 2 1
>>> 2 2
>>> 2 3
>>> 3 2
>>> 3 3
>>> 3 4
>>> ;
>>>
>>> how can one, or can one, reset first.id if, because of a delete, it
> isn't
>>> the first record anymore? For example:
>>>
>>> data want;
>>> set test;
>>> by id;
>>> if first.id then do;
>>> if x eq 1 then do;
>>> delete;
>>> /* reset first.id here*/;
>>> end;
>>> end;
>>> first_id=first.id;
>>> run;
>>>
>>> would (if the missing command were added) end up looking like:
>>>
>>> data desired_want;
>>> input id x first_id;
>>> cards;
>>> 1 2 1
>>> 1 3 0
>>> 1 4 0
>>> 2 2 1
>>> 2 3 0
>>> 3 2 1
>>> 3 3 0
>>> 3 4 0
>>> ;
>>>
>>> Just wondering,
>>> Art
>>>
>
|