Tuesday, 23 February 2016

SQL Stored Procedure Performance Improvement

Purpose: Use this job aid to review ways to improve the performance of stored procedures.
  • To improve the performance of stored procedures, you can
  • Use the SET NOCOUNT ON statement. Place this statement just after the AS keyword in the body of the procedure. This prevents SQL Server from sending messages to the client after SELECT, INSERT, UPDATE, MERGE and DELETE statements are executed. This eliminates unnecessary network overhead.
  • Use schema names to create or reference database objects in a procedure. This prevents permission and access problems that are caused by a user's schema being assigned to objects that don't have a schema. It will also allow the Database Engine to resolve object names without having to search multiple schemas. This will decrease processing time.
  • Don't use wrapping functions on columns which contain WHERE and JOIN clauses. This will define the column as non-deterministic and will prevent the query processor from accessing the indexes.
  • Avoid using SELECT*, to prevent Database Engine errors that might cause procedure execution to stop.
  • Don't process or return large volumes of data. Using stored procedures to process or return only small sets of data increases efficiency. It reduces the amount of data that has to be transmitted via a network, and reduces the processing that the client application has to do.
  • Improve error handling using the Transact-SQL TRY...CATCH construct. This allows for more accurate error reporting with less programming.
  • Prevent passing NULL to columns that don't allow null values by using the DEFAULT keyword. The DEFAULT keyword must be in all table columns that are referenced by CREATE TABLE or ALTER TABLE Transact-SQL statements.
  • Eliminate null values from queries by using the modification statements that convert nulls and include logic. Null values in Transact-SQL are problematic because a null is a placeholder for an unknown value. This can cause strange behavior when using AGGREGATE functions or querying for result sets.
  • Use NULL or NOT NULL for each column in a temporary table to ensure consistent nullability behavior.

Monday, 15 February 2016

SAS Certification Practice Exam: Base Programming for SAS 9

I recently took SAS base exam after 5 week of study. I managed to pass SAS base certification with 85% score.

Firstly,  I started of with the following SAS official guide book.

SAS Certification Prep Guide, 3rd Edition Front Cover

I  finish above book in 4 weeks then I practiced mock exams for a week from various pdfs available over the web. I found some similar questions and some of these questions I answered wrong many times while taking mock tests.I am putting these questions here might be any help to you.


Question 51
Question  148
Question 212
The following SAS program is submitted:

data work.retail;
cost = ‘20000’;
total= .10* cost
run;

What is the result?

A. The value of the variable TOTAL in the output data set is 2000. No messages are written to the SAS log.

B. The value of the variable TOTAL in the output data set is 2000. A note that conversion has taken place is written to the SAS log
.
C. The value of the variable TOTAL in the output data set is missing. An error message is written to the SAS log.

D. The variable TOTAL in the output data set has no value. The program fails to execute due to a syntax error.

The following SAS program is submitted:

data work.retail;
cost = '20000';
total = .10 * cost;
run;

Which one of the following is the value of the variable TOTAL in the output data set?



A. 2000
B. '2000'
C. . (missing numeric value)
D. ' ' (missing character value)






The following SAS program is submitted:


What is the result?
A. The value of the variable Discount in the output data set is 2000. No messages are written to the SAS log.

B. The value of the variable Discount in the output data set is 2000. A note that conversion has taken place is written to the SAS log.

C. The value of the variable Discount in the output data set is missing. A note in the SAS log refers to invalid numeric data.

D. The variable Discount in the output data set is set to zero. No messages are written to the SAS log.
Answer: B
Answer: A
Answer: C
Question 201
Question 202
Question 22
The following SAS program is submitted:
data work.totalsales (keep =monthsales{12} );
set work.monthlysales (keep = year product sales);
array monthsales {12} ;
do i=1 to 12;
monthsales{i} = sales;
end;
run;

The data set named WORK.MONTHLYSALES has one observation per month for each of five
years for a total of 60 observations.

Which one of the following is the result of the above program?

A. The program fails execution due to data errors.
B. The program fails execution due to syntax errors.
C. The program executes with warnings and creates the WORK.TOTALSALES data set.
D. The program executes without errors or warnings and creates the WORK.TOTALSALES dataset

The following SAS program is submitted:
data work.totalsales;
set work.monthlysales(keep = year product sales);

retain monthsales {12} ;
array monthsales {12} ;
              do i = 1 to 12;
                   monthsales{i} = sales;
              end;
        cnt + 1;
