Sentry Page Protection
Coding Exercise (Answer)
Exercise 1
Answer:
Proc Sort Data=sashelp.stocks out=stocks;
by stock descending high;
Run;
Exercise 2
Exercise 1
Answer:
Proc Sort Data=sashelp.stocks out=stocks;
by stock descending high;
Run;
Exercise 2
Answer:
Proc Sort Data=sashelp.stocks out=stocks;
by stock volume;
Run;
Data Stocks2;
Set Stocks;
by stock volume;
if first.stock then i=1;
else if last.stock then i=2;
if i in (1, 2);
Run;
Intel had its highest trade volume on April 1, 1993. The record trade volume is 184,804,114.
Exercise 3
Answer:
Proc Sort Data=sashelp.stocks out=high;
by stock high;
where date>='01JAN1990'D and date<='01DEC1999'D;
run;
data high2;
set high;
by stock high;
if last.stock;
keep stock high;
run;
Proc Sort Data=sashelp.stocks out=low;
by stock low;
where date>='01JAN1990'D and date<='01DEC1999'D;
run;
data low2;
set low;
by stock low;
if first.stock;
keep stock low;
run;
data diff;
merge high2 low2;
by stock;
diff = high-low;
run;
Exercise 4
Answer:
Data Shoes;
Set sashelp.shoes;
rate = returns / sales;
arbi = 1; * Arbitrary variable *;
Run;
Proc sort data=shoes;
by rate;
Run;
Data Shoes2;
Set shoes;
by arbi rate;
if last.arbi;
Run;
*Please note that an arbitrary variable is needed when there is no grouping variable.
Exercise 5
Answer:
Data Shoes;
Set sashelp.shoes;
rate = returns / sales;
Run;
Proc Sort Data=Shoes;
by region rate;
Run;
Data shoes3;
set shoes;
by region rate;
if last.region;
Run;
Slipper has the highest return rate in Africa. The return rate is 8.8%.
Exercise 6
Answer:
Data Shoes;
Set sashelp.shoes;
rate = returns / sales;
Run;
Proc sort data=shoes;
by region subsidiary descending rate;
Run;
Data Shoes4;
set shoes;
by region subsidiary descending rate;
retain num;
if first.subsidiary then num=0;
num+1;
if num in (1, 2);
run;
Slipper and Boot are returned the most in Bangkok, Asia.
Exercise 7
Answer:
Data Shoes;
Set sashelp.shoes;
arbi = 1;
Run;
Data Total;
Set Shoes;
By Arbi;
Total+Sales;
If last.arbi;
keep Arbi total;
Run;
Exercise 8
Answer:
Data Shoes;
Set sashelp.shoes;
arbi = 1;
Run;
Data Total;
Set Shoes;
By Arbi;
Total+Sales;
If last.arbi;
keep Arbi total;
Run;
** Calculate sales from each region **;
Proc Sort Data=Shoes;
By region;
Run;
Data TotalR;
Set Shoes;
By region;
if first.region then totalR=0;
Arbi = 1;
TotalR+Sales;
If last.region;
keep Region Arbi totalR;
Run;
** Calculate percentage contribution and bonus **;
Data Contri;
Merge TotalR Total;
By Arbi;
ContriPct = TotalR / Total;
Bonus = ContriPct * Total * 0.05;
Run;
Proc Sort Data=sashelp.stocks out=stocks;
by stock volume;
Run;
Data Stocks2;
Set Stocks;
by stock volume;
if first.stock then i=1;
else if last.stock then i=2;
if i in (1, 2);
Run;
Intel had its highest trade volume on April 1, 1993. The record trade volume is 184,804,114.
Exercise 3
Answer:
Proc Sort Data=sashelp.stocks out=high;
by stock high;
where date>='01JAN1990'D and date<='01DEC1999'D;
run;
data high2;
set high;
by stock high;
if last.stock;
keep stock high;
run;
Proc Sort Data=sashelp.stocks out=low;
by stock low;
where date>='01JAN1990'D and date<='01DEC1999'D;
run;
data low2;
set low;
by stock low;
if first.stock;
keep stock low;
run;
data diff;
merge high2 low2;
by stock;
diff = high-low;
run;
Exercise 4
Answer:
Data Shoes;
Set sashelp.shoes;
rate = returns / sales;
arbi = 1; * Arbitrary variable *;
Run;
Proc sort data=shoes;
by rate;
Run;
Data Shoes2;
Set shoes;
by arbi rate;
if last.arbi;
Run;
*Please note that an arbitrary variable is needed when there is no grouping variable.
Exercise 5
Answer:
Data Shoes;
Set sashelp.shoes;
rate = returns / sales;
Run;
Proc Sort Data=Shoes;
by region rate;
Run;
Data shoes3;
set shoes;
by region rate;
if last.region;
Run;
Slipper has the highest return rate in Africa. The return rate is 8.8%.
Exercise 6
Answer:
Data Shoes;
Set sashelp.shoes;
rate = returns / sales;
Run;
Proc sort data=shoes;
by region subsidiary descending rate;
Run;
Data Shoes4;
set shoes;
by region subsidiary descending rate;
retain num;
if first.subsidiary then num=0;
num+1;
if num in (1, 2);
run;
Slipper and Boot are returned the most in Bangkok, Asia.
Exercise 7
Answer:
Data Shoes;
Set sashelp.shoes;
arbi = 1;
Run;
Data Total;
Set Shoes;
By Arbi;
Total+Sales;
If last.arbi;
keep Arbi total;
Run;
Exercise 8
Answer:
Data Shoes;
Set sashelp.shoes;
arbi = 1;
Run;
Data Total;
Set Shoes;
By Arbi;
Total+Sales;
If last.arbi;
keep Arbi total;
Run;
** Calculate sales from each region **;
Proc Sort Data=Shoes;
By region;
Run;
Data TotalR;
Set Shoes;
By region;
if first.region then totalR=0;
Arbi = 1;
TotalR+Sales;
If last.region;
keep Region Arbi totalR;
Run;
** Calculate percentage contribution and bonus **;
Data Contri;
Merge TotalR Total;
By Arbi;
ContriPct = TotalR / Total;
Bonus = ContriPct * Total * 0.05;
Run;