Hi all,
I have a data set with coded values 0 and 1 that are sorted by subject and date. Each time the occurrence happens, coded 1. When stops, coded 0. If occurrence happens again, then 1 etc. I want to output the observation by subject at each change of occurrence (1, 0, back to 1 etc).
Any ideas how I can set this up?
Subject | Response | Date | Wanted |
123 | 1 | 10-May-18 | X |
123 | 1 | 22-Jun-18 | |
123 | 0 | 30-Jul-18 | X |
123 | 1 | 12-Sep-18 | X |
125 | 1 | 15-Oct-18 | X |
125 | 1 | 16-Oct-18 | |
125 | 1 | 15-Nov-18 | |
125 | 0 | 1-Dec-18 | X |
125 | 0 | 20-Dec-18 |
Use the NOTSORTED keyword on your BY statement.
data want ;
set have;
by subject response notsorted;
if first.response;
run;
Use the NOTSORTED keyword on your BY statement.
data want ;
set have;
by subject response notsorted;
if first.response;
run;
If data sorted by subject variable :
DATA WANT (drop=x);
SET HAVE;
BY SUBJECT;
X=IFN(RESPONSE EQ LAG(Response),0,1);
IF X;
RUN;
Thanks
🙂
That will not work if two adjacent subjects happen to have the same value of RESPONSE.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.