BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Karolus
Obsidian | Level 7

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;

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20
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

View solution in original post

3 REPLIES 3
Karolus
Obsidian | Level 7
p.s. apologies for the typos , I meant "among two only possibilities"
PeterClemmensen
Tourmaline | Level 20
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
Karolus
Obsidian | Level 7

oh my, of course,  a very forgotten option, 'by notsorted'.  A millions thanks!!

SAS Innovate 2025: Call for Content

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!

Submit your idea!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 607 views
  • 2 likes
  • 2 in conversation