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

 

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!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

View solution in original post

14 REPLIES 14
Reeza
Super User

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
Fluorite | Level 6
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?
Reeza
Super User

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

 

 

Juli13
Fluorite | Level 6
is this correct:

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;
Juli13
Fluorite | Level 6
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;
Reeza
Super User

No, it doesn't match the first one:

 

if percent < 1 or count <3
Juli13
Fluorite | Level 6

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;

Reeza
Super User

Run the code and test it.

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

 

 

 

Juli13
Fluorite | Level 6
is the logic correct?
Reeza
Super User

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

Reeza
Super User

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. 

 

 

ballardw
Super User

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.

Juli13
Fluorite | Level 6
Thank you so much ballardw! I couldn't understand why code 1 was behaving incorrectly. They are in % format. Thanks!
Reeza
Super User

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!

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 14 replies
  • 1366 views
  • 1 like
  • 3 in conversation