Lesson 5: Filtering a table

Today, you’ll learn how to filter a table and save the output to a new one.

/*Create table with dummy practice data*/
DATA motor_vehicle_fatalities;
INPUT Year Country $ Fatalities Rate;
DATALINES;
2015 USA 35092 11.3
2014 USA 32675 10.3
2013 USA 32719 10.3
2013 FRANCE 3268 5.1
;
RUN;

You can filter the table using either the IF or WHERE statement:

DATA usa_fatalities;
/*Use SET statement to specify the input table*/
SET motor_vehicle_fatalities;
/*Use the optional WHERE or IF statement to filter*/
WHERE Country = "USA";
RUN;

What we’ve effectively done here is take the fatalities table, filtered it, and saved the output to a new table. Pretty easy!

Practice question: which statement would you use to filter for 2013 policies only?

a) WHERE Year = “2013”;
b) WHERE Year = 2013;
c) IF Year = “2013”;
d) IF YEAR = 2013;
e) a & c
f) b & d

 

 

 

 

 

 

Answer: f
The variable “Year” is numerical, hence quotation marks should not be used. Both the WHERE and IF statement can be used to filter.

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s