Monday, July 13, 2015

PROC SQL

One of cool things about SAS is that, unlike R and Stata, you can use SQL commands as part of the base package.

I'll assume you have the auto dataset loaded.

Since you've loaded the auto dataset, we can skip the data step and go directly to "proc sql":

Task #1: select all observations

proc sql;
select * from auto;
run;

Task #2: select observations where price of car is above $8000

proc sql;
select * from auto where price > 8000;
run;

Task #3: find the average price of foreign and domestic cars

proc sql;
select avg(price) as avgprice, foreign from auto group by foreign;
run;

Task #4: order observations according to price

proc sql;
select * from auto order by price;
run;

Task #5: find observations where the model contains AMC

proc sql;
select * from auto where make contains 'AMC';

run;

Task #6: find the number of cars which are foreign and domestic

proc sql;
select count(make) as numberofcars, foreign from auto group by foreign;
run;

Task #7: find the number of cars which are above $8000 and less than $8000

proc sql;
select count(make) as numberofcars, (price > 8000) as highprice from auto group by highprice;

run;

Task #8: create a table to store data (and subsequently print it)
proc sql;
create table exampletable as select * from auto where price > 8000;
run;

proc print data = exampletable;

run;

No comments:

Post a Comment