monthsales{cnt} = sales;
run;

The data set named WORK.MONTHLYSALES has one observation per month for each of five
years for a total of 60 observations.
Which one of the following is the result of the above program?
A. The program fails execution due to data errors.
B. The program fails execution due to syntax errors.
C. The program runs with warnings and creates the WORK.TOTALSALES data set with 60
observations.
D. The program runs without errors or warnings and creates the WORK.TOTALSALES data set
with 60 observations

data work.totalsales (keep =monthsales{12} );
set work.monthlysales (keep = year product sales);
       array monthsales {12} ;
              do i=1 to 12;
                   monthsales{i} = sales;
              end;
              drop i;
run;

The program fails execution due to syntax errors. What is the cause of the syntax error?


A. The variable MONTHSALES does not exist.

B. An array cannot be referenced on a KEEP data set option.

C. The KEEP= data set option should be (KEEP = MONTHSALES).

D. The KEEP= data set option should be the statement KEEP MONTHSALES{12}.

Answer: B
Answer: B
Answer: B
Question 149
Question 150
Question 7
The following SAS program is submitted:
data work.test;

Author = 'Agatha Christie';
First = substr(scan(author,1,' ,'),1,1);
run;

Which one of the following is the length of the variable FIRST in the output data set?
A. 1
B. 6
C. 15
D. 200

The following SAS program is submitted:
data work.test;

Author = 'Christie, Agatha';
First = substr(scan(author,2,' ,'),1,1);
run;
Which one of the following is the value of the variable FIRST in the output data set?
A. A
B. C
C. Agatha
D. ' ' (missing character value)
The following SAS program is submitted:
data one;
        addressl = ‘214 London Way’;
 run;
data one;
   set one;
address = tranwrd(address1, ‘Way’, ‘Drive’); run;
What are the length and value of the variable ADDRESS?
A. Length is 14; value is ‘214 London Dri’.
B. Length is 14; value is ‘214 London Way’.
C. Length is 16; value is ‘214 London Drive’.
D. Length is 200; value is ‘214 London Drive’.

Answer: D
Answer: A
Answer: D

Question 37
Question 156

Given the raw data file YEARAMT:

----|---10---|---20---|----30
1901 2
1905 1
1910 6
1925 .
1941 1
The following SAS program is submitted:
data coins;
infile ‘yearamt’;
input year quantity;
run;

Which statement(s) completed the program and produced a non-missing value for the variable
TOTQUANTITY in the final observation of the output data set?
A. totquantity + quantity;
B. totquantity = sum(totquantity + quantity);
C. retain totquantity; totquantity = totquantity + quantity;
D. retain totquantity0; totquantity = totquantity + quantity;
libname sasdata 'SAS-data-library';
data test;
set sasdata.chemists (keep = job_code);
if job_code = 'chem3'
then description = 'Senior Chemist';
run;
The variable JOB_CODE is a character variable with a length of 6 bytes.
Which one of the following is the length of the variable

DESCRIPTION in the output data set?

A. 6 bytes
B. 8 bytes
C. 14 bytes
D. 200 bytes
Answer : A
Answer : C
Question 66
Question 81

The following SAS program is submitted:
proc means data = sasuser.shoes;
where product in (‘Sandal’ , ‘Slipper’ , ‘Boot’);
run;

Which ODS statements complete the program and send the report to an HTML file?

A. ods html = ‘sales.html’; ods html close;
B. ods file = ‘sales.html’; ods file close;
C. ods file html = ‘sales.html’; ods file close;
D. ods html file = ‘sales.html’; ods html close;

The following SAS program is submitted:
<_insert_ods_code_>
proc means data=SASUSER.SHOES;
where Product in ('Sandal' , 'Slipper' , 'Boot');
run;
<_insert_ods_code_>

Which ODS statements inserted, respectively, in the two location above creates a report stored in an html file?

A. ods html open='sales.html';
    ods html close;
B. ods file='sales.html' / html;
     ods file close;
C. ods html file='sales.html';
    ods html close;
D. ods file html='sales.html';
    ods file close;

Answer: D
Answer: C
Question 207
Given the SAS data set WORK.P2000:
AND WORK.P2008






OUTPUT REQUIRED

ANSWER






