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

Hi,

I would like to delete all the line that have a commun variable condionally to a "If then" statement. 

 

For instance, if X2= 2 , I want that all the observations that have the same X1 as the observation with X2=2 are deleted.

 

Example :

HAVE :

X1 X2
A 1
A 3
A 4
A 5
B 1
B 3
B 5
B 6
B 8
B 2
B 1
C 1
C 5
C 4
C 3
C 9

 

WANT :

X1 X2
A 1
A 3
A 4
A 5
C 1
C 5
C 4
C 3
C 9

 

 

I first thought to  :

Data Wante;

Set Have;

By X1;

If X2=2 then delete;

run;

 

 

But it doesn't work... 

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20


data have;
input X1 $	X2;
cards;
A	1
A	3
A	4
A	5
B	1
B	3
B	5
B	6
B	8
B	2
B	1
C	1
C	5
C	4
C	3
C	9
;

data want;
 do until(z);
   set have(in=a) have(in=b) end=z;
   by x1;
   if first.x1 then _n_=0;
   if  a and x2=2 then _n_=1 ;
   if b and not _n_ then output;
 end;
run;

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20


data have;
input X1 $	X2;
cards;
A	1
A	3
A	4
A	5
B	1
B	3
B	5
B	6
B	8
B	2
B	1
C	1
C	5
C	4
C	3
C	9
;

data want;
 do until(z);
   set have(in=a) have(in=b) end=z;
   by x1;
   if first.x1 then _n_=0;
   if  a and x2=2 then _n_=1 ;
   if b and not _n_ then output;
 end;
run;
Ksharp
Super User
data have;
input X1 $	X2;
cards;
A	1
A	3
A	4
A	5
B	1
B	3
B	5
B	6
B	8
B	2
B	1
C	1
C	5
C	4
C	3
C	9
;
proc sql;
create table want as
select * from have 
 where x1 not in (select distinct x1 from have where x2=2);
quit;
hhinohar
Quartz | Level 8
data have;
	infile datalines dlm="09"x;
	input X1 $ X2;
	datalines;
A	1
A	3
A	4
A	5
B	1
B	3
B	5
B	6
B	8
B	2
B	1
C	1
C	5
C	4
C	3
C	9
;
run;

data want;
  do _N_=1 by 1 until(last.x1);
    set have;
    by x1;

    if x2=2 then
      flg=1;
  end;

  do _N_=1 by 1 to _N_ ;
    set have;
    by x1;
    
    if flg then continue;
	output;
  end;
  drop flg;
run;

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
  • 559 views
  • 0 likes
  • 5 in conversation