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
Super User
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
Super User
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 2024

Innovate_SAS_Blue.png

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. 

Register now!

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

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