|
On Sun, 26 Oct 2008 10:30:04 -0400, Muthia Kachirayan
<muthia.kachirayan@GMAIL.COM> wrote:
>On Sun, Oct 26, 2008 at 7:19 AM, Peter Müller <mortal@gmx.li> wrote:
>
>> Hello to the list members!
>>
>>
>> I have got a dataset with 2 groups with differing N.
>>
>> I need to fill up the second group with the value zero for variable b and
>> need to fill up group 2 for the variable a, so that both groups have five
>> entries:
>>
>>
>> data* test;
>> input group a ;
>> cards;
>> 1 4
>> 1 3
>> 1 2
>> 1 1
>> 1 0
>> 2 1
>> 2 0
>> ;
>> run;
>>
>>
>>
>> I need
>>
>> group a
>>
>> 1 4
>> 1 3
>> 1 2
>> 1 1
>> 1 0
>> 2 1
>> 2 0
>> new additional wanted:
>> 2 0
>> 2 0
>> 2 0
>> ;
>>
>> thank you very much!
>>
>>
>> Peter M�ller
>>
>
>Peter,
>
>A more generalized problem can be tackled using the following code and the
>data set. The maximum of group can be anywhere and not necessarily the
>first. In the first pass of the data set (TEST), the maximum size of GROUP
>is found and retained. At the end, it is stored as a macro variable.
>
> In the second pass of the data set, missing rows are filled and output as
>desired.
>
>data test;
>input group a ;
>cards;
>1 4
>1 3
>1 2
>3 4
>3 3
>3 2
>3 2
>3 4
>3 5
>2 1
>2 0
>;
>run;
>proc sort data = test;
>by group;
>run;
>
>data _null_;
>retain max_group;
>do _n_ = 1 by 1 until(last.group);
> set test end = eof;
> by group;
> max_group = max(max_group, _n_);
>end;
>if eof then call symputx('max_group', max_group);
>run;
>%put &max_group;
>data need;
>do _n_ = 1 by 1 until(last.group);
> set test;
> by group;
> output;
>end;
>if &max_group > _n_ then
> do _n_ = _n_ + 1 by 1 while(_n_ le &max_group);
> a = 0;
> output;
> end;
>run;
A variation is to do it in one step, with no macro variable, using a double
nested DoW structure:
data need(drop = max_group);
do until (eof); do _n_ = 1 by 1 until(last.group);
set test end = eof;
by group;
max_group = max(max_group, _n_);
end; end;
eof = 0;
do until (eof);
do _n_ = 1 by 1 until(last.group);
set test;
by group;
output;
end;
if max_group > _n_ then
do _n_ = _n_ + 1 by 1 while(_n_ le max_group);
a = 0;
output;
end;
end;
run;
>
>OUTPUT:
>
> Obs group a
>
> 1 1 4
> 2 1 3
> 3 1 2
> 4 1 0
> 5 1 0
> 6 1 0
> 7 2 1
> 8 2 0
> 9 2 0
> 10 2 0
> 11 2 0
> 12 2 0
> 13 3 4
> 14 3 3
> 15 3 2
> 16 3 2
> 17 3 4
> 18 3 5
>
>Regards,
>
>Muthia Kachirayan
|