Date: Mon, 9 Jul 2007 15:57:50 -0500
Reply-To: Suhong Tong <sophidt@HOTMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Suhong Tong <sophidt@HOTMAIL.COM>
Subject: Re: How to assign the 2nd obs value to the 1st obs
In-Reply-To: <7.0.1.0.2.20070709110506.07a442b0@wispertel.net>
Content-Type: text/plain; format=flowed
Hi Richard,
Thanks for helping me solve this problem. I tested your code, it gives me
what I expected. I truely appreciate your beautiful code, but a little
beyound my knowledge. If you can give me a little bit education, so I am
able to apply it to other similar situation.
First, is _p create a tempary internal variable for programming use?
second, I never see a code like ^last., how is it different from regular
last.?
Third, I never see set statement in IF-THEN clause, can you provide a
explanation?
last, what is point= means? how to use it? I didn't see it in the output
data set.
I always feel so grateful that so many intelegent people in this list, so I
constantly learn new skills and knowledge.
Many thanks,
Sophia
>From: Richard Read Allen <peakstat@WISPERTEL.NET>
>Reply-To: Richard Read Allen <peakstat@WISPERTEL.NET>
>To: SAS-L@LISTSERV.UGA.EDU
>Subject: Re: How to assign the 2nd obs value to the 1st obs
>Date: Mon, 9 Jul 2007 11:15:07 -0700
>
>
>
>Sophia,
>
>
>When I need to look forward and backward in the same data step, I use the
>following logic. Perhaps it will help you with your problem.
>
>
>- Richard
>
>
>data
> given;
>
>
>input
> PR PP PE;
>
>
>cards
>;
>
>26117 12644 78765
>
>26117 12644 78766
>
>26117 12644 78767
>
>26117 12644 79398
>
>26117 12644 80601
>
>26117 12644 81343
>
>26117 12644 81503
>
>26117 12644 83429
>
>32640 15436 107309
>
>32640 15436 114404
>
>32640 15436 163072
>
>32640 15436 166924
>
>32640 15436 94725
>
>32640 15436 94726
>
>32640 15436 94727
>
>32640 15436 94728
>
>
>
>
>data
> LagForward;
>
>
>set
> Given;
>
>
>by
> PR PP;
>
>
>/*--
>Forward ------------*/
>
>
>_p=_n_+1
>;
>
>
>if
>
>_p<1
>
>then
>
>do
>;
>next_PE=.
>;
>end
>;
>
>
>else
>
>if
> ^last.PP
>then
>
>set
> Given(keep=PE
>
> rename=(PE=next_PE)) point=_p;
>
>
>else
>
>next_PE=.
>;
>
>
>/*-- Lag
>----------------*/
>
>
>_q=_n_-1
>;
>
>
>if
>
>_q<1
>
>then
>
>last_PE=.
>;
>
>
>else
>
>if
> ^first.PP
>then
>
>set
> Given(keep=PE
>
> rename=(PE=last_PE)) point=_q;
>
>
>else
>
>last_PE=.
>;
>
>
>run
>;
>
>
>
>
>On 7/8/07, Suhong Tong <sophidt@hotmail.com> wrote:
>
>
>Hi Howard and Muthia,
>
>
>First, Thank you for helping me on my data problem. Second, I
>apologize
>
>that I provided a misleading output. Instead putting the wrong
>output I
>
>created, I really should have put a result that I want. Below is what
>I
>
>want:
>
>
>For the data presented below, PE=78765 - 78767, 94725 - 94728 are
>
>sequential
>
>numbers, thus these two sets are considered as counseling calls. I
>believe
>
>DIF(PE) only take care of 94726 - 94728,
>
>IFN( first.pr, 1....) can take care of PE=78765, but not something
>stuck
>
>in
>
>the middle(not the first.), like 94725, if I understood what you
>suggested
>
>correctly.
>
>
>I am thinking if SAS has some function can do things opposite to LAG
>
>function, meaning move 1 observation up, then my problem can be taken
>care
>
>of.
>
>
>Thanks,
>
>
>Sohpia
>
>
>pr pp pe IND_PEDIFF
>
>26117 12644 78765 1
>
>26117 12644 78766 1
>
>26117 12644 78767 1
>
>26117 12644 79398 0
>
>26117 12644 80601 0
>
>26117 12644 81343 0
>
>26117 12644 81503 0
>
>26117 12644 83429 0
>
>32640 15436 107309 0
>
>32640 15436 114404 0
>
>32640 15436 163072 0
>
>32640 15436 166924 0
>
>32640 15436 94725 1
>
>32640 15436 94726 1
>
>32640 15436 94727 1
>
>32640 15436 94728 1
>
>
>
>
>
> >From: "Howard Schreier <hs AT dc-sug DOT org>"
><nospam@HOWLES.COM>
>
> >Reply-To: "Howard Schreier <hs AT dc-sug DOT org>"
><nospam@HOWLES.COM>
>
> >To: SAS-L@LISTSERV.UGA.EDU
>
> >Subject: Re: How to assign the 2nd obs value to the 1st obs
>
> >Date: Sat, 7 Jul 2007 22:56:16 -0400
>
> >
>
> >On Fri, 6 Jul 2007 18:57:17 -0400, Sophia Tong
><sophiDT@HOTMAIL.COM>
>
>wrote:
>
> >
>
> > >Dear list,
>
> > >
>
> > >I am trying to determine whether a call is a real counseling
>call or
>
> >random
>
> > >incoming call by looking at the PE numbers. If under the
>same PR-PP,
>
>PE
>
> > >come as a sequential numbers then that set of sequential
>numbers
>
>consider
>
> >as
>
> > >a set of counseling calls.
>
> > >
>
> > >What I did is let PE2=lag(PE), then PE_DIFF=PE-PE2, if PE_DIFF=1
>then
>
> >that
>
> > >call identified as a conseling call. As you see below, the first
>obs
>
>with
>
> > >PE_DIFF=-264, this one should be identified as a real call.
>Another one
>
> > >where PE_DIFF=-72199, that one also should be identified as a
>real
>
>call.
>
> > >
>
> > >My question is how to assign 1s to IND_PEDIFF after SAS see the
>first
>
>1s
>
> >in
>
> > >PE_DIFF?
>
> > >Mine way of flagging in IND_PEDIFF does not cover all
>the
>
>cases. Please
>
> >help.
>
> > >
>
> > >Thanks in advance.
>
> > >
>
> > >Sophia
>
> >
> > The SAS
>System 08:03
>
> > >Friday, July 6, 2007 30
>
> > >
>
> > >
>
>IND_
>
> >
> > PR PP PE PE2 PE_DIFF
>
> >PEDIFF
>
> > > CALLS
>
> > >
>
> >
> > 26117 12644 78765 79029 -264
>
>1
>
> > > 1
>
> >
> > 26117 12644 78766 78765 1
>
>1
>
> > > 2
>
> >
> > 26117 12644 78767 78766 1
>
>1
>
> > > 3
>
> >
> > 26117 12644 79398 78767 631
>
>0
>
> > > 4
>
> >
> > 26117 12644 80601 79398 1203
>
>0
>
> > > 5
>
> >
> > 26117 12644 81343 80601 742
>
>0
>
> > > 6
>
> >
> > 26117 12644 81503 81343 160
>
>0
>
> > > 7
>
> >
> > 26117 12644 83429 81503 1926
>
>0
>
> > > 8
>
> >
> > 32640 15436 107309 94633 12676
>
>1
>
> > > 1
>
> >
> > 32640 15436 114404 107309 7095
>
>0
>
> > > 2
>
> >
> > 32640 15436 163072 114404 48668
>
>0
>
> > > 3
>
> >
> > 32640 15436 166924 163072 3852
>
>0
>
> > > 4
>
> >
> > 32640 15436 94725 166924 -72199
>
>0
>
> > > 5
>
> >
> > 32640 15436 94726 94725 1
>
>1
>
> > > 6
>
> >
> > 32640 15436 94727 94726 1
>
>1
>
> > > 7
>
> >
> > 32640 15436 94728 94727 1
>
>1
>
> > > 8
>
> >
>
> >When presenting a table like this, it helps very much to reduce
>the
>
> >whitespace in order to avoid wrapping. See
>
> >
>
> >
>http://sascommunity.org/wiki/Preparing_Sample_Data_for_SAS-L
>
> >
>
> >Now, to the question. Try something like
>
> >
>
> > data result;
>
> > set given;
>
> > by pr pp;
>
> > IND_PEDIFF = ifn ( first.pr, 1, dif ( pe )=1
>);
>
> > run;
>
> >
>
> >Notice that the indicator can be derived in one statement, with
>no
>
> >intermediate variables. The DIF function combines the roles of the
>LAG
>
> >function and the subtraction operator. When compared to the value 1,
>the
>
> >result is either true (1) or false (0). The IFN function overrides
>that
>
> >result for the first observation in each BY group.
>
>
>_________________________________________________________________
>
>
>
>http://imagine-windowslive.com/hotmail/?locale=en-us&ocid=TXT_TAGHM_migration_HM_mini_pcmag_0507
>
>
>
>
>
>
_________________________________________________________________
http://im.live.com/messenger/im/home/?source=hmtextlinkjuly07
|