Solved
Contributor
Posts: 69

# deleting a variable on the basis of another variable

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

Accepted Solutions
Solution
‎11-21-2017 12:18 AM
Super User
Posts: 6,644

## Re: deleting a variable on the basis of another variable

Posted in reply to raqthesolid

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;

All Replies
Solution
‎11-21-2017 12:18 AM
Super User
Posts: 6,644

## Re: deleting a variable on the basis of another variable

Posted in reply to raqthesolid

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;

Posts: 1,312

## Re: deleting a variable on the basis of another variable

Posted in reply to raqthesolid

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;``````
☑ This topic is solved.

Need further help from the community? Please ask a new question.

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