|
Here is one way to tackle it:
1. sort the data by patno and abs(lbdy)
2. use first.patno to get the record from the sorted data and assign
vis=5
3. merge back to original data
All very simple step, you should try yourself.
On Thu, 18 Jun 2009 09:10:16 -0700, Al <ali6058@GMAIL.COM> wrote:
>On Jun 18, 9:23 am, snoopy...@GMAIL.COM (Joe Matise) wrote:
>> I recommend a more clear explanation of how you determine vis_num; I
have no
>> idea why you gave patno #1 a vis_num only for the second row, or any of
the
>> others really; I suspect if you spend a bit more time considering the
logic
>> of your request, your answer will follow fairly quickly.
>>
>> In general, if you want to consider all of the records at once for a
>> particular patno, and then retroactively do (something), you can use the
>> double DoW loop. For your data you seem to separate the low lb_day from
the
>> high lb_day, so you'd need an artificial variable to do that (as you
need to
>> have proper BY group sorting). Something like this probably will work,
>> though you note I left a bit where I don't follow your logic so I can't
fill
>> that bit in.
>>
>> data have;
>> input patno lbtest $ lb_day lbres;
>> if lb_day le 7 then sortord=1;
>> else sortord=2;
>> datalines;
>> 1 Creat 4 32
>> 1 Creat 7 12
>> 1 Creat 6 11
>> 1 Creat 2 10
>> 2 Creat 5 30
>> 2 Creat 6 23
>> 2 Creat 7 19
>> 3 Creat 2 16
>> 3 Creat 3 17
>> 3 Creat 4 18
>> 1 Creat 12 32
>> 1 Creat 13 12
>> 1 Creat 14 11
>> 1 Creat 16 10
>> 2 Creat 15 30
>> 2 Creat 16 23
>> 2 Creat 14 19
>> 3 Creat 13 16
>> 3 Creat 14 17
>> 3 Creat 15 18
>> ;;;;
>> run;
>>
>> data want;
>> retain vis_num visrec;
>> do _n_ = 1 by 1 until (last.patno);
>> set have;
>> by sortord patno;
>> *your logic here to figure out your vis_num;
>> if (something) then do;
>> visrec = lb_day;
>> if sortord = 1 then vis_num = 5;
>> else if sortord = 2 then vis_num = 15;
>> end;
>> end;
>> do _n_ = 1 by 1 until (last.patno);
>> set have;
>> by sortord patno;
>> if lb_day ne visrec then call missing(vis_num); *to clear vis_num
from
>> records other than the desired record;
>> output;
>> end;
>> run;
>>
>> -Joe
>>
>>
>>
>> On Thu, Jun 18, 2009 at 7:34 AM, Al <ali6...@gmail.com> wrote:
>> > All,
>>
>> > I have a dataset as following
>>
>> > Patno lbtest lb_day lbres vis_num
>> > 1 Creat 4 32
>> > 1 Creat 7 12
>> > 1 Creat 6 11
>> > 1 Creat 2 10
>> > 2 Creat 5 30
>> > 2 Creat 6 23
>> > 2 Creat 7 19
>> > 3 Creat 2 16
>> > 3 Creat 3 17
>> > 3 Creat 4 18
>> > 1 Creat 12 32
>> > 1 Creat 13 12
>> > 1 Creat 14 11
>> > 1 Creat 16 10
>> > 2 Creat 15 30
>> > 2 Creat 16 23
>> > 2 Creat 14 19
>> > 3 Creat 13 16
>> > 3 Creat 14 17
>> > 3 Creat 15 18
>>
>> > I have to create a variable vis_num based on the value of
>> > lb_day.following the below rules
>> > for lb_day between 2 and 7 if lb_day = 5 then vis_num should be 5
>> > if
>> > lb_day doesnt have a value (i.e 5 in this case)..then i have to pick
>> > a
>> > value which is closer to 5 ..if equi distant .. i should be able to
>> > pick the latter value ..and same with 12 and 15 .. i should be
>> > assiging 15.
>>
>> > The output should look like below.I guess .. i was very crude in
>> > explaining the issue.
>>
>> > Patno lbtest lb_day lbres vis_num
>> > 1 Creat 4 32 .
>> > 1 Creat 7 12 5lab_day (7,6) equidistant
>> > from 5.latter was picked(7)
>> > 1 Creat 6 11 .
>> > 1 Creat 2 10 .
>> > 2 Creat 5 30 5
>> > 2 Creat 6 23 .
>> > 2 Creat 7 19 .
>> > 3 Creat 2 16 .
>> > 3 Creat 3 17 .
>> > 3 Creat 4 18 5lab_day(4) closer to 5 ..so
>> > it was picked
>> > 1 Creat 12 32 .
>> > 1 Creat 13 12 .
>> > 1 Creat 14 11 .
>> > 1 Creat 16 10 15
>> > 2 Creat 14 30 .
>> > 2 Creat 15 23 15
>> > 2 Creat 16 19 .
>> > 3 Creat 13 16 .
>> > 3 Creat 14 17 .
>> > 3 Creat 15 18 15
>>
>> > Thanks in advance- Hide quoted text -
>>
>> - Show quoted text -
>
>
>Hi ALL;
>
>
> Here you go again
> patno test lbdy
> 1 cret 5
> 1 cret 8
> 2 cret 4
> 2 cret 3
> 3 cret 2
> 3 cret 6
> 3 cret 7
> I am supposed to create a variable vis
> if lbdy = 5 then vis = 5
> if lbdy doesnt have 5 in it ..i should be able
> to pick a row and assign 5 to it based on which one
> is closer to 5 , and if equdistant i should be able to pick
> the latter ..
>
>
> Here is the output ..
>
> patno test lbdy vis
> 1 cret 5 5
> 1 cret 8 .
> 2 cret 4 5
> 2 cret 3 .
> 3 cret 4 .
> 3 cret 6 5
> 3 cret 7 .
>
> Thanks in advance and i apolozise for
> unclear post, hope i was clear this time
|