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
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;
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;
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;
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.
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.