Solved
Contributor
Posts: 24

# Logic problem

Variable name-cars

name of dataset-name

I am trying to remove certain cars that make up <1% or <3% from a dataset.

I am not sure which of the following if then do end statement has a correct logic-

Code 1-

data work.name work.name_other; set work.name;

if cars not in ('BMW', 'AUDI', 'BENZ') then do;
if percent < 1 or count <3 then output work.name_other; /*Pull cars that make up <1% or <3*/
end;

else output work.name;
run;

Code 2-

data work.name work.name_other; set work.name;
if count <3 and cars not in ('BMW', 'AUDI', 'BENZ')
then output work.name_other; /*Pull cars that make up <1% or <3*/

else output work.name;
run;

Thanks!

Accepted Solutions
Solution
‎11-29-2017 11:57 AM
Super User
Posts: 13,941

## Re: Logic problem

Are your Percent values multiplied by 100?  Example: 0.25 or 25? Depending on how you created percent you might have percents in the 0 to 1 range not the 0 to 100 range and therefore your "Percent <1" may behave incorrectly.

All Replies
Super User
Posts: 24,015

## Re: Logic problem

LPT: format your code, it makes it easier to see if the indents match and if they don't you can find your errors more easily.

``````data work.name work.name_other;
set work.name;

if cars not in ('BMW', 'AUDI', 'BENZ') then
do;
if percent < 1 or count <3 then output work.name_other; /*Pull cars that make up <1% or <3*/
end;
else output work.name;
run;

data work.name work.name_other;
set work.name;

if count <3 and cars not in ('BMW', 'AUDI', 'BENZ') then output work.name_other; /*Pull cars that make up <1% or <3*/
else output work.name;
run;``````

They aren't the same logic, but they're both valid SAS syntax.

It's not clear what your actual question is - the logic for the if/then/do is correct in both cases, but since you didn't specify what you're logic should should be and what you have I can't comment beyond this.

Contributor
Posts: 24

## Re: Logic problem

I am trying to remove certain cars that make up <1% or <3% from a dataset.
How can I edit Code 2 to match the logic?
Super User
Posts: 24,015

## Re: Logic problem

Juli13 wrote:
I am trying to remove certain cars that make up <1% or <3% from a dataset.
How can I edit Code 2 to match the logic?

Add the part about COUNT, you currently have COUNT<3 or PERCENT<1 but your comment is regarding percentages...and anything less than 1% is also less than 3% so it's not clear logic, but easy enough to code.

Contributor
Posts: 24

## Re: Logic problem

is this correct:

data work.name work.name_other; set work.name;
if count <3 and percent > 1 and cars not in ('BMW', 'AUDI', 'BENZ')
then output work.name_other; /*Pull cars that make up <1% or <3*/
else output work.name;
run;
Contributor
Posts: 24

## Re: Logic problem

data work.name work.name_other; set work.name;
if count <3 and percent < 1 and cars not in ('BMW', 'AUDI', 'BENZ')
then output work.name_other; /*Pull cars that make up <1% or <3*/
else output work.name;
run;
Super User
Posts: 24,015

## Re: Logic problem

No, it doesn't match the first one:

``if percent < 1 or count <3``
Contributor
Posts: 24

## Re: Logic problem

data work.name work.name_other; set work.name;
if count <3 and percent < 1 and cars not in ('BMW', 'AUDI', 'BENZ')
then output work.name_other; /*Pull cars that make up <1% or <3*/
else output work.name;
run;

Super User
Posts: 24,015

## Re: Logic problem

Run the code and test it.

Give the output data set different names and then use PROC COMPARE to check for any differences.

Contributor
Posts: 24

## Re: Logic problem

is the logic correct?
Super User
Posts: 24,015

## Re: Logic problem

The logic is the same. Is it correct? No idea.

Super User
Posts: 24,015

## Re: Logic problem

It's also bad form to use the same output name. Try using different ones otherwise you destroy your source data.

data work.name work.name_other;

set work.name;

if count <3 and percent < 1 and cars not in ('BMW', 'AUDI', 'BENZ')
then output work.name_other; /*Pull cars that make up <1% or <3*/
else output work.name;
run;

Once you run this once, and it replaces your original data so you cannot re-run this step without first recreating the original NAME data set.

Solution
‎11-29-2017 11:57 AM
Super User
Posts: 13,941

## Re: Logic problem

Are your Percent values multiplied by 100?  Example: 0.25 or 25? Depending on how you created percent you might have percents in the 0 to 1 range not the 0 to 100 range and therefore your "Percent <1" may behave incorrectly.

Contributor
Posts: 24

## Re: Logic problem

Thank you so much ballardw! I couldn't understand why code 1 was behaving incorrectly. They are in % format. Thanks!
Super User
Posts: 24,015

## Re: Logic problem

It helps if you state what's happening, for example you never said anything was 'behaving incorrectly', all you were asking was "is the logic right" and didn't provide any sample data. If you'd provided sample data this would have been answered in the first example.  And this is why I said the code is the same, but I can't answer if the logic was correct, because it clearly wasn't....

Juli13 wrote:
Thank you so much ballardw! I couldn't understand why code 1 was behaving incorrectly. They are in % format. Thanks!

☑ This topic is solved.