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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.