d**********2 发帖数: 14 | 1 Hi,
I have a data which contains only one variable, the value of which is either
1 or 2, so that the data looks like this, 1221111222221221222 (of course
this is what it looks like after I transposed it). Now I need to know how I
could calculate the number of times 1 and 2 appeared CONSECUTIVELY, for
example, in this case, 1 made a single appearance for 3 times and made a
quadraple appearance for once, 2 made a double appearance for twice, a
triple appearance for once and a 5-times-in-a-row appearance for once. How
do I write a sas code to achieve this?
Thanks you so much!
Laomao | R*********i 发帖数: 7643 | 2 Using the data in original stacking structure, assume variable name is var
and dataset name is test:
data test1;
set test;
by var notsorted;
retain counter;
if first.var then counter=1;
else counter=counter+1;
if last.var then output;
run;
proc sql;
select var,counter,n(counter) as numtimes
from test1
group by var,counter
order by var,counter;
quit; |
|