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.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.