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

Hi

 

Lets say in this dataset I have repeated measures for one subject. However, I want to flag if the next observation is different from the previous one...for example the dataset looks like this 

 

Subject  Event

1           A

1           B

1           A

1           A

1           C

1           A

1           B

1           B

1           C

 

 

Now I want to flag if the next event is different from the prior one and my output should mark these values as a flag

 

Subject  Event Flag

1           A          Y

1           B         Y

1           A         Y

1           A

1           C         Y

1           A         Y

1           B         Y

1           B

1           C         Y

 

 

Is there a way to do this?

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Looking at the wanted result, it seems that what you want to do is set flag to 'Y' when the previous event is different from the current?

 

data have;
input Subject Event $;
datalines;
1 A
1 B
1 A
1 A
1 C
1 A
1 B
1 B
1 C
;

data want;
   set have;
   if event ne lag1(event) then flag='Y';
run;

 

Result:

 

Subject Event flag 
1       A     Y 
1       B     Y 
1       A     Y 
1       A       
1       C     Y 
1       A     Y 
1       B     Y 
1       B       
1       C     Y 

View solution in original post

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

Looking at the wanted result, it seems that what you want to do is set flag to 'Y' when the previous event is different from the current?

 

data have;
input Subject Event $;
datalines;
1 A
1 B
1 A
1 A
1 C
1 A
1 B
1 B
1 C
;

data want;
   set have;
   if event ne lag1(event) then flag='Y';
run;

 

Result:

 

Subject Event flag 
1       A     Y 
1       B     Y 
1       A     Y 
1       A       
1       C     Y 
1       A     Y 
1       B     Y 
1       B       
1       C     Y 
Pmyosh
Obsidian | Level 7

Hi,

 

You can use the LAG Function.  If you want the value from the previous record, use LAG(variable).

If previous of previous record, use LAG2

.

.

.

Here's sample code

 

DATA sample;
	INPUT Subject  Event ;
DATALINES ;
1 1
1 2
1 3
1 4
1 5
1 5
1 6
;
RUN ;
DATA sample2 (Drop=Newx);
	SET sample ;
 	newx = LAG(EVENT) ;
	IF Event = newx THEN Flag = 'Y' ; ELSE Flag = 'N' ;
RUN ;

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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