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

Hi All,

 

I am trying to implement a logic but not able to achieve the right output. I would appreciate any help.

 

I have a dataset which has columns: student and flag. I want to calculate consecutive_flag column which will create group of flags within the student group. an example below:

 

student flag consective_flag
1 0 0
1 0 0
1 0 0
1 1 1
1 1 1
1 1 1
1 0 0
1 1 2
1 1 2
1 0 0
1 0 0
1 1 3
1 1 3
2 1 1
2 1 1
2 1 1
2 1 1
2 0 0
2 1 2
2 0 0
2 0 0
2 1 3
2 1 3
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

data have(drop=consective_flag);

input student   flag consective_flag;

datalines;

1    0    0

1    0    0

1    0    0

1    1    1

1    1    1

1    1    1

1    0    0

1    1    2

1    1    2

1    0    0

1    0    0

1    1    3

1    1    3

2    1    1

2    1    1

2    1    1

2    1    1

2    0    0

2    1    2

2    0    0

2    0    0

2    1    3

2    1    3

;

 

data want;

set have;

by student flag notsorted;

retain t consective_flag ;

if first.student then call missing(consective_flag,t);

if first.flag and not flag then consective_flag=flag;

else if first.flag and flag then do;

t+flag;

consective_flag=t;

end;

drop t;

run;

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20

data have(drop=consective_flag);

input student   flag consective_flag;

datalines;

1    0    0

1    0    0

1    0    0

1    1    1

1    1    1

1    1    1

1    0    0

1    1    2

1    1    2

1    0    0

1    0    0

1    1    3

1    1    3

2    1    1

2    1    1

2    1    1

2    1    1

2    0    0

2    1    2

2    0    0

2    0    0

2    1    3

2    1    3

;

 

data want;

set have;

by student flag notsorted;

retain t consective_flag ;

if first.student then call missing(consective_flag,t);

if first.flag and not flag then consective_flag=flag;

else if first.flag and flag then do;

t+flag;

consective_flag=t;

end;

drop t;

run;

anu1999
Obsidian | Level 7

appreciate it so much! it works perfect.

Ksharp
Super User
data have(drop=consective_flag);
input student   flag consective_flag;
datalines;
1    0    0
1    0    0
1    0    0
1    1    1
1    1    1
1    1    1
1    0    0
1    1    2
1    1    2
1    0    0
1    0    0
1    1    3
1    1    3
2    1    1
2    1    1
2    1    1
2    1    1
2    0    0
2    1    2
2    0    0
2    0    0
2    1    3
2    1    3
;
data want;
 set have;
 by student flag notsorted;
 if first.student then count=0;
 if first.flag and flag=1 then count+1;
 if flag=1 then want=count;
  else want=0;
 drop count;
run;
anu1999
Obsidian | Level 7

Another great way. Thanks, this works too.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 4 replies
  • 1285 views
  • 2 likes
  • 3 in conversation