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

Hi!

I have a code where I remove all observations with the same variable A when variable B is not the same number for that same A.

 

data want(drop=flag);
do until (last. a);
set have;
by a notsorted;
if first. a then _iorc_=b;
if _iorc_ ne b then flag=1;
end;
do until (last. a);
set have;
by a notsorted;
if not flag then output;
end;
run;

Now I want to look at the data I removed.

I want to remove all observations of variable A where the variable B only has a unqiue number for that A.

 

    A B

1 1 2

2 1 2

3 2 4

4 2 4

5 2 5

6 3 1

7 4 2

8 4 1

to

    A B

 

 

1 2 4

2 2 4

3 2 5

 

4 4 2

5 4 1

Help would be much appreciated!!:)

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

It's easiest if you do it while you are separating out the observations you want:

 

data want removed;
do until (last. a);
   set have;
   by a notsorted;
   if first. a then _iorc_=ve;
   if _iorc_ ne ve then flag=1;
end;
do until (last. a);
   set have;
   by a notsorted;
   if flag then output removed;
   else output want;
end;
drop flag;
run;

This assumes that your original code was working properly to get the observations you want.

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

Please:

  • post data in readily usable form (data step with datalines)
  • test your code with this data before posting
  • copy/paste the code directly here

I post this because your code, as posted, can never run.


@Viktoreli wrote:

Hi!

I have a code where I remove all observations with the same variable A when variable B is not not the same number for that same A.

 

data want(drop=flag);
do until (last. a);
set have;
by a notsorted;
if first. a then _iorc_=ve;
if _iorc_ ne ve then flag=1;
end;
do until (last. a);
set have;
by a notsorted;
if not flag then output;
end;
run;

Now I want to look at the data I removed.

I want to remove all observations of variable A where the variable B only has a unqiue number for that A.

 

    A B

1 1 2

2 1 2

3 2 4

4 2 4

5 2 5

6 3 1

7 4 2

8 4 1

to

    A B

 

 

1 2 4

2 2 4

3 2 5

 

4 4 2

5 4 1

Help would be much appreciated!!:)


 

Viktoreli
Obsidian | Level 7
Thanks for your reply! Looked at my code and noticed mistakes when changing the names of the variables. I will try to follow your pointers in the future:)!
Astounding
PROC Star

It's easiest if you do it while you are separating out the observations you want:

 

data want removed;
do until (last. a);
   set have;
   by a notsorted;
   if first. a then _iorc_=ve;
   if _iorc_ ne ve then flag=1;
end;
do until (last. a);
   set have;
   by a notsorted;
   if flag then output removed;
   else output want;
end;
drop flag;
run;

This assumes that your original code was working properly to get the observations you want.

Viktoreli
Obsidian | Level 7
Thank you so much:)
tomrvincent
Rhodochrosite | Level 12
What does 'is not not the same' mean?
Viktoreli
Obsidian | Level 7
Good question:)

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 1228 views
  • 8 likes
  • 4 in conversation