Hi everyone,
I have the following scenario:
The raw data has a seqoccur to indicate the order in which it happened and cannot be changed. The type variable indicates what happened among to only possibilities.
data rawdata;
input seqoccur 1-2 type $ 3-13;
cards;
1 CONTINUOUS
2 CONTINUOUS
3 BROKEN
4 BROKEN
5 BROKEN
6 BROKEN
7 CONTINUOUS
8 CONTINUOUS
9 CONTINUOUS
10 CONTINUOUS
11 CONTINUOUS
12 BROKEN
13 CONTINUOUS
14 BROKEN
15 BROKEN
;
run;
To this, in the variable I call 'grouptype' I need to assign a value incrementally as below, i.e. repeating within the first type of occurrence and incrementing by the next occurrence and the number doesn't change until the next occurrence. I've tried the usual sort/retain/first.variable techniques but none I know gives the result as below in "grouptype". Any suggestions? many thanks!
data want;
input seqoccur 1-2 type $ 3-13 grouptype 14-15;
cards;
1 CONTINUOUS 1
2 CONTINUOUS 1
3 BROKEN 2
4 BROKEN 2
5 BROKEN 2
6 BROKEN 2
7 CONTINUOUS 3
8 CONTINUOUS 3
9 CONTINUOUS 3
10 CONTINUOUS 3
11 CONTINUOUS 3
12 BROKEN 4
13 CONTINUOUS 5
14 BROKEN 6
15 BROKEN 6
;
run;
data rawdata;
input seqoccur 1-2 type $ 3-13;
cards;
1 CONTINUOUS
2 CONTINUOUS
3 BROKEN
4 BROKEN
5 BROKEN
6 BROKEN
7 CONTINUOUS
8 CONTINUOUS
9 CONTINUOUS
10 CONTINUOUS
11 CONTINUOUS
12 BROKEN
13 CONTINUOUS
14 BROKEN
15 BROKEN
;
run;
data want;
set rawdata;
by type notsorted;
if first.type then grouptype + 1;
run;
Result:
seqoccur type grouptype 1 CONTINUOUS 1 2 CONTINUOUS 1 3 BROKEN 2 4 BROKEN 2 5 BROKEN 2 6 BROKEN 2 7 CONTINUOUS 3 8 CONTINUOUS 3 9 CONTINUOUS 3 10 CONTINUOUS 3 11 CONTINUOUS 3 12 BROKEN 4 13 CONTINUOUS 5 14 BROKEN 6 15 BROKEN 6
data rawdata;
input seqoccur 1-2 type $ 3-13;
cards;
1 CONTINUOUS
2 CONTINUOUS
3 BROKEN
4 BROKEN
5 BROKEN
6 BROKEN
7 CONTINUOUS
8 CONTINUOUS
9 CONTINUOUS
10 CONTINUOUS
11 CONTINUOUS
12 BROKEN
13 CONTINUOUS
14 BROKEN
15 BROKEN
;
run;
data want;
set rawdata;
by type notsorted;
if first.type then grouptype + 1;
run;
Result:
seqoccur type grouptype 1 CONTINUOUS 1 2 CONTINUOUS 1 3 BROKEN 2 4 BROKEN 2 5 BROKEN 2 6 BROKEN 2 7 CONTINUOUS 3 8 CONTINUOUS 3 9 CONTINUOUS 3 10 CONTINUOUS 3 11 CONTINUOUS 3 12 BROKEN 4 13 CONTINUOUS 5 14 BROKEN 6 15 BROKEN 6
oh my, of course, a very forgotten option, 'by notsorted'. A millions thanks!!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.