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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 652 views
  • 2 likes
  • 3 in conversation