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

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  
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Use the NOTSORTED keyword on your BY statement.

data want ;
  set have;
  by subject response notsorted;
  if first.response;
run;

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

Use the NOTSORTED keyword on your BY statement.

data want ;
  set have;
  by subject response notsorted;
  if first.response;
run;
singhsahab
Lapis Lazuli | Level 10

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

🙂

Tom
Super User Tom
Super User

That will not work if two adjacent subjects happen to have the same value of RESPONSE.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4 replies
  • 839 views
  • 2 likes
  • 3 in conversation