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!!:)
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.
Please:
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!!:)
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.
Nearly 200 sessions are now available on demand in the Innovate Hub.
Watch Now →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.