QUESTION 191
QUESTION 190
A raw data file is listed below:
RANCH,1250,2,1,Sheppard Avenue,"$64,000"
SPLIT,1190,1,1,Rand Street,"$65,850"
CONDO,1400,2,1.5,Market Street,"80,050"
TWOSTORY,1810,4,3,Garris Street,"$107,250"
RANCH,1500,3,3,Kemble Avenue,"$86,650"
SPLIT,1615,4,3,West Drive,"94,450"
SPLIT,1305,3,1.5,Graham Avenue,"$73,650"
The following SAS program is submitted using the raw data file as input:

data work.condo_ranch;
infile 'file-specification' dsd;
input style $ @;
if style = 'CONDO' or style = 'RANCH' then
input sqfeet bedrooms baths street $ price : dollar10.;
run;

How many observations does the WORK.CONDO_RANCH data set contain?
A. 0
B. 3
C. 5
D. 7
A raw data file is listed below:
RANCH,1250,2,1,Sheppard Avenue,"$64,000"
SPLIT,1190,1,1,Rand Street,"$65,850"
CONDO,1400,2,1.5,Market Street,"80,050"
TWOSTORY,1810,4,3,Garris Street,"$107,250"
RANCH,1500,3,3,Kemble Avenue,"$86,650"
SPLIT,1615,4,3,West Drive,"94,450"
SPLIT,1305,3,1.5,Graham Avenue,"$73,650"
The following SAS program is submitted using the raw data file as input:

data work.condo_ranch;
infile 'file-specification' dsd;
input style $ @;
if style = 'CONDO' or style = 'RANCH';
input sqfeet bedrooms baths street $ price : dollar10.;
run;

How many observations will the output data set contain?

A. 0
B. 3
C. 5
D. 7
Answer: D
Answer: B
Question 25
QUESTION NO: 111

Given the SAS data set SASUSER.HOUSES:

Obs style bedrooms baths price sqfeet street
1CONDO21.5800501200MAIN
2CONDO32.5793501300ELM
3CONDO42.51271501400OAK
4CONDO22.01107001100FIFTH
5TWOSTORY43.01072502100SECOND
6TWOSTORY21.0556501600WEST
7TWOSTORY21.0692501450NORTH
6TWOSTORY42.5102950 2000SOUTH

The following SAS program is submitted:

proc report data = sasuser.houses nowd headline;
column style price;
where price It 100000;
define price / mean width = 9 format = dollar12.;
title;
run;
The following output is desired:
styleprice
-------------
CONDO$79,700
TWOSTORY$62550

Which DEFINE statement completes the program and produces the desired output?

A. define style / width = 9,
B. define style / order width = 9;
C. define style / group width = 9;
D. define style / display width = 9;

Given the following raw data records in TEXTFILE.TXT:
John,FEB,13,25,14,27,Final
John,MAR,26,17,29,11,23,Current
Tina,FEB,15,18,12,13,Final
Tina,MAR,29,14,19,27,20,Current
The following output is desired:
Obs
Name
Month
Status
Week1
Week2
Week3
Week4
Week5
1
John
FEB
Final
$13
$25
$14
$27
.
2
John
MAR
Current
$26
$17
$29
$11
$23
3
Tina
FEB
Final
$15
$18
$12
$13
.
4
Tina
MAR
Current
$29
$14
$19
$27
$20
Which SAS program correctly produces the desired output?
A.
data WORK.NUMBERS;
length Name $ 4 Month $ 3 Status $ 7;
infile ‘TEXTFILE.TXT’ dsd;
input Name $ Month $;
if Month=’FEB’ then input Week1 Week2 Week3 Week4 Status $;
else if Month=’MAR’ then input Week1 Week2 Week3 Week4 Week5 Status $;
format Week1-Week5 dollar6.;
run;  proc print data=WORK.NUMBERS; run;
B.
data WORK.NUMBERS;
length Name $ 4 Month $ 3 Status $ 7;
infile ‘TEXTFILE.TXT’ dlm=’,’ missover;
input Name $ Month $;
if Month=’FEB’ then input Week1 Week2 Week3 Week4 Status $;
else if Month=’MAR’ then input Week1 Week2 Week3 Week4 Week5 Status $;
format Week1-Week5 dollar6.;
run;   proc print data=WORK.NUMBERS; run;
C.
data WORK.NUMBERS;
length Name $ 4 Month $ 3 Status $ 7;
infile ‘TEXTFILE.TXT’ dlm=’,’;
input Name $ Month $ @;
if Month=’FEB’ then input Week1 Week2 Week3 Week4 Status $;
else if Month=’MAR’ then input Week1 Week2 Week3 Week4 Week5 Status $;
format Week1-Week5 dollar6.;
run;
proc print data=WORK.NUMBERS;
run;
D.
data WORK.NUMBERS;
length Name $ 4 Month $ 3 Status $ 7;
infile ‘TEXTFILE.TXT’ dsd @;  input Name $ Month $;
if Month=’FEB’ then input Week1 Week2 Week3 Week4 Status $;
else if Month=’MAR’ then input Week1 Week2 Week3 Week4 Week5 Status $;
format  Week1-Week5 dollar6.;  run;  proc print data=WORK.NUMBERS; run;
Answer: C
Answer :C  ( Option C in PDF is wrong )
Question 104
Question 214
Id
Char1
111
A
158
B
329
C
644
D
and the SAS data set WORK.TWO:
Id
Char2
111
E
538
F
644
G
The following program is submitted:
data WORK.BOTH;
set WORK.ONE WORK.TWO;
by Id;
run;
What is the first observation in SAS data set WORK.BOTH?
A.
Id
Char1
Char2
111
A

B.
Id
Char1
Char2
111
E

C.
Id
Char1
Char2
111
A
 E
D.
Id
Char1
Char2
644
D
G
The following SAS program is sumbitted:
data WORK.INFO;
infile ‘DATAFILE.TXT’;
input @1 Company $20. @25 State $2. @;
if State=’ ‘ then input @30 Year;
else input @30 City Year;
input NumEmployees;
run;
How many raw data records are read during each iteration of the DATA step?
A. 1
B. 2
C. 3
D. 4                                                                                                       
Answer B
Question 62
The following SAS program is submitted:
data numrecords;
infile ‘file specification’;
input@1 patient $15.
relative$ 16-26@;
if relative = ‘children’ then
input @54 diagnosis $15. @;
else if relative = ‘parents’ then
input @28 doctor $15.
clinic $ 44-53
@54 diagnosis $15. @;
input age;
run;

How many raw data records are read during each iteration of the DATA step execution?

A. 1
B. 2
C. 3
D. 4



ANSWER :A             ( IN PDF Answer is C which is wrong)
Answer : B
Question 158
QUESTION 114
Which one of the following SAS statements renames two variables?

A. set work.dept1
work.dept2         (rename = (jcode = jobcode) (sal = salary));

B. set work.dept1
work.dept2        (rename = (jcode = jobcode sal = salary));

C. set work.dept1 work.dept2
                          (rename = jcode = jobcode sal = salary);
D. set work.dept1
work.dept2         (rename = (jcode jobcode) (sal salary));

data WORK.NEW;
set WORK.OLD;
Count+1;
run;
The varaible Count is created using a sum statement. Which statement regarding this variable is true?
A. It is assigned a value 0 when the data step begins execution.
B. It is assigned a value of missing when the data step begins execution.
C. It is assigned a value 0 at compile time.
D. It is assigned a value of missing at compile time.
Answer: B
Answer: C
Question 154
Question 196
Which one of the following is true of the RETAIN statement in a SAS DATA step program?

A. It can be used to assign an initial value to _N_ .
B. It is only valid in conjunction with a SUM function.
C. It has no effect on variables read with the SET, MERGE and UPDATE statements.
D. It adds the value of an expression to an accumulator variable and ignores missing values.
Which one of the following is true of the SUM statement in a SAS DATA step program?

A. It is only valid in conjunction with a SUM function.
B. It is not valid with the SET, MERGE and UPDATE statements.
C. It adds the value of an expression to an accumulator variable and ignores missing values.
D. It does not retain the accumulator variable value from one iteration of the SAS DATA step to the next.

Answer: C
Answer : C
Question 152
Question 153
The following SAS program is submitted:
data work.test;
First = 'Ipswich, England';
City_Country = substr(First,1,7)!!', '!!'England'; run;
Which one of the following is the length of the variable CITY_COUNTRY in the output data set?
A. 6
B. 7
C. 17
D. 25

The following SAS program is submitted:
data work.test;
First = 'Ipswich, England';
City = substr(First,1,7);
City_Country = City!!', '!!'England'; run;
Which one of the following is the value of the variable CITY_COUNTRY in the output data set?
A. Ipswich!!
B. Ipswich, England
C. Ipswich, 'England'
D. Ipswic h, England
Answer: D
Answer: B