Sentry Page Protection
Coding Exercise (Answer)
Exercise 1
Answer:
Data PhoneBill2;
Set PhoneBill;
Base = 30;
Add_Min = Max(Minutes-200, 0);
Add_Meg = Max(Messages-25, 0);
Add_Dat = Max(Data-1000, 0);
Amount = 30 + Add_Min*0.1 + Add_Meg*0.5 + Add_Dat*0.05;
Run;
Exercise 2
Exercise 1
Answer:
Data PhoneBill2;
Set PhoneBill;
Base = 30;
Add_Min = Max(Minutes-200, 0);
Add_Meg = Max(Messages-25, 0);
Add_Dat = Max(Data-1000, 0);
Amount = 30 + Add_Min*0.1 + Add_Meg*0.5 + Add_Dat*0.05;
Run;
Exercise 2
Answer:
Data Race2;
Set Race;
Avg = Mean(Lap1, Lap2, Lap3, Lap4, Lap5);
Run;
Exercise 3
Answer:
Data Expense2;
Set Expense;
RandInt = Floor(Rand('uniform')*10) + 1;
If RandInt = 5;
Run;
Exercise 4
Answer:
Data Date2;
Set Date;
Age = Floor((MarriageDate - BirthDate)/365.25);
Run;
Exercise 5
Answer:
Data Name2;
Set Name;
Ind_space = index(Name, " ");
First = substr(Name, 1, Ind_Space-1);
Last = substr(Name, Ind_Space);
Run;
Exercise 6
Answer:
Data Product2;
Set Product;
Length class $30;
d1 = substr(barcode, 1, 1);
if d1 = 2 then class = "Meat, Fruits, Vegetables";
else if d1 = 3 then class = "Drug";
else if d1 = 4 then class = "Loyalty Card";
else if d1 = 5 or d1 = 9 then class = "Coupon";
else if d1 in (0, 1, 6, 7, 8) then class = "General";
Run;
Exercise 7
Answer:
Data Product3;
Set Product;
ManfNo = substr(barcode, 2, 5);
Run;
Exercise 8
Answer:
Data Product4;
Set Product;
Ind = Index(barcode, "67503");
Run;
*The barcode is associated with the product #67503 if the Ind variable contains a non-zero value.
Exercise 9
Answer:
Data Product5;
Set Product;
** Last Digit **;
d12 = substr(barcode, 12, 1);
** Check Digit Calculations **;
d1=substr(barcode, 1, 1);
d2=substr(barcode, 2, 1);
d3=substr(barcode, 3, 1);
d4=substr(barcode, 4, 1);
d5=substr(barcode, 5, 1);
d6=substr(barcode, 6, 1);
d7=substr(barcode, 7, 1);
d8=substr(barcode, 8, 1);
d9=substr(barcode, 9, 1);
d10=substr(barcode, 10, 1);
d11=substr(barcode, 11, 1);
odd = sum(of d1, d3, d5, d7, d9, d11)*3;
even = sum(of d2, d4, d6, d8, d10);
check = 10 - mod(odd+even, 10);
** Check if Last Digit is the same as Check Digit **;
if d12 ^= check then flag = 1;
Run;
* There is an error if flag shows the value of 1.;
Data Race2;
Set Race;
Avg = Mean(Lap1, Lap2, Lap3, Lap4, Lap5);
Run;
Exercise 3
Answer:
Data Expense2;
Set Expense;
RandInt = Floor(Rand('uniform')*10) + 1;
If RandInt = 5;
Run;
Exercise 4
Answer:
Data Date2;
Set Date;
Age = Floor((MarriageDate - BirthDate)/365.25);
Run;
Exercise 5
Answer:
Data Name2;
Set Name;
Ind_space = index(Name, " ");
First = substr(Name, 1, Ind_Space-1);
Last = substr(Name, Ind_Space);
Run;
Exercise 6
Answer:
Data Product2;
Set Product;
Length class $30;
d1 = substr(barcode, 1, 1);
if d1 = 2 then class = "Meat, Fruits, Vegetables";
else if d1 = 3 then class = "Drug";
else if d1 = 4 then class = "Loyalty Card";
else if d1 = 5 or d1 = 9 then class = "Coupon";
else if d1 in (0, 1, 6, 7, 8) then class = "General";
Run;
Exercise 7
Answer:
Data Product3;
Set Product;
ManfNo = substr(barcode, 2, 5);
Run;
Exercise 8
Answer:
Data Product4;
Set Product;
Ind = Index(barcode, "67503");
Run;
*The barcode is associated with the product #67503 if the Ind variable contains a non-zero value.
Exercise 9
Answer:
Data Product5;
Set Product;
** Last Digit **;
d12 = substr(barcode, 12, 1);
** Check Digit Calculations **;
d1=substr(barcode, 1, 1);
d2=substr(barcode, 2, 1);
d3=substr(barcode, 3, 1);
d4=substr(barcode, 4, 1);
d5=substr(barcode, 5, 1);
d6=substr(barcode, 6, 1);
d7=substr(barcode, 7, 1);
d8=substr(barcode, 8, 1);
d9=substr(barcode, 9, 1);
d10=substr(barcode, 10, 1);
d11=substr(barcode, 11, 1);
odd = sum(of d1, d3, d5, d7, d9, d11)*3;
even = sum(of d2, d4, d6, d8, d10);
check = 10 - mod(odd+even, 10);
** Check if Last Digit is the same as Check Digit **;
if d12 ^= check then flag = 1;
Run;
* There is an error if flag shows the value of 1.;