From: mahender on

data new;
input id transaction $;
datalines;
101 x
101 a
101 s
102 w
102 e
103 f
104 f
105 g
105 h
108 q
108 w
108 f
;
run;


this is the data set and i need 1st 2 transaction from every id for
the whole data..using sas or procsql
From: Patrick on
data want;
set new;
by id;
if first.id then flag=0;
flag+1;
if flag<=2 then output;
drop flag;
run;
From: Patrick on
...."flag" is not a good variable name in this context, "counter"
sounds more appropriate, so:

data want;
set new;
by id;
if first.id then counter=0;
counter+1;
if counter<=2 then output;
drop counter;
run;
From: data _null_; on
On Jul 10, 9:01 pm, Patrick <patrick.mat...(a)gmx.ch> wrote:
> ..."flag" is not a good variable name in this context, "counter"
> sounds more appropriate, so:
>
> data want;
>   set new;
>   by id;
>   if first.id then counter=0;
>   counter+1;
>   if counter<=2 then output;
>   drop counter;
> run;

The same but different...

data want;
do _n_ = 1 by 1 until(last.id);
set new;
by id;
if _n_ le 2 then output;
end;
run;
From: Patrick on
....or under the assumption that an ID is never missing:

data want;
set new;
by id;
if lag2(id) ne id then output;
run;