BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Shradha1
Obsidian | Level 7
I have a list of students and a list of devices they have used for various sessions, all in a sorted form:
Student Device
A. Mac
A. Tablet
A. Laptop
B. PC
B. ANDROID
B. ANDROID
C. TABLET
C. TABLET
C. IPHONE

I need to create a flag for those students for whom the device in their last session doesn't match with the device in the previous session.
So for the above data, the flag will be created for student A and C. How do I proceed? Please help
1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Try this

 

data have;
input Student $ Device $;
datalines;
A Mac    
A Tablet 
A Laptop 
B PC     
B ANDROID
B ANDROID
C TABLET 
C TABLET 
C IPHONE 
;

data want(drop = d);
   do until (last.Student);
      set have;
      by Student;
      d = lag(Device);
      flag = (last.Student and Device ne d);
   end;
   do until (last.Student);
      set have;
      by Student;
      output;
   end;
run;

 

Result:

 

Student  Device   flag 
A        Mac      1 
A        Tablet   1 
A        Laptop   1 
B        PC       0 
B        ANDROID  0 
B        ANDROID  0 
C        TABLET   1 
C        TABLET   1 
C        IPHONE   1 

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

Try this

 

data have;
input Student $ Device $;
datalines;
A Mac    
A Tablet 
A Laptop 
B PC     
B ANDROID
B ANDROID
C TABLET 
C TABLET 
C IPHONE 
;

data want(drop = d);
   do until (last.Student);
      set have;
      by Student;
      d = lag(Device);
      flag = (last.Student and Device ne d);
   end;
   do until (last.Student);
      set have;
      by Student;
      output;
   end;
run;

 

Result:

 

Student  Device   flag 
A        Mac      1 
A        Tablet   1 
A        Laptop   1 
B        PC       0 
B        ANDROID  0 
B        ANDROID  0 
C        TABLET   1 
C        TABLET   1 
C        IPHONE   1 

Kurt_Bremser
Super User
data want;
set have;
by student; /* add notsorted option if necessary */
ldev = lag(device); /* keep previous value of device */
if
  last.student /* test only at last observation of student */
  and device ne ldev /* compare devices */
  and not first.student /* students with only one entry are never flagged */
then flag = 1;
else flag = 0;
drop ldev;
run;
Astounding
PROC Star
Let's simplify:

data want;
set have;
by student device notsorted;
if last.student and first.device then flag=1;
run;
Astounding
PROC Star

Good catch, @Kurt_Bremser .  I could have been more careful.  However, the suggestion you made won't do the job.  It will flag the first observation for each student (as long as the student has more than one observation).  

 

Here is a more careful variation:

 

data want;
set have;
by student device notsorted;
if last.student and first.device and not first.student then flag=1;
run;

 

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 6 replies
  • 795 views
  • 2 likes
  • 4 in conversation