Date: Fri, 24 Sep 2010 12:38:54 -0400
Reply-To: Gerhard Hellriegel <gerhard.hellriegel@T-ONLINE.DE>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Gerhard Hellriegel <gerhard.hellriegel@T-ONLINE.DE>
Subject: Re: Using retain
so you want a new init for each new sub?
Why not something like:
data want;
set have;
by sub type;
retain val_ 0;
n_val=val_+val;
/*f=first.type;
l=last.type;*/
if first.sub then do;
val_=val;
n_val=val_;
end;
if last.type then val_=val;
drop val_;
run;
Gerhard
On Fri, 24 Sep 2010 11:47:19 -0400, sas quest <sasquest@GMAIL.COM> wrote:
>oh..yes..i was trying to on similar lines but couldn't succeed since i
kept
>writing if first.sub then retain val_ 0; which was grossly incorrect
>
>On Fri, Sep 24, 2010 at 11:30 AM, Joe Matise <snoopy369@gmail.com> wrote:
>
>> I would suggest this instead:
>>
>> data want;
>> set have;
>> by sub type;
>> retain val_ 0;
>> if first.sub then do;
>> val_=0;
>> n_val=val;
>> end;
>> n_val=val_+val;
>> if last.type then val_=n_val;
>> drop val_ type_counter;
>> run;
>>
>> insomuch as the above works for C,D,E types, and the below only works
for
>> A,B. type_counter is unnecessary.
>>
>> -Joe
>>
>> On Fri, Sep 24, 2010 at 10:12 AM, Arthur Tabachneck <art297@netscape.net
>> >wrote:
>>
>> > How about:
>> >
>> > data want;
>> > set have;
>> > by sub type;
>> > retain val_ 0;
>> > if first.sub then type_counter=1;
>> > if type_counter eq 1 then do;
>> > val_=val;
>> > n_val=val;
>> > end;
>> > else n_val=val_+val;
>> > if last.type then type_counter+1;
>> > drop val_ type_counter;
>> > run;
>> >
>> > Art
>> > --------
>> > On Fri, 24 Sep 2010 11:07:03 -0400, sas quest <sasquest@GMAIL.COM>
>> wrote:
>> >
>> > >Joe,
>> > >your suggested change doesnt work.since i want to retain last value
of
>> > type
>> > >so that i can add it up to get continuous values for each sub.
>> > >
>> > >thanks
>> > >On Fri, Sep 24, 2010 at 10:55 AM, Joe Matise <snoopy369@gmail.com>
>> wrote:
>> > >
>> > >> If you use 'if first.sub' instead of if type = 'A' then it should
work
>> > as
>> > >> you want.
>> > >>
>> > >> -Joe
>> > >>
>> > >>
>> > >> On Fri, Sep 24, 2010 at 9:54 AM, sas quest <sasquest@gmail.com>
>> wrote:
>> > >>
>> > >>> hi ,
>> > >>> my code fails as soon as sub changes to 2.
>> > >>> Art's code works ..but i wanted something more general without
>> > referring
>> > >>> to
>> > >>> type='A',since i might anticipate a variety of types.
>> > >>>
>> > >>> Thanks
>> > >>>
>> > >>> On Fri, Sep 24, 2010 at 10:47 AM, Arthur Tabachneck
>> > <art297@netscape.net
>> > >>> >wrote:
>> > >>>
>> > >>> > I think that you are trying to achieve the following:
>> > >>> >
>> > >>> > data have;
>> > >>> > input sub type $ val;
>> > >>> > cards;
>> > >>> > 1 A 20
>> > >>> > 1 A 30
>> > >>> > 1 A 36
>> > >>> > 1 A 41
>> > >>> > 1 B 1
>> > >>> > 1 B 5
>> > >>> > 1 B 11
>> > >>> > 1 B 19
>> > >>> > 2 A 12
>> > >>> > 2 A 20
>> > >>> > 2 A 29
>> > >>> > 2 A 31
>> > >>> > 2 B 3
>> > >>> > 2 B 10
>> > >>> > 2 B 11
>> > >>> > ;
>> > >>> > run;
>> > >>> >
>> > >>> > data want;
>> > >>> > set have;
>> > >>> > retain val_ 0;
>> > >>> > if type eq 'A' then do;
>> > >>> > val_=val;
>> > >>> > n_val=val;
>> > >>> > end;
>> > >>> > else n_val=val_+val;
>> > >>> > drop val_;
>> > >>> > run;
>> > >>> >
>> > >>> > HTH,
>> > >>> > Art
>> > >>> > ---------
>> > >>> > On Fri, 24 Sep 2010 10:36:59 -0400, Sas Quest
<sasquest@GMAIL.COM>
>> > >>> wrote:
>> > >>> >
>> > >>> > >data have;
>> > >>> > >input sub type$ val;
>> > >>> > >cards;
>> > >>> > >1 A 20
>> > >>> > >1 A 30
>> > >>> > >1 A 36
>> > >>> > >1 A 41
>> > >>> > >1 B 1
>> > >>> > >1 B 5
>> > >>> > >1 B 11
>> > >>> > >1 B 19
>> > >>> > >2 A 12
>> > >>> > >2 A 20
>> > >>> > >2 A 29
>> > >>> > >2 A 31
>> > >>> > >2 B 3
>> > >>> > >2 B 10
>> > >>> > >2 B 11
>> > >>> > >run;
>> > >>> > >
>> > >>> > >Want:
>> > >>> > >Sub type val n_val
>> > >>> > >1 A 20 20
>> > >>> > >1 A 30 30
>> > >>> > >1 A 36 36
>> > >>> > >1 A 41 41
>> > >>> > >1 B 1 42
>> > >>> > >1 B 5 46
>> > >>> > >1 B 11 52
>> > >>> > >1 B 19 60
>> > >>> > >2 A 12 12
>> > >>> > >2 A 20 20
>> > >>> > >2 A 29 29
>> > >>> > >2 A 31 31
>> > >>> > >2 B 3 34
>> > >>> > >2 B 10 41
>> > >>> > >2 B 11 42
>> > >>> > >
>> > >>> > >my code:
>> > >>> > >
>> > >>> > >data want;
>> > >>> > >set have;
>> > >>> > >by sub type;
>> > >>> > >retain val_ 0;
>> > >>> > >n_val=val_+val;
>> > >>> > >/*f=first.type;
>> > >>> > >l=last.type;*/
>> > >>> > >if last.type then val_=val;
>> > >>> > >drop val_;
>> > >>> > >run;
>> > >>> > >
>> > >>> > >
>> > >>> > >What can be modified in my code to get the desired output?
>> > >>> > >
>> > >>> > >Thanks
>> > >>> >
>> > >>>
>> > >>
>> > >>
>> >
>>
|