BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
UcheOkoro
Lapis Lazuli | Level 10

Hello,

Please, I need help excluding observations from a variable that are out of order. For each ID, the order should be 010101 or 101010.  If the order is 001101, we exclude the repeated value, so it becomes 0101. The following is my sample data.

Thank you!

data WORK.CLASS(label='Student Data');
 input ID$3. Wgtloss;
 datalines;
222 0
222 1
222 1
222 0
222 0
222 0
223 1
223 1
223 1
223 0
223 0
223 1
224 1
224 1
224 0
224 1
224 0
224 0
 ;run;
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Seems like a simple application of BY group processing.  Use the NOTSORTED keyword on the BY statement to form the groups.

data have;
 input ID :$3. Wgtloss;
datalines;
222 0
222 1
222 1
222 0
222 0
222 0
223 1
223 1
223 1
223 0
223 0
223 1
224 1
224 1
224 0
224 1
224 0
224 0
;

data want;
  set have;
  by id wgtloss notsorted ;
  if first.wgtloss;
run;

Results

Obs    ID     Wgtloss

  1    222       0
  2    222       1
  3    222       0
  4    223       1
  5    223       0
  6    223       1
  7    224       1
  8    224       0
  9    224       1
 10    224       0

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

Seems like a simple application of BY group processing.  Use the NOTSORTED keyword on the BY statement to form the groups.

data have;
 input ID :$3. Wgtloss;
datalines;
222 0
222 1
222 1
222 0
222 0
222 0
223 1
223 1
223 1
223 0
223 0
223 1
224 1
224 1
224 0
224 1
224 0
224 0
;

data want;
  set have;
  by id wgtloss notsorted ;
  if first.wgtloss;
run;

Results

Obs    ID     Wgtloss

  1    222       0
  2    222       1
  3    222       0
  4    223       1
  5    223       0
  6    223       1
  7    224       1
  8    224       0
  9    224       1
 10    224       0
UcheOkoro
Lapis Lazuli | Level 10

Thank you so much for your assistance!

 

UcheOkoro
Lapis Lazuli | Level 10

Please, I have a follow up question. What if I want only the order where the number 1 comes first i.e. 101010?

 

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 154 views
  • 0 likes
  • 2 in conversation