|
1. Regarding the first problem: I don't understand where the 9th and
final observation in the desired output comes from. There are only 8
observations in the input. I also don't understand what is meant by
"looping back" when ID or C changes. Perhaps you want OUT set to zero
for the last observation in each group, regardless of the result from
the other rule. However, the example is not crafted well enough to
illustrate that.
2. Regarding the second problem: It would be beneficial to see some
DATA step code, with comments indicating the areas of difficulty.
3. Regarding ability to post directly to SAS-L: I presume you have
registered via the uga.edu website. Go to the SAS-L page
(http://www.listserv.uga.edu/archives/sas-l.html) and click "Post to
the list". You should get a prompt for your e-mail address and password.
Follow the directions on that form. If you don't succeed, report
precisely what you see, quoting messages verbatim.
On Sun, 6 Jun 2004 12:52:32 -0700, SAS Plumber <sasplumber@HOTMAIL.COM>
wrote:
>I hope I can help a little, Thomas.
>
>Okay, so you want to read row j+1 whilst processing row j. Well, SAS
>has the LAG function, but not a look-ahead function! So, here's how
to
>do it (use a 2nd SET statement with the POINT parameter so you can
get
>the obs you want):
>
>data mydata;
> infile cards;
> input id a b c d;
> cards;
>1 0.1 0.5 10 1
>1 0.05 0.45 10 0.5
>1 0.105 0.5 10 1
>1 0.1 0.6 11 0.5
>1 0.2 0.55 11 0.35
>2 1 1.1 10 1
>2 0.8 1 10 0.9
>2 0.4 1 10 0.95
>;
>run;
>
>data result;
> set mydata end=finish;
> by id;
> *keep id a b c d out;
> if not finish and not last.id then do;
> next_n_ = _n_ + 1;
> set mydata (keep=a b c d rename=(a=NextA b=NextB c=NextC
d=NextD))
> point=next_n_;
>
> if NextB le b and NextD lt d then do;
> out = a - NextA;
> end;
> else do;
> out = 0;
> end;
>
> end;
> else do;
> NextA = .;
> NextB = .;
> NextC = .;
> NextD = .;
> out = .;
> end;
>run;
>
>proc print;run;
>
>I hope this gives you a headstart. Maybe some of the other SAS-Lers
>will help out further (or improve on what I've done)
>
>Good luck
>
>The Plumber
>
>>From: Thomas <tythong@yahoo.com>
>>To: sasplumber@hotmail.com
>>Subject: SAS Datastep
>>Date: Fri, 4 Jun 2004 05:13:42 -0700 (PDT)
>>
>>Hi Plumber,
>>
>>How are you? It's awkward to email you personally for
>>help as I do not know why I cannot access the posting
>>area after registering with SAS-L.
>>I would very much appreciate it if you could help me
>>on the SAS programming as I have been suffering long,
>>long time on the following problems due to my limited
>>knowledge on SAS. Basically, the problem has two
>>parts as belows.
>>
>>Thank you very much!!
>>Warmest Regards,
>>Thomas
>>
>>(1). I would like to do a comparison between row J and
>>J+1 and output at J, but I realise that I cannot use
>>the lag or retain to perform this as the output will
>>print on J+1 as I want it to be printed on J. And, I
>>have a criteria to satisfy that if the j+1 observation
>>is true then the SAS program will perform the
>>comparison. Procedure steps (given the dataset as
>>belows) :
>> 1. If the observation of b at j+1 (row) is lesser or
>>equal than b at j, AND
>> 2. if the observation of d at j+1 is smaller than d
>>at j, THEN
>> 3. "out" at j equal to a(j)-a(j+1), ELSE
>> 4. "out" at j equal to 0.
>> 5. The program should loop back (restart again) when
>>the id is changed or c is changed.
>>
>>*INPUT;
>>Id a b c d
>>1 0.1 0.5 10 1
>>1 0.05 0.45 10 0.5
>>1 0.105 0.5 10 1
>>1 0.1 0.6 11 0.5
>>1 0.2 0.55 11 0.35
>>2 1 1.1 10 1
>>2 0.8 1 10 0.9
>>2 0.4 1 10 0.95
>>
>>*Desired Outpu;
>>Id a b c d out
>>1 0.1 0.5 10 1 0.05
>>1 0.05 0.45 10 0.5 0
>>1 0.105 0.5 10 1 0
>>1 0.1 0.6 11 0.5 -0.1
>>1 0.2 0.55 11 0.35 0
>>2 1 1.1 10 1 0.2
>>2 0.8 1 10 0.9 0
>>2 0.4 1 10 0.95 0
>>2 0.3 0.8 10 0.9 0.1
>>
>>
>>The second problem is
>>(2). I have a dataset as shown belows (sashelp.txt)
>>and would like to compute the variables LOA and IND in
>>the output (sasout.txt). The procedure logic is that
>>1. When p equal to bid and delb is not equal to zero
>>then do LOA=min(siz, delb) and IND=S. when p equal to
>>ofr and deba is not equal to zero then do LOA=min(siz,
>>dela) and IND=B. Else LOA=0, IND=Y. 2. The next obs.
>>of delb or dela will be reduced if QID is the same as
>>previous one and LOA is not equal to zero, then the
>>next delb or dela of the same QID should minus the
>>previous non-zero LOA. And, do the 1 step comparison
>>again when the criteria in 1. are satisfied. 3. If p
>>equal to bid but delb for the next same QID, after
>>taking into account for the previous determined LOA,
>>is equal to 0 then the criteria at first step are not
>>satisfied then LOA=0 and IND=Y. Same for p=ofr case.
>>4. loop again/restart for new DATE and new ID.
>>
>>*input;
>>ID DATE QID P SIZ BID BIDS OFR OFRS DELB DELA
>>A 1 1 10 50 10 100 10.5 50 100 0
>>A 1 1 10 60 10 100 10.5 50 100 0
>>A 1 1 10.5 20 10 100 10.5 50 100 0
>>A 1 2 10.5 30 10 200 10.5 150 0 30
>>A 1 2 11 50 10 200 10.5 150 0 30
>>A 2 4 12 50 11.5 50 12 100 0 0
>>A 2 4 12 20 11.5 100 12 100 0 0
>>A 2 5 11.5 30 11 150 11.5 200 0 100
>>A 2 5 11.5 40 11 150 11.5 200 0 100
>>B 1 3 22 100 21 50 21.5 80 50 0
>>B 1 3 21 80 21 50 21.5 80 50 0
>>B 1 3 21 50 21 50 21.5 80 50 0
>>B 1 3 20.5 100 21 50 21.5 80 50 0
>>B 1 4 21 50 20.5 100 21 100 0 40
>>B 1 4 21 20 20.5 100 21 100 0 40
>>B 2 5 21.5 30 20.5 50 21 100 50 0
>>B 2 5 20.5 60 20.5 50 21 100 50 0
>>B 2 5 20.5 30 20.5 50 21 100 50 0
>>
>>
>>*desired output;
>>
>>ID DATE QID P SIZ BID BIDS OFR OFRS DELB DELA LOA IND
>>A 1 1 10 50 10 100 10.5 50 100 0 50 S
>>A 1 1 10 60 10 100 10.5 50 100 0 50 S
>>A 1 1 10.5 20 10 100 10.5 50 100 0 0 Y
>>A 1 2 10.5 30 10 200 10.5 150 0 30 30 B
>>A 1 2 11 50 10 200 10.5 150 0 30 0 Y
>>A 2 4 12 50 11.5 50 12 100 0 0 0 Y
>>A 2 4 12 20 11.5 100 12 100 0 0 0 Y
>>A 2 5 11.5 30 11 150 11.5 200 0 100 30 B
>>A 2 5 11.5 40 11 150 11.5 200 0 100 40 B
>>B 1 3 22 100 21 50 21.5 80 50 0 0 Y
>>B 1 3 21 80 21 50 21.5 80 50 0 50 B
>>B 1 3 21 50 21 50 21.5 80 50 0 0 Y
>>B 1 3 20.5 100 21 50 21.5 80 50 0 0 Y
>>B 1 4 21 50 20.5 100 21 100 0 40 40 B
>>B 1 4 21 20 20.5 100 21 100 0 40 0 Y
>>B 2 5 21.5 30 20.5 50 21 100 50 0 0 Y
>>B 2 5 20.5 60 20.5 50 21 100 50 0 50 S
>>B 2 5 20.5 30 20.5 50 21 100 50 0 0 Y
>>
>>
>>
>>
>>
>>__________________________________
>>Do you Yahoo!?
>>Friends. Fun. Try the all-new Yahoo! Messenger.
>>http://messenger.yahoo.com/
|