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

Hello Folks,

I have following kind of data;

 

Firm var_A 

a       1           

a       2 

a      3

a      4

a      5

b      2

b      1

c      1

 

i know i can always use "if and then statement" but it will delete only 2,3 and 4.

i want to Delete Firm, if the var_A is between 2 to 4. i don't want to delete the only 2-4 but what i want is to delete the all of the Firm if any on the Var_A comes in the range of 2-4. so after that logic only C will remain. all of Firm A and b should delete. 
I will be thankful for any help. 
Regards

raq

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

There are probably several ways.  Here's one:

 

data want;

delete_flag = 0;

do until (last.firm);

   set have;

   by firm;

   if var_a  in (2, 3, 4) then delete_flag=1;

end;

do until (last.firm);

   set have;

   by firm;

   if delete_flag = 0 then output;

end;

drop delete_flag;

run;

View solution in original post

2 REPLIES 2
Astounding
PROC Star

There are probably several ways.  Here's one:

 

data want;

delete_flag = 0;

do until (last.firm);

   set have;

   by firm;

   if var_a  in (2, 3, 4) then delete_flag=1;

end;

do until (last.firm);

   set have;

   by firm;

   if delete_flag = 0 then output;

end;

drop delete_flag;

run;

mkeintz
PROC Star

You want to delete all obs for a firm if any record of that frim has var_a between 2 and 4.

 

You can do this with a SET command that reads all var_a between 2 and 4 for a given firm prior to reading all records for the same firm:

 

data have;
  input Firm $1. var_A ;
datalines;
a  1           
a  2 
a  3
a  4
a  5
b  2
b  1
c  1
run;

data want (drop=exclude);
  set have (where=(var_a between 2 and 4)  in=found_2_to_4)
      have (in=allobs);
  by firm;
  if first.firm then exclude=ifn(found_2_to_4=1,1,0);
  retain exclude;
  if allobs=1 and exclude=0;
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

SAS Innovate 2025: Register Today!

 

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 928 views
  • 1 like
  • 3 in conversation