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!!
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.