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 ❤️ 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 ❤️ 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!
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.
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.
@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.
No, it doesn't match the first one:
if percent < 1 or count <3
data work.name work.name_other; set work.name;
if count ❤️ 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;
Run the code and test it.
Give the output data set different names and then use PROC COMPARE to check for any differences.
The logic is the same. Is it correct? No idea.
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 ❤️ 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.
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.
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!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: