SAS Programming 2 Data Manipulation Techniques - Quizzes PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 92

SAS® Programming 2: Data

Manipulation Techniques –
Lesson Quizzes

Course Notes
SAS® Programming 2: Data Manipulation Techniques – Lesson Quizzes Course Notes was
developed by NULL. Additional contributions were made by NULL. Instructional design, editing, and
production support was provided by the Learning Design and Development team.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or
trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration.
Other brand and product names are trademarks of their respective companies.

SAS® Programming 2: Data Manipulation Techniques – Lesson Quizzes Course Notes

Copyright © 2019 SAS Institute Inc. Cary, NC, USA. All rights reserved. Printed in the United States
of America. No part of this publication may be reproduced, stored in a retrieval system, or
transmitted, in any form or by any means, electronic, mechanical, photocopying, or otherwise,
without the prior written permission of the publisher, SAS Institute Inc.

Book code NULL, course code PG294LQ, prepared date 24Apr2019. PG294LQ_001

ISBN NULL
For Your Infor mation iii

Table of Contents

Lesson 1 Lesson Quizzes...................................................................................1-1

1.1 Quizzes ...........................................................................................................1-3

Lesson 1 – Controlling DATA Step Processing...............................................1-3

Lesson 2 – Summarizing Data .....................................................................1-8

Lesson 3 – Manipulating Data with Functions .............................................. 1-13

Lesson 4 – Creating Custom Formats......................................................... 1-18

Lesson 5 – Combining Tables .................................................................... 1-23

Lesson 6 – Processing Repetitive Code...................................................... 1-28

Lesson 7 – Restructuring Tables ................................................................ 1-33

1.2 Solutions ....................................................................................................... 1-38

Lesson 1 .................................................................................................. 1-38

Lesson 2 .................................................................................................. 1-46

Lesson 3 .................................................................................................. 1-53

Lesson 4 .................................................................................................. 1-59

Lesson 5 .................................................................................................. 1-65

Lesson 6 .................................................................................................. 1-74

Lesson 7 .................................................................................................. 1-79


iv For Your Information

To learn more…
For information about other courses in the curriculum, contact the
SAS Education Division at 1-800-333-7660, or send e-mail to
training@sas.com. You can also find this information on the web at
http://support.sas.com/training/ as well as in the Training Course
Catalog.

For a list of SAS books (including e-books) that relate to the topics
covered in this course notes, visit https://www.sas.com/sas/books.html or
call 1-800-727-0025. US customers receive free shipping to US
addresses.
Lesson 1 Lesson Quizzes
1.1 Quizzes........................................................................................................................ 1-3
Lesson 1 – Controlling DATA Step Processing............................................................. 1-3
Lesson 2 – Summarizing Data .................................................................................. 1-8
Lesson 3 – Manipulating Data with Functions ............................................................ 1-13
Lesson 4 – Creating Custom Formats ...................................................................... 1-18
Lesson 5 – Combining Tables ................................................................................. 1-23
Lesson 6 – Processing Repetitive Code ................................................................... 1-28
Lesson 7 – Restructuring Tables.............................................................................. 1-33

1.2 Solutions ................................................................................................................... 1-38


Lesson 1 ............................................................................................................. 1-38
Lesson 2 ............................................................................................................. 1-46
Lesson 3 ............................................................................................................. 1-53
Lesson 4 ............................................................................................................. 1-59
Lesson 5 ............................................................................................................. 1-65
Lesson 6 ............................................................................................................. 1-74
Lesson 7 ............................................................................................................. 1-79
1-2 Lesson 1 Lesson Quizzes

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.1 Quizzes 1-3

1.1 Quizzes
Lesson 1 – Controlling DATA Step Processing

1. Which statement is false concerning the compilation phase of the DATA


step?

a. Initial values are assigned to the columns.


b. The program data vector (PDV) is created.
c. The DATA step is checked for syntax errors.
d. The descriptor portion of the output table is created.

3
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

2. Which statement is not a compile-time-only statement?

a. KEEP
b. LENGTH
c. SET
d. WHERE

5
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-4 Lesson 1 Lesson Quizzes

3. Which statement is true concerning the execution phase of the DATA


step?

a. Data is processed in the program data vector (PDV).


b. An implied OUTPUT occurs at the top of the DATA step.
c. An implied REINITIALIZE occurs at the bottom of the DATA step.
d. Columns read from the input table are set to missing when SAS returns
to the top of the DATA step.

7
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

4. The DATA step debugger in SAS Enterprise Guide can be used with DATA
and PROC steps.

a. True
b. False

9
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.1 Quizzes 1-5

5. Which PUTLOG statements create the following results in the SAS log?
Name=Alfred Height=69 Weight=112.5 Ratio=0.61 _ERROR_=0 _N_=1
Ratio=0.61

a. putlog all; putlog Ratio;


b. putlog all; putlog Ratio=;
c. putlog _all_; putlog Ratio;
d. putlog _all_; putlog Ratio=;

11
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

6. How many rows and columns are in the output table Shi ppingZones
given the following information?
data ShippingZones;
set Shipping;
The input table Shi pping Zone=1;
contains 5 rows and 3 columns output;
Zone=2;
(Product, BoxSize, and Ra te).
Rate=(Rate*1.5);
run;

a. 5 rows and 3 columns


b. 5 rows and 4 columns
c. 10 rows and 3 columns
d. 10 rows and 4 columns
13
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-6 Lesson 1 Lesson Quizzes

7. Which statement is true given the following information?

data Females Other;


s a shelp.class contains 19 rows: set sashelp.class;
if sex='F' then
9 rows with a s ex value of F and output Females;
10 rows with a s ex value of M. output Other;
run;

a. The Other table contains 10 rows.


b. The Other table contains 19 rows.
c. The F emales table contains 10 rows.
d. The F emales table contains 19 rows.

15
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

8. Which statement is false?

a. The KEEP statement names the columns to include from the input table.
b. The DROP statement names the columns to exclude from the output
table.
c. The KEEP= option in the DATA statement names the columns to include
in the output table.
d. The DROP= option in the SET statement names the columns to exclude
from being read into the PDV.

17
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.1 Quizzes 1-7

9. Which columns are in the final table work.boots?

data work.boots(drop=Product);
set sashelp.shoes(keep=Product Subsidiary Sales Inventory);
where Product='Boot';
drop Sales Inventory;
Total=sum(Sales,Inventory);
run;

a. Subs idiary
b. Subs idiary and Total
c. Product and Subs idiary
d. Product, Subsidiary, Sales, and Inventory

19
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

10. What is the result of running the following DATA step?

data work.boots;
set sashelp.shoes(keep=Sales Product Subsidiary);
where Product='Boot';
NewSales=Sales*1.25;
run;

a. The step produces work.boots with three columns.


b. The step produces work.boots with four columns.
c. The step produces an error due to invalid syntax for the KEEP= option.
d. The step produces an error because the Sa l es column is not read in from
the s a shelp.shoes table.
21
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-8 Lesson 1 Lesson Quizzes

Lesson 2 – Summarizing Data

1. Which statement contains valid syntax for the RETAIN statement?

a. retain year 2018;


b. retain year*2018;
c. retain year=2018;
d. retain year{2018};

24
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

2. Which statement is false concerning the sum statement?

a. The sum statement ignores missing values.


b. The sum statement initially sets the accumulator column to missing.
c. The sum statements adds a numeric value to an accumulator column.
d. The sum statement automatically retains the value of the accumulating
column.

26
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.1 Quizzes 1-9

3. What is the value of Count at the end of the third DATA step iteration?
w o rk.nums
data newnums;
set nums;
retain Count 100;
Count+Tens;
run;

a. . (missing)
b. 60
c. 130
d. 160

28
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

4. What is the value of Count at the end of the third DATA step iteration?
w o rk.nums
data newnums;
set nums;
Count+Tens;
run;

a. . (missing)
b. 20
c. 30
d. 70

30
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-10 Lesson 1 Lesson Quizzes

5. Which step executes successfully without an error, given the input table
s a shelp.class?

data new;
a. data new; b. set sashelp.class;
set sashelp.class; where Sex='M';
Ratio=Height/Weight; Ratio=Height/Weight;
where Sex='M' & Ratio>0.6; where Ratio>0.6;
run; run;

c. data new; d. data new;


set sashelp.class; set sashelp.class;
where Sex='M’; if Sex='M’;
Ratio=Height/Weight; Ratio=Height/Weight;
if Ratio>0.6; where Ratio>0.6;
run; run;
32
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

6. Which statement is true given the following program?

data work.student;
set sashelp.class;
by Name;
run;

a. The PDV contains a temporary variable named F i rst.Name.


b. The output table work.student contains a column named La s t.Name.
c. The DATA step sorts the input table by the column N a me.
d. An error is produced because the BY statement is not permitted in the
DATA step.

34
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.1 Quizzes 1-11

7. What are the correct values for F i rst.Name and La st.Name if the value of
N a me appears only once in the input table?

data work.student;
set sashelp.class;
by Name;
run;

a. F i rst.Name=0 and La s t.Name=0


b. F i rst.Name=1 and La s t.Name=1
c. F i rst.Name=1 and La s t.Name=0
d. F i rst.Name=0 and La s t.Name=1

36
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

8. Which DATA step statement indicates to continue processing the last row
of a BY group?

a. if First.JobTitle;
b. if Last.JobTitle;
c. where First.JobTitle=1;
d. where Last.JobTitle=1;

38
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-12 Lesson 1 Lesson Quizzes

9. Which statement needs to be added to the DATA step to reset the value
of TotalWt for each new BY group?

data GenderWeight;
set Students;
by Gender;
… add statement here …
TotalWt+Weight;
if Last.Gender=1 then output;
run;
a. TotalWt=0;
b. if Last.Gender=0 then TotalWt=0;
c. if First.Gender=0 then TotalWt=0;
d. if First.Gender=1 then TotalWt=0;
40
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

10. What are the values for F i rst.City and La st.City for the third row of the
input table given the following information?
data StatePopulation;
set Population;
by State City;
run;

a. F i rst.City=0 and La s t.City=0


b. F i rst.City=1 and La s t.City=0
c. F i rst.City=0 and La s t.City=1
d. F i rst.City=1 and La s t.City=1

42
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.1 Quizzes 1-13

Lesson 3 – Manipulating Data with Functions

1. Functions and CALL routines both return a value that must be used in an
assignment statement or expression.

a. True
b. False

45
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

2. Which function calculates the average of the columns W eek1, Week2,


W eek3, and W eek4?

a. mean(Week1, Week4)
b. mean(Week1-Week4)
c. mean(of Week1, Week4)
d. mean(of Week1-Week4)

47
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-14 Lesson 1 Lesson Quizzes

3. Which expression rounds each value of Sa l es to the nearest hundredth


(or two decimal places)?

a. round(Sales)
b. round(Sales, 2)
c. round(Sales, .01)
d. round(Sales, dollar10.2)

49
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

4. Which function could be used to remove the non-numeric symbols in


Phone?
a. COMPRESS Phone

b. COMPBL 202-555-0190
202.555.0110
c. SCAN
(202)555-0133
d. FIND
[ 202]555-0128

51
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.1 Quizzes 1-15

5. Which statement reads Ci tyCountry and correctly assigns a value to


Country?

a. Country=scan(CityCountry, 2); CityCountry Country


b. Country=scan(CityCountry, -1); A thens, Greece Gre ece
c. Country=scan(CityCountry, 2, ','); Ne w Delhi, India India
d. Country=scan(CityCountry, 2, ', '); A uckland, New Zealand New Zealand

53
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

6. Which expression creates Ci tyCountry?

a. cat(City, ", ", Country)


City Country CityCountry
b. cats(", ", City, Country)
A thens Gre ece A thens, Greece
c. catx(City, ", ", Country) Ne w Delhi India Ne w Delhi, India
d. catx(", ", City, Country) A uckland New Zealand A uckland, New Zealand

55
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-16 Lesson 1 Lesson Quizzes

7. How many rows are written to output based on the following statement?

if find(Location, "Oahu", "i") > 0 then output;

a. 0 Location
Honolulu, Oahu
b. 1
Ka anapali, Maui
c. 3
Hilo, Hawaii
d. 5
ka ilua, oahu
LA IE, OAHU

57
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

8. Which of the following functions can convert the values of the numeric
variable Level to character values?

a. put(Level, 3.)
b. put(3., Level)
c. input(3., Level)
d. input(Level, 3.)

59
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.1 Quizzes 1-17

9. Which of the following functions converts the character values of Ba s e to


numeric values?

a. put(comma10.2, Base)
b. put(Base, comma10.2)
c. input(Base, comma10.2)
d. input(comma10.2, Base)

61
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

10. Which step is not required when converting a character column named
Da te to a numeric SAS date column with the same name?
a. Rename the Da te column to a new name, such as Cha rDate.
b. Use the INPUT function to read the renamed Cha rDate character
column and create a numeric column named Da te.
c. Specify an appropriate informat in the INPUT function.
d. Format the new numeric Da te column.

63
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-18 Lesson 1 Lesson Quizzes

Lesson 4 – Creating Custom Formats

1. Which of the following contains valid syntax?

value grades 'A'='Excellent'


a. . 'B'='Good';

value qtrfmt 1,2,3='First'


b. . 4,5,6='Second';

value $grades. 'A'='Excellent'


c. .
'B'='Good';

d. .value qtrfmt '1'-'3'='First'


'4'-'6'='Second';

66
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

2. What is the formatted value for the value of 100 given the following
step?
proc format;
value rates 1-<100='low'
100<-<200='medium'
200-300 ='high'
other ='out of range';
run;

a. low
b. medium
c. high
d. out of range
68
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.1 Quizzes 1-19

3. In the FORMAT procedure, you specify the name of the format and the
name of the column that will use the custom format.

a. True
b. False

70
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

4. What is the correct formatted output given the following PROC FORMAT
step and the input table?
proc format;
value $answer '1'='Yes'
'2'='No'
'other'='Not Answered';
run;

a. b. c. d.

72
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-20 Lesson 1 Lesson Quizzes

5. Which one of the following does not have proper syntax for specifying a
range in the VALUE statement?

a. 500>-700
b. 500-<700
c. 'A'-'C'
d. 'Horse'-'Mouse'

74
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

6. Which statement is true concerning options for the FORMAT procedure?

a. The FMTLIB option goes in the SELECT statement.


b. The CNTLIN= option goes in the VALUE statement.
c. The FMTLIB option specifies the library to store the format.
d. The CNTLIN= option specifies a table from which formats are built.

76
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.1 Quizzes 1-21

7. Which columns are required in an input table to create a format based


on numeric ranges?

a. FMTNAME, START, and LABEL


b. FORMAT, START, END, and NAME
c. FMTNAME, START, END, and LABEL
d. FORMAT, START, LAST, NAME, and TYPE

78
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

8. Which option in the PROC FORMAT statement specifies a library to store


a custom format?

a. CATALOG=
b. FMTLIB=
c. LIBRARY=
d. STORE=

80
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-22 Lesson 1 Lesson Quizzes

9. What is the default search order that is used to locate formats?

a. LIBRARY.FORMATS  WORK.FORMATS
b. SASHELP.FORMATS  LIBRARY.FORMATS
c. SASHELP.FORMATS  WORK.FORMATS
d. WORK.FORMATS  LIBRARY.FORMATS

82
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

10. Which of the following contains valid syntax for the FMTSEARCH=
option?

a. options fmtsearch=sashelp;
b. options fmtsearch=sashelp.formats;
c. options fmtsearch=(sashelp sashelp.fmts);
d. options fmtsearch=[sashelp.fmts sashelp];

84
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.1 Quizzes 1-23

Lesson 5 – Combining Tables

1. Which statement is true concerning concatenating tables?

a. All tables must have the same number of columns.


b. Columns in all tables must have matching names and lengths.
c. Tables must be in the same library.
d. Missing values are generated for columns that exist in one input table and
not in another.

87
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

2. Which statement renames the existing column Location in work.travel


as Des tination?

a. set vacations(rename=(Location=Destination)) travel;

b. set vacations travel(rename=(Destination=Location));

c. set vacations travel(rename=(Location=Destination));

d. Aset vacations travel(rename(Destination=Location));

89
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-24 Lesson 1 Lesson Quizzes

3. Which statement is true concerning merging with matching rows?

a. The MERGE statement must refer to temporary input tables.


b. The columns in the BY statement can be in only one of the tables.
c. Only two input tables can be specified in the MERGE statement.
d. The input tables must be sorted by the columns in the BY statement.

91
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

4. How many rows are in the output table given the following?
work.agetable w ork.foodtable
data both;
merge agetable
foodtable;
by Name;
run;

a. two
b. three
c. four
d. five

93
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.1 Quizzes 1-25

5. How many rows are in the output table given the following?
w ork.empsau work.phonec

a. two
data empsauc;
b. three merge empsau phonec;
by EmpID;
c. four run;
d. five

95
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

6. What is the result of the following step?


w ork.donors1 w ork.donors2
data combine;
merge donors1 donors2;
by ID;
run;

a. The table com bine is created with four columns and five rows.
b. The step fails because the BY column ID is not properly sorted.
c. The step fails because U ni ts is in both tables and not the BY column.
d. The step fails because of duplicate ID values within the donors1 table.

97
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-26 Lesson 1 Lesson Quizzes

7. Which statement best describes the rows in the output table?


data bonuses;
merge managers(in=M)
staff(in=S);
by EmpID;
if M=0 and S=1;
run;

a. all the matching rows from both m a nagers and s taff


b. only the rows from m a nagers that have no match in s taff
c. only the rows from s taff that have no match in m a nagers
d. all the matching and nonmatching rows from both m a nagers and s taff

99
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

8. What are the values of C and A during the third iteration of the DATA
step?
w ork.clients work.amounts
data client_amount;
merge clients(in=C)
amounts(in=A);
by Name;
run;

a. C=0 and A =0
b. C=1 and A =0
c. C=0 and A =1
d. C=1 and A =1
101
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.1 Quizzes 1-27

9. What is the value of Location in the output table?


loc1 loc2 loc3
C o de L o cation C o de L o cation Co de L o cation
A Fr ance A B elgium A I t aly

a. Italy
data locALL;
b. France merge loc1 loc2 loc3;
c. Belgium by Code;
run;
d. France Belgium Italy

103
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

10. The DATA step can be used to merge the following three tables:

dataset1 dataset2 dataset3


Student Test Sc ore Su bject Test Su bject Av gScore

a. True
b. False

105
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-28 Lesson 1 Lesson Quizzes

Lesson 6 – Processing Repetitive Code

1. Which output table is produced from the following step?


data Earnings(keep=Qtr Earned);
Amount=1000; Rate=.075/4;
do Qtr=1 to 4;
Earned+(Amount+Earned)*Rate;
end;
run;

a. b.

c. d.

108
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

2. Which statement is true regarding the iterative DO loop?

DO index-column = start TO stop <BY increment> ;

a. The start and stop values can be character or numeric values.


b. If an increment value is not specified, the default increment is 0.
c. The index column is incremented at the bottom of each DO loop.
d. The index column is not in the final table unless specifically kept.

110
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.1 Quizzes 1-29

3. How many rows are in the output table given the following?

pg 2.savings data work.savings;


set pg2.savings;
Savings=0;
do Year=1 to 5;
do qtr=1 to 4;
Savings+Amount;
Savings+(Savings*0.02/12);
end;
end;
a. 1 run;
b. 4
c. 5
d. 20
112
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

4. What is the final value of Yea r given the following step?

data invest;
do Year=2010 to 2019;
Capital+5000;
Capital+(Capital*.03);
end;
run;

a. . (missing)
b. 2010
c. 2019
d. 2020

114
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-30 Lesson 1 Lesson Quizzes

5. Which of the following statements contains valid syntax?

a. do 1 to 10 by 2;
b. do while (Year>2025);
c. do until Earnings<=100000;
d. do date='01JAN2019' to '31JAN2019';

116
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

6. How many rows are in the output table given the following?

work.bikeinfo data bikeinfo2;


set bikeinfo;
do month=1 to 3;
do week=1 to 4;
bike=bike+2;
end;
output;
end;
a. 2 run;
b. 3
c. 6
d. 12
e. 24
118
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.1 Quizzes 1-31

7. What is the value of x at the completion of the DATA step?

data test;
x=15;
do until(x>12);
x+1;
end;
run;

a. . (missing)
b. 13
c. 15
d. 16

120
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

8. Which statement is false?

a. The DO UNTIL loop executes until a condition is true.


b. The DO WHILE loop always executes at least one time.
c. The DO WHILE loop checks the condition at the top of the loop.
d. The DO UNTIL loop checks the condition at the bottom of the loop.

122
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-32 Lesson 1 Lesson Quizzes

9. Which of the following statements contains valid syntax?

a. do Age=10 to 14 and while (Weight<150);


b. do week=1 to 52 do until (Mileage ge 2750);
c. do Increase=5 to 10 while (temperature lt 102);
d. do Year=2018 to 2028 or until (Earnings<=100000);

124
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

10. Which output table is produced from the following step?

data test;
bike=10;
do day=1 to 7 while (bike lt 13);
bike=bike+2;
end;
run;

a. b.

c. d.

126
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.1 Quizzes 1-33

Lesson 7 – Restructuring Tables

1. Which is the better description for the following table?

a. wide table
b. narrow table

129
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

2. Which statement is needed for creating multiple rows from a single row
when using the DATA step to go from a wide to a narrow table?

a. WIDE
b. NARROW
c. RETAIN
d. OUTPUT

131
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-34 Lesson 1 Lesson Quizzes

3. How many rows will be in the final table if work.airwide contains three
rows?
data work.airnarrow;
set work.airwide;
a. 3 Month='Jan';
Air=Jan;
b. 6 output;
Month='Feb';
c. 9 Air=Feb;
d. 12 output;
Month='Mar';
Air=Mar;
output;
keep Year Month Air;
run;

133
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

4. When using the DATA step to go from a narrow table to a wide table, the
KEEP statement is needed to hold values in the PDV across multiple
iterations of the DATA step.

a. True
b. False

135
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.1 Quizzes 1-35

5. Which statement needs to be added to the DATA step to include only the
last row per Yea r in the output table?
data work.airwide2(keep=Year Jan Feb Mar);
set work.airnarrow;
by Year;
retain Jan Feb Mar;
if Month='Jan' then Jan=Air;
else if Month='Feb' then Feb=Air;
else if Month='Mar' then Mar=Air;
... insert statement here ...
run;
a. output;
b. if Last then output;
c. if Last.Year=1 then output;
d. if Last.Year=0 then output;
137
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

6. Which statement is false concerning the TRANSPOSE procedure?

a. Columns are transposed into rows.


b. By default, numeric columns are transposed.
c. Use a BY statement to sort the data while transposing.
d. Use a VAR statement to specifically specify the character and numeric
columns to transpose.

139
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-36 Lesson 1 Lesson Quizzes

7. Which statements are needed in a PROC TRANSPOSE step for the


following example (narrow  wide)?

a. by Day; b. id Day;
var Meal Food; var Food Meal;

c. by Day; d. by Day;
id Food; id Meal;
var Meal; var Food;
141
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

8. Which statement or statements are needed in a PROC TRANSPOSE step


for the following example (wide  narrow)?

a. by Day; b. var Breakfast Lunch Dinner;

c. id Day; d. id Day;
var Breakfast Lunch Dinner;

143
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.1 Quizzes 1-37

9. Which option is needed in the PROC TRANSPOSE statement to rename


the _N AME_ column?

a. _name_=Meal
b. name=Meal
c. prefix=Meal
d. rename=Meal

145
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

10. Which option is needed in the PROC TRANSPOSE statement to rename


the COL columns?

a. out=meals2(COL1=Day_7 COL2=Day_1)
b. out=meals2(name=(COL1=Day_7 COL2=Day_1))
c. out=meals2(rename=(COL1=Day_7 COL2=Day_1))
d. out=meals2(prefix=(COL1=Day_7 COL2=Day_1))

147
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-38 Lesson 1 Lesson Quizzes

1.2 Solutions
Lesson 1

1. Which statement is false concerning the compilation phase of the DATA


step?

a. Initial values are assigned to the columns.


b. The program data vector (PDV) is created.
c. The DATA step is checked for syntax errors.
d. The descriptor portion of the output table is created.

4
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Initial values are assigned to columns at the beginning of the execution phase.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.2 Solutions 1-39

2. Which statement is not a compile-time-only statement?

a. KEEP
b. LENGTH
c. SET
d. WHERE

6
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

At execution time, the SET statement is processed to read data into the PDV. The compile-time
statements of KEEP, LENGTH, and WHERE are not processed at execution time. The rules of these
statements are processed in the compilation phase so that their impact will be observed in the output
table.

3. Which statement is true concerning the execution phase of the DATA


step?

a. Data is processed in the program data vector (PDV).


b. An implied OUTPUT occurs at the top of the DATA step.
c. An implied REINITIALIZE occurs at the bottom of the DATA step.
d. Columns read from the input table are set to missing when SAS returns
to the top of the DATA step.

8
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

During execution, data manipulation occurs in the PDV. An implied OUTPUT and RETURN (not
REINITIALIZE ) occurs at the bottom of the DATA step. When SAS returns to the top of the DATA
step, columns read from the input table are retained and computed columns are set to missing.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-40 Lesson 1 Lesson Quizzes

4. The DATA step debugger in SAS Enterprise Guide can be used with DATA
and PROC steps.

a. True
b. False

10
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The Data step debugger in SAS Enterprise Guide works only with DATA steps.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.2 Solutions 1-41

5. Which PUTLOG statements create the following results in the SAS log?
Name=Alfred Height=69 Weight=112.5 Ratio=0.61 _ERROR_=0 _N_=1
Ratio=0.61

a. putlog all; putlog Ratio;


b. putlog all; putlog Ratio=;
c. putlog _all_; putlog Ratio;
d. putlog _all_; putlog Ratio=;

12
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

_ALL_ is a keyword to show all of the contents of the PDV. Ratio= writes out the column name, an
equal sign, and the value of Ratio. Ratio writes out only the value.
data new;
set sashelp.class(keep=Name Height Weight);
Ratio=round(Height/Weight,.01);
putlog all; putlog Ratio=;
run;

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-42 Lesson 1 Lesson Quizzes

6. How many rows and columns are in the output table Shi ppingZones
given the following information?
data ShippingZones;
set Shipping;
The input table Shi pping Zone=1;
contains 5 rows and 3 columns output;
Zone=2;
(Product, BoxSize, and Ra te).
Rate=(Rate*1.5);
run;

a. 5 rows and 3 columns


b. 5 rows and 4 columns
c. 10 rows and 3 columns
d. 10 rows and 4 columns
14
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The explicit OUTPUT statement is sending the ZONE=1 rows to the output table. There is no explicit
OUTPUT statement after ZONE=2, so those rows are not making it to the output table. An implicit
OUTPUT is not at the bottom of the DATA step due to the explicit OUTPUT. The four columns are
Product, BoxSize, Rate, and Zone.
Note: There were 5 observations read from the data set WORK.SHIPPING.
Note: The data set WORK.SHIPPINGZONES has 5 observations and 4 variables.
data Shipping;
input Product $ BoxSize Rate;
datalines;
A 5 10
B 6 11
C 7 12
D 8 13
E 9 13
;

data ShippingZones;
set Shipping;
Zone=1;
output;
Zone=2;
Rate=(Rate*1.5);
run;

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.2 Solutions 1-43

7. Which statement is true given the following information?

data Females Other;


s a shelp.class contains 19 rows: set sashelp.class;
if sex='F' then
9 rows with a s ex value of F and output Females;
10 rows with a s ex value of M. output Other;
run;

a. The Other table contains 10 rows.


b. The Other table contains 19 rows.
c. The F emales table contains 10 rows.
d. The F emales table contains 19 rows.

16
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The Females table will contain 9 rows and the Other table will contain 19 rows.

8. Which statement is false?

a. The KEEP statement names the columns to include from the input table.
b. The DROP statement names the columns to exclude from the output
table.
c. The KEEP= option in the DATA statement names the columns to include
in the output table.
d. The DROP= option in the SET statement names the columns to exclude
from being read into the PDV.

18
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The KEEP statement controls which columns are in the output table.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-44 Lesson 1 Lesson Quizzes

9. Which columns are in the final table work.boots?

data work.boots(drop=Product);
set sashelp.shoes(keep=Product Subsidiary Sales Inventory);
where Product='Boot';
drop Sales Inventory;
Total=sum(Sales,Inventory);
run;

a. Subs idiary
b. Subs idiary and Total
c. Product and Subs idiary
d. Product, Subsidiary, Sales, and Inventory

20
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The column Subsidiary from the input table and the calculated column Total are in the final table.
Product, Sales, and Inventory are dropped.
Note: The data set WORK.BOOTS has 52 observations and 2 variables.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.2 Solutions 1-45

10. What is the result of running the following DATA step?

data work.boots;
set sashelp.shoes(keep=Sales Product Subsidiary);
where Product='Boot';
NewSales=Sales*1.25;
run;

a. The step produces work.boots with three columns.


b. The step produces work.boots with four columns.
c. The step produces an error due to invalid syntax for the KEEP= option.
d. The step produces an error because the Sa l es column is not read in from
the s a shelp.shoes table.
22
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

work.boots is created with the columns of Product, Subsidiary, NewSales, and Sales. The values of
NewSales and Sales are missing. Sales is uninitialized because the value was not read in from the
input table.
Note: Variable Sales is uninitialized.
Note: Missing values were generated as a result of performing an operation on missing values.
Note: There were 52 observations read from the data set SASHELP.SHOES.
WHERE Product='Boot';
Note: The data set WORK.BOOTS has 52 observations and 4 variables.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-46 Lesson 1 Lesson Quizzes

Lesson 2

1. Which statement contains valid syntax for the RETAIN statement?

a. retain year 2018;


b. retain year*2018;
c. retain year=2018;
d. retain year{2018};

25
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Year is the column to retain with an initial value of 2018. A space goes between the retain column
and the initial value.

2. Which statement is false concerning the sum statement?

a. The sum statement ignores missing values.


b. The sum statement initially sets the accumulator column to missing.
c. The sum statements adds a numeric value to an accumulator column.
d. The sum statement automatically retains the value of the accumulating
column.

27
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The sum statement ignores missing values, initially sets the accumulator column to 0, adds a
numeric value to an accumulator column, and automatically retains the value of the accumulator
column.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.2 Solutions 1-47

3. What is the value of Count at the end of the third DATA step iteration?
w o rk.nums
data newnums;
set nums;
retain Count 100;
Count+Tens;
run;

a. . (missing)
b. 60
c. 130
d. 160

29
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The initial value of Count is 100. The end of the first iteration is 100+10=110. The end of the second
iteration is 110+20=130. The end of the third iteration is 130+30=160.
data nums;
input Tens;
datalines;
10
20
30
40
;
run;

data newnums;
set nums;
retain Count 100;
Count+Tens;
run;

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-48 Lesson 1 Lesson Quizzes

4. What is the value of Count at the end of the third DATA step iteration?
w o rk.nums
data newnums;
set nums;
Count+Tens;
run;

a. . (missing)
b. 20
c. 30
d. 70

31
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The initial value of Count is 0. The end of the first iteration is 0+10=10. The end of the second
iteration is 10+20=30. The end of the third iteration is 30+.=30. The sum statement ignores missing
values.
data nums;
input Tens;
datalines;
10
20
.
40
;
run;

data newnums;
set nums;
retain Count 100;
Count+Tens;
run;

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.2 Solutions 1-49

5. Which step executes successfully without an error, given the input table
s a shelp.class?

data new;
a. data new; b. set sashelp.class;
set sashelp.class; where Sex='M';
Ratio=Height/Weight; Ratio=Height/Weight;
where Sex='M' & Ratio>0.6; where Ratio>0.6;
run; run;

c. data new; d. data new;


set sashelp.class; set sashelp.class;
where Sex='M’; if Sex='M’;
Ratio=Height/Weight; Ratio=Height/Weight;
if Ratio>0.6; where Ratio>0.6;
run; run;
33
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The WHERE statement subsets data as it is being read into the PDV. Ratio is not in the input table,
so Ratio cannot be specified in the WHERE statement. To subset Ratio, the condition must be
specified in an IF statement.

6. Which statement is true given the following program?

data work.student;
set sashelp.class;
by Name;
run;

a. The PDV contains a temporary variable named F i rst.Name.


b. The output table work.student contains a column named La s t.Name.
c. The DATA step sorts the input table by the column N a me.
d. An error is produced because the BY statement is not permitted in the
DATA step.

35
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The BY statement produces the columns First.Name and Last.Name in the PDV, assuming the
input table is sorted by Name. These columns will not be in the output table.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-50 Lesson 1 Lesson Quizzes

7. What are the correct values for F i rst.Name and La st.Name if the value of
N a me appears only once in the input table?

data work.student;
set sashelp.class;
by Name;
run;

a. F i rst.Name=0 and La s t.Name=0


b. F i rst.Name=1 and La s t.Name=1
c. F i rst.Name=1 and La s t.Name=0
d. F i rst.Name=0 and La s t.Name=1

37
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

If First.Name and Last.Name are both equal to 1, the Name value is unique to that row. It is the first
time and the last time that the Name value appears.

8. Which DATA step statement indicates to continue processing the last row
of a BY group?

a. if First.JobTitle;
b. if Last.JobTitle;
c. where First.JobTitle=1;
d. where Last.JobTitle=1;

39
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

This statement is true if Last.Jobtitle is equal to a nonzero or nonmissing value. Therefore, this
statement is true when Last.Jobtitle is equal to 1.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.2 Solutions 1-51

9. Which statement needs to be added to the DATA step to reset the value
of TotalWt for each new BY group?

data GenderWeight;
set Students;
by Gender;
… add statement here …
TotalWt+Weight;
if Last.Gender=1 then output;
run;
a. TotalWt=0;
b. if Last.Gender=0 then TotalWt=0;
c. if First.Gender=0 then TotalWt=0;
d. if First.Gender=1 then TotalWt=0;
41
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

First.Gender=1 represents the beginning of each new BY group. When First.Gender is equal to 1,
the value of the accumulator column needs to be set to 0.
proc sort data=sashelp.class out=students(rename=(Sex=Gender));
by Sex;
run;

data GenderWeight;
set Students;
by Gender;
if First.Gender=1 then TotalWt=0;
TotalWt+Weight;
if Last.Gender=1 then output;
run;

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-52 Lesson 1 Lesson Quizzes

10. What are the values for F i rst.City and La st.City for the third row of the
input table given the following information?
data StatePopulation;
set Population;
by State City;
run;

a. F i rst.City=0 and La s t.City=0


b. F i rst.City=1 and La s t.City=0
c. F i rst.City=0 and La s t.City=1
d. F i rst.City=1 and La s t.City=1

43
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The values of First.City and Last.City are dependent on the values of State. For the third row,
First.City is equal to 1 because that is the first time that Greenville appears within NC. Last.City is
equal to 1 because that is the last time Greenville appears within NC.
State=NC City=Cary Population=162320 FIRST.State=1 LAST.State=0 FIRST.City=1 LAST.City=1
_ERROR_=0 _N_=1
State=NC City=Durham Population=263016 FIRST.State=0 LAST.State=0 FIRST.City=1
LAST.City=1 _ERROR_=0 _N_=2
State=NC City=Greenville Population=91495 FIRST.State=0 LAST.State=1 FIRST.City=1
LAST.City=1 _ERROR_=0 _N_=3
State=SC City=Greenville Population=67453 FIRST.State=1 LAST.State=0 FIRST.City=1
LAST.City=1 _ERROR_=0 _N_=4
State=SC City=Sumter Population=40723 FIRST.State=0 LAST.State=1 FIRST.City=1 LAST.City=1
_ERROR_=0 _N_=5

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.2 Solutions 1-53

data Population;
input State $ City $11. Population;
datalines;
NC Cary 162320
NC Durham 263016
NC Greenville 91495
SC Greenville 67453
SC Sumter 40723
;
run;

data StatePopulation;
set Population;
by State City;
put _all_;
run;

Lesson 3

1. Functions and CALL routines both return a value that must be used in an
assignment statement or expression.

a. True
b. False

46
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

A function returns a value that must be used in an assignment statement or expression, but a CALL
routine alters existing column values or performs other system functions.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-54 Lesson 1 Lesson Quizzes

2. Which function calculates the average of the columns W eek1, Week2,


W eek3, and W eek4?

a. mean(Week1, Week4)
b. mean(Week1-Week4)
c. mean(of Week1, Week4)
d. mean(of Week1-Week4)

48
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Numeric column lists are specified with a hyphen between the first and last columns in the range.
The keyword OF must be used if a column list is used as an argument in a function.

3. Which expression rounds each value of Sa l es to the nearest hundredth


(or two decimal places)?
a. round(Sales)
b. round(Sales, 2)
c. round(Sales, .01)
d. round(Sales, dollar10.2)

50
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Use the second argument in the ROUND function to specify the rounding unit.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.2 Solutions 1-55

4. Which function could be used to remove the non-numeric symbols in


Phone?

a. COMPRESS Phone

b. COMPBL 202-555-0190
202.555.0110
c. SCAN
(202)555-0133
d. FIND
[ 202]555-0128

Phone=compress(Phone, '-.()[] ');

or
Phone=compress(Phone,,'kd');
52
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The second argument of the COMPRESS function can be used to specify all symbols to remove
from the values of Phone. The optional third argument of COMPRESS could further simplify this
code by working with entire character classes instead of having to specify individual characters. For
example, the ‘d’ modifier specifies only digits, and the ‘k’ modifier instructs COMPRESS to keep the
specified items instead of removing them. So specifying ‘kd’ for the third argument causes
COMPRESS to keep only digits.

5. Which statement reads Ci tyCountry and correctly assigns a value to


Country?

a. Country=scan(CityCountry, 2); CityCountry Country


b. Country=scan(CityCountry, -1); A thens, Greece Gre ece
c. Country=scan(CityCountry, 2, ','); Ne w Delhi, India India
d. Country=scan(CityCountry, 2, ', '); A uckland, New Zealand New Zealand

54
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The SCAN function should return the second word using only the comma as a delimiter.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-56 Lesson 1 Lesson Quizzes

6. Which expression creates Ci tyCountry?

a. cat(City, ", ", Country)


City Country CityCountry
b. cats(", ", City, Country)
A thens Gre ece A thens, Greece
c. catx(City, ", ", Country) Ne w Delhi India Ne w Delhi, India
d. catx(", ", City, Country) A uckland New Zealand A uckland, New Zealand

56
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The CATX function concatenates strings together, removes leading and training blanks, and inserts
the separator that is defined as the first argument.

7. How many rows are written to output based on the following statement?

if find(Location, "Oahu", "i") > 0 then output;

a. 0 Location
Honolulu, Oahu
b. 1
Ka anapali, Maui
c. 3
Hilo, Hawaii
d. 5
ka ilua, oahu
LA IE, OAHU

58
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The “I” modifier as the third argument in the FIND function makes the search case insensitive.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.2 Solutions 1-57

8. Which of the following functions can convert the values of the numeric
variable Level to character values?

a. put(Level, 3.)
b. put(3., Level)
c. input(3., Level)
d. input(Level, 3.)

60
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The PUT function explicitly converts numeric values to character values. You specify the keyword
PUT followed by the variable name and then the format. The variable name and format are enclosed
in parentheses and separated by a comma.

9. Which of the following functions converts the character values of Ba s e to


numeric values?
a. put(comma10.2, Base)
b. put(Base, comma10.2)
c. input(Base, comma10.2)
d. input(comma10.2, Base)

62
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The INPUT function explicitly converts character values to numeric values. You specify the keyword
INPUT followed by the variable name and then the informat. The variable name and informat are
enclosed in parentheses and separated by a comma. A numeric informat is needed for character-to-
numeric conversions.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-58 Lesson 1 Lesson Quizzes

10. Which step is not required when converting a character column named
Da te to a numeric SAS date column with the same name?

a. Rename the Da te column to a new name, such as Cha rDate.


b. Use the INPUT function to read the renamed Cha rDate character
column and create a numeric column named Da te.
c. Specify an appropriate informat in the INPUT function.
d. Format the new numeric Da te column.

64
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Formatting the new column is not required but is recommended.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.2 Solutions 1-59

Lesson 4

1. Which of the following contains valid syntax?

value grades 'A'='Excellent'


a. . 'B'='Good';

value qtrfmt 1,2,3='First'


b. . 4,5,6='Second';

value $grades. 'A'='Excellent'


c. .
'B'='Good';

d. .value qtrfmt '1'-'3'='First'


'4'-'6'='Second';

67
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

This VALUE statement is creating a numeric format and numeric values are specified.
a. This VALUE statement is creating a numeric format but character values are specified.
ERROR: The quoted string 'A' is not acceptable to a numeric format or informat.
c. This VALUE statement contains a period in the format name.
ERROR 22-322: Syntax error, expecting one of the following: a name, $.
ERROR 200-322: The symbol is not recognized and will be ignored
d. This VALUE statement is creating a numeric format but character values are specified.
ERROR: The quoted string '1' is not acceptable to a numeric format or informat.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-60 Lesson 1 Lesson Quizzes

2. What is the formatted value for the value of 100 given the following
step?
proc format;
value rates 1-<100='low'
100<-<200='medium'
200-300 ='high'
other ='out of range';
run;

a. low
b. medium
c. high
d. out of range
69
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

100 falls into the OTHER category. 1-<100 is less than 100. 100<-<200 is greater than 100.

3. In the FORMAT procedure, you specify the name of the format and the
name of the column that will use the custom format.

a. True
b. False

71
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The format name is specified in the VALUE statement of PROC FORMAT. However, PROC
FORMAT has no reference to the table or column to be formatted. Those items appear in other steps
such as a FORMAT statement within PROC PRINT.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.2 Solutions 1-61

4. What is the correct formatted output given the following PROC FORMAT
step and the input table?
proc format;
value $answer '1'='Yes'
'2'='No'
'other'='Not Answered';
run;

a. b. c. d.

73
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The word OTHER is in quotation marks. Therefore, it is not seen as a keyword. A value of 3 is not
referenced by any of the values in the VALUE statement. The value 3 will be displayed as 3.
proc format;
value $answer '1'='Yes'
'2'='No'
'other'='Not Answered';
run;

data new;
input Code $;
datalines;
1
2
3
;
run;

proc print data=new noobs;


format Code $answer.;
run;

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-62 Lesson 1 Lesson Quizzes

5. Which one of the following does not have proper syntax for specifying a
range in the VALUE statement?

a. 500>-700
b. 500-<700
c. 'A'-'C'
d. 'Horse'-'Mouse'

75
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The greater than symbol is not valid for a range. The less than symbol can be used after the starting
value or before the ending value.

6. Which statement is true concerning options for the FORMAT procedure?

a. The FMTLIB option goes in the SELECT statement.


b. The CNTLIN= option goes in the VALUE statement.
c. The FMTLIB option specifies the library to store the format.
d. The CNTLIN= option specifies a table from which formats are built.

77
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

CNTLIN= specifies the table to read in order to create a format. This option goes in the PROC
FORMAT statement. The FMTLIB option goes in the PROC FORMAT statement and creates a report
containing information about your custom formats.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.2 Solutions 1-63

7. Which columns are required in an input table to create a format based


on numeric ranges?

a. FMTNAME, START, and LABEL


b. FORMAT, START, END, and NAME
c. FMTNAME, START, END, and LABEL
d. FORMAT, START, LAST, NAME, and TYPE

79
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

At a minimum, FMTNAME, START, and LABEL are required for a CNTLIN= table. END is also
needed for ranges.

8. Which option in the PROC FORMAT statement specifies a library to store


a custom format?

a. CATALOG=
b. FMTLIB=
c. LIBRARY=
d. STORE=

81
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The LIBRARY= option specifies the library or catalog (or both) that contains the formats that you are
creating. The FMTLIB= option creates a report containing information about your custom formats.
There is not a CATALOG= or STORE= option in the PROC FORMAT statement.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-64 Lesson 1 Lesson Quizzes

9. What is the default search order that is used to locate formats?

a. LIBRARY.FORMATS  WORK.FORMATS
b. SASHELP.FORMATS  LIBRARY.FORMATS
c. SASHELP.FORMATS  WORK.FORMATS
d. WORK.FORMATS  LIBRARY.FORMATS

83
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

By default, SAS first looks in WORK.FORMATS. If the format is not in that location, SAS looks in
LIBRARY.FORMATS.

10. Which of the following contains valid syntax for the FMTSEARCH=
option?

a. options fmtsearch=sashelp;
b. options fmtsearch=sashelp.formats;
c. options fmtsearch=(sashelp sashelp.fmts);
d. options fmtsearch=[sashelp.fmts sashelp];

85
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The FMTSEARCH= option must specify search criteria in parentheses. After looking in the default
locations, SAS looks in SASHELP.FORMATS and then SASHELP.FMTS.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.2 Solutions 1-65

Lesson 5

1. Which statement is true concerning concatenating tables?

a. All tables must have the same number of columns.


b. Columns in all tables must have matching names and lengths.
c. Tables must be in the same library.
d. Missing values are generated for columns that exist in one input table and
not in another.

88
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Tables with different columns can be concatenated, but missing values are generated for columns
that exist in one input table and not in another.

2. Which statement renames the existing column Location in work.travel


as Des tination?

a. set vacations(rename=(Location=Destination)) travel;

b. set vacations travel(rename=(Destination=Location));

c. set vacations travel(rename=(Location=Destination));

d. Aset vacations travel(rename(Destination=Location));

90
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Parentheses surround all data set options, and another set of parentheses surrounds the columns
listed for the RENAME= data set option.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-66 Lesson 1 Lesson Quizzes

3. Which statement is true concerning merging with matching rows?

a. The MERGE statement must refer to temporary input tables.


b. The columns in the BY statement can be in only one of the tables.
c. Only two input tables can be specified in the MERGE statement.
d. The input tables must be sorted by the columns in the BY statement.

92
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

With match-merging, each input table must first be sorted on the values of the BY columns or have
an appropriate index.

4. How many rows are in the output table given the following?
work.agetable w ork.foodtable
data both;
merge agetable
foodtable;
by Name;
run;

a. two
b. three
c. four
d. five

94
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

There will be two rows for Bob and three rows for Sue.
Note: There were 2 observations read from the data set WORK.AGETABLE.
Note: There were 5 observations read from the data set WORK.FOODTABLE.
Note: The data set WORK.BOTH has 5 observations and 3 variables.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.2 Solutions 1-67

data agetable;
input Name $ Age;
datalines;
Bob 12
Sue 15
;
run;

data foodtable;
input Name $ Food $;
datalines;
Bob Pizza
Bob Cupcakes
Sue Burgers
Sue Grapes
Sue Brownies
;

data both;
merge agetable foodtable;
by Name;
run;

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-68 Lesson 1 Lesson Quizzes

5. How many rows are in the output table given the following?
w ork.empsau work.phonec

a. two
data empsauc;
b. three merge empsau phonec;
by EmpID;
c. four run;
d. five

96
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

There will be a matching row for 121150, a nonmatching row for 121151, a matching row for 121152,
and a nonmatching row for 121153.
Note: There were 3 observations read from the data set WORK.EMPSAU.
Note: There were 3 observations read from the data set WORK.PHONEC.
Note: The data set WORK.EMPSAUC has 4 observations and 4 variables.
data empsau;
input First $ Gender $ EmpID;
datalines;
Togar M 121150
Kylie F 121151
Birin M 121152
;

data phonec;
input EmpID Phone $15.;
datalines;
121150 +61(2)5555-1795
121152 +61(2)5555-1667
121153 +61(2)5555-1348
;

data empsauc;
merge empsau phonec;
by EmpID;
run;

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.2 Solutions 1-69

6. What is the result of the following step?


w ork.donors1 w ork.donors2
data combine;
merge donors1 donors2;
by ID;
run;

a. The table com bine is created with four columns and five rows.
b. The step fails because the BY column ID is not properly sorted.
c. The step fails because U ni ts is in both tables and not the BY column.
d. The step fails because of duplicate ID values within the donors1 table.

98
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The two tables are not sorted by ID, so the step produces an error.
ERROR: BY variables are not properly sorted on data set WORK.DONORS1.
data donors1;
input ID Type $ Units;
datalines;
2304 O 16
1129 A 48
1129 A 50
2486 B 63
;
run;

data donors2;
input ID Code $ Units;
datalines;
1129 63 32
2304 61 45
1387 64 67

;
run;

data combine;
merge donors1 donors2;
by ID;
run;

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-70 Lesson 1 Lesson Quizzes

7. Which statement best describes the rows in the output table?


data bonuses;
merge managers(in=M)
staff(in=S);
by EmpID;
if M=0 and S=1;
run;

a. all the matching rows from both m a nagers and s taff


b. only the rows from m a nagers that have no match in s taff
c. only the rows from s taff that have no match in m a nagers
d. all the matching and nonmatching rows from both m a nagers and s taff

100
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

S=1 specifies that the staff table must contribute to the row, and M=0 specifies that the managers
table must not contribute to the row.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.2 Solutions 1-71

8. What are the values of C and A during the third iteration of the DATA
step?
w ork.clients work.amounts
data client_amount;
merge clients(in=C)
amounts(in=A);
by Name;
run;

a. C=0 and A =0
b. C=1 and A =0
c. C=0 and A =1
d. C=1 and A =1
102
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The third iteration is for the nonmatch of Davis. Davis is in the clients table (C=1) and not the
amounts table (A=0).
C=1 A=1 Name=Ankerton ID=11123 Amt=92 FIRST.Name=1 LAST.Name=0 _ERROR_=0 _N_=1
C=1 A=1 Name=Ankerton ID=11123 Amt=43 FIRST.Name=0 LAST.Name=1 _ERROR_=0 _N_=2
C=1 A=0 Name=Davis ID=22298 Amt=. FIRST.Name=1 LAST.Name=1 _ERROR_=0 _N_=3
C=1 A=1 Name=Masters ID=33351 Amt=27 FIRST.Name=1 LAST.Name=1 _ERROR_=0 _N_=4
data clients;
input Name $ ID;
datalines;
Ankerton 11123
Davis 22298
Masters 33351
;

data amounts;
input Name $ Amt;
datalines;
Ankerton 92
Ankerton 43
Masters 27
;

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-72 Lesson 1 Lesson Quizzes

data client_amount;
merge clients(in=C)
amounts(in=A);
by Name;
putlog _all_;
run;

9. What is the value of Location in the output table?


loc1 loc2 loc3
C o de L o cation C o de L o cation Co de L o cation
A Fr ance A B elgium A I t aly

a. Italy
data locALL;
b. France merge loc1 loc2 loc3;
c. Belgium by Code;
run;
d. France Belgium Italy

104
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The value of Location is based on the last table read. The loc3 table has a value of Italy for
Location.
data loc1;
input Code $ Location $;
datalines;
A France
;

data loc2;
input Code $ Location $;
datalines;
A Belgium
;

data loc3;
input Code $ Location $;
datalines;
A Italy
;

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.2 Solutions 1-73

data locALL;
merge loc1 loc2 loc3;
by Code;
run;

10. The DATA step can be used to merge the following three tables:

dataset1 dataset2 dataset3

Student Test Sc ore Su bject Test Su bject Av gScore

a. True
b. False

106
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Two DATA steps are needed. In the first DATA step, dataset1 and dataset2 can be merged by Test.
In the second DATA step, dataset3 can be merged with the result of the first DATA step by Subject.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-74 Lesson 1 Lesson Quizzes

Lesson 6

1. Which output table is produced from the following step?


data Earnings(keep=Qtr Earned);
Amount=1000; Rate=.075/4;
do Qtr=1 to 4;
Earned+(Amount+Earned)*Rate;
end;
run;

a. b.

c. d.

109
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The implied OUTPUT occurs after the DO loop. The value of QTR is 5, which is one increment
beyond the stop value of 4.

2. Which statement is true regarding the iterative DO loop?

DO index-column = start TO stop <BY increment> ;

a. The start and stop values can be character or numeric values.


b. If an increment value is not specified, the default increment is 0.
c. The index column is incremented at the bottom of each DO loop.
d. The index column is not in the final table unless specifically kept.

111
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The index column is incremented at the bottom of each DO loop. The start and stop values must be
numeric when used with the keyword TO. The default increment is 1. The index column is in the final
table unless specifically dropped.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.2 Solutions 1-75

3. How many rows are in the output table given the following?

pg 2.savings data work.savings;


set pg2.savings;
Savings=0;
do Year=1 to 5;
do qtr=1 to 4;
Savings+Amount;
Savings+(Savings*0.02/12);
end;
end;
a. 1 run;
b. 4
c. 5
d. 20
113
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Four rows are in the output table, one row per each row in the input table. The implied OUTPUT is
after the nested DO loops.

4. What is the final value of Yea r given the following step?

data invest;
do Year=2010 to 2019;
Capital+5000;
Capital+(Capital*.03);
end;
run;

a. . (missing)
b. 2010
c. 2019
d. 2020

115
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The final value of Year is 2020, which is one increment beyond the stop value of 2019.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-76 Lesson 1 Lesson Quizzes

5. Which of the following statements contains valid syntax?

a. do 1 to 10 by 2;
b. do while (Year>2025);
c. do until Earnings<=100000;
d. do date='01JAN2019' to '31JAN2019';

117
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

When using WHILE or UNTIL in the DO statement, the expression must be in a set of parentheses.
For a, the index column is missing. For c, the parentheses are missing around the expression. For d,
the DATE values are character instead of numeric (‘01JAN2019’d).

6. How many rows are in the output table given the following?

work.bikeinfo data bikeinfo2;


set bikeinfo;
do month=1 to 3;
do week=1 to 4;
bike=bike+2;
end;
output;
end;
a. 2 run;
b. 3
c. 6
d. 12
e. 24
119
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

For each row read in, three rows are created (one for each of three months). 2 rows read * 3 months
= 6 rows.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.2 Solutions 1-77

7. What is the value of x at the completion of the DATA step?

data test;
x=15;
do until(x>12);
x+1;
end;
run;

a. . (missing)
b. 13
c. 15
d. 16

121
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

A DO UNTIL is evaluated at the bottom. The initial value of X is 15. The DO loop occurs one time
even though 15 is greater than 12 because the condition is not checked until the bottom of the loop.
15 becomes 16 before the condition is checked.

8. Which statement is false?

a. The DO UNTIL loop executes until a condition is true.


b. The DO WHILE loop always executes at least one time.
c. The DO WHILE loop checks the condition at the top of the loop.
d. The DO UNTIL loop checks the condition at the bottom of the loop.

123
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The DO UNTIL loop always executes at least one time because the condition is checked at the
bottom of the loop. The DO WHILE loop executes only if the condition is true because the condition
is checked at the top of the loop.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-78 Lesson 1 Lesson Quizzes

9. Which of the following statements contains valid syntax?

a. do Age=10 to 14 and while (Weight<150);


b. do week=1 to 52 do until (Mileage ge 2750);
c. do Increase=5 to 10 while (temperature lt 102);
d. do Year=2018 to 2028 or until (Earnings<=100000);

125
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

When combining an iterative with a conditional, you cannot use extra words such as AND, DO, or
OR. The conditional immediately follows the iterative.

10. Which output table is produced from the following step?

data test;
bike=10;
do day=1 to 7 while (bike lt 13);
bike=bike+2;
end;
run;

a. b.

c. d.

127
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

On the second iteration of the DO loop, DAY=2 and BIKE=14. At the bottom of the loop, DAY
becomes 3. At the top of the loop, the condition is checked. 14 exceeds 13, so the DO loop is over
with DAY=3 and BIKE=14.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.2 Solutions 1-79

Lesson 7

1. Which is the better description for the following table?

a. wide table
b. narrow table

130
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The measures (air travel) are spit into multiple columns, which is called a wide table.
data new;
set sashelp.air;
where year(date) between 1956 and 1958
and month(date) between 1 and 6;
Year=cats('Yr',year(date));
retain Jan Feb Mar Apr May Jun;
if month(date)=1 then Jan=Air;
else if month(date)=2 then Feb=Air;
else if month(date)=3 then Mar=Air;
else if month(date)=4 then Apr=Air;
else if month(date)=5 then May=Air;
else if month(date)=6 then Jun=Air;
if month(date)=6 then output;
keep Year Jan Feb Mar Apr May Jun;
run;

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-80 Lesson 1 Lesson Quizzes

2. Which statement is needed for creating multiple rows from a single row
when using the DATA step to go from a wide to a narrow table?

a. WIDE
b. NARROW
c. RETAIN
d. OUTPUT

132
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

When going from a wide table to a narrow table, the OUTPUT statement is needed to create multiple
rows from a single row during one iteration of the DATA step.

3. How many rows will be in the final table if work.airwide contains three
rows?
data work.airnarrow;
set work.airwide;
a. 3 Month='Jan';
Air=Jan;
b. 6 output;
Month='Feb';
c. 9 Air=Feb;
d. 12 output;
Month='Mar';
Air=Mar;
output;
keep Year Month Air;
run;

134
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

For each row read from the input table work.airwide, three rows are created (one for January data,
one for February data, and one for March data). Because there are three rows in the input table, the
final table work.airnarrow will contain 9 rows, (3 input rows * 3 rows created per each iteration).

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.2 Solutions 1-81

data airwide;
set sashelp.air;
where year(date) between 1956 and 1958
and month(date) between 1 and 6;
Year=cats('Yr',year(date));
retain Jan Feb Mar Apr May Jun;
if month(date)=1 then Jan=Air;
else if month(date)=2 then Feb=Air;
else if month(date)=3 then Mar=Air;
else if month(date)=4 then Apr=Air;
else if month(date)=5 then May=Air;
else if month(date)=6 then Jun=Air;
if month(date)=6 then output;
keep Year Jan Feb Mar Apr May Jun;
run;

data work.airnarrow;
set work.airwide;
Month='Jan’;
Air=Jan;
output;
Month='Feb';
Air=Feb;
output;
Month='Mar’;
Air=Mar;
output;
keep Year Month Air;
run;

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-82 Lesson 1 Lesson Quizzes

4. When using the DATA step to go from a narrow table to a wide table, the
KEEP statement is needed to hold values in the PDV across multiple
iterations of the DATA step.

a. True
b. False

136
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The RETAIN statement is needed when using the DATA step to go from a narrow table to a wide
table. The RETAIN statement holds values in the PDV across multiple iterations of the DATA step.
The KEEP statement controls the columns that makes it to the final table.

5. Which statement needs to be added to the DATA step to include only the
last row per Yea r in the output table?
data work.airwide2(keep=Year Jan Feb Mar);
set work.airnarrow;
by Year;
retain Jan Feb Mar;
if Month='Jan' then Jan=Air;
else if Month='Feb' then Feb=Air;
else if Month='Mar' then Mar=Air;
... insert statement here ...
run;
a. output;
b. if Last then output;
c. if Last.Year=1 then output;
d. if Last.Year=0 then output;
138
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

Due to the BY statement, the PDV contains a column named First.Year and a column named
Last.Year. Last.Year will be equal to 1 when the last row of Year is read in from the input table. It is
during this iteration that the PDV contents need to be output.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.2 Solutions 1-83

data airwide;
set sashelp.air;
where year(date) between 1956 and 1958
and month(date) between 1 and 6;
Year=cats('Yr',year(date));
retain Jan Feb Mar Apr May Jun;
if month(date)=1 then Jan=Air;
else if month(date)=2 then Feb=Air;
else if month(date)=3 then Mar=Air;
else if month(date)=4 then Apr=Air;
else if month(date)=5 then May=Air;
else if month(date)=6 then Jun=Air;
if month(date)=6 then output;
keep Year Jan Feb Mar Apr May Jun;
run;

data work.airnarrow;
set work.airwide;
Month='Jan';
Air=Jan;
output;
Month='Feb';
Air=Feb;
output;
Month='Mar';
Air=Mar;
output;
keep Year Month Air;
run;

data work.airwide2(keep=Year Jan Feb Mar);


set work.airnarrow;
by Year;
retain Jan Feb Mar;
if Month='Jan' then Jan=Air;
else if Month='Feb' then Feb=Air;
else if Month='Mar' then Mar=Air;
if Last.Year=1 then output;
run;

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-84 Lesson 1 Lesson Quizzes

6. Which statement is false concerning the TRANSPOSE procedure?

a. Columns are transposed into rows.


b. By default, numeric columns are transposed.
c. Use a BY statement to sort the data while transposing.
d. Use a VAR statement to specifically specify the character and numeric
columns to transpose.

140
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

PROC TRANSPOSE cannot sort the data. The data needs to be sorted before the TRANSPOSE
statement. The BY statement transposes data within groups.

7. Which statements are needed in a PROC TRANSPOSE step for the


following example (narrow  wide)?

a. by Day; b. id Day;
var Meal Food; var Food Meal;

c. by Day; d. by Day;
id Food; id Meal;
var Meal; var Food;
142
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The BY statement transposes the data by the grouping of Day. The ID statement uses the values of
Meal as the column headings. The VAR statement transposes the Food values into rows.

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.2 Solutions 1-85

data meals;
length Day Meal Food $ 10;
input Day $ Meal $ Food $;
datalines;
Saturday Breakfast Yogurt
Saturday Lunch Sandwich
Saturday Dinner Steak
Sunday Breakfast Pancakes
Sunday Lunch Salad
Sunday Dinner Lasagna
;
run;

proc transpose data=meals out=meals1(drop=_name_);


by Day;
id Meal;
var Food;
run;

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-86 Lesson 1 Lesson Quizzes

8. Which statement or statements are needed in a PROC TRANSPOSE step


for the following example (wide  narrow)?

a. by Day; b. var Breakfast Lunch Dinner;

c. id Day; d. id Day;
var Breakfast Lunch Dinner;

144
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The columns of Breakfast, Lunch, and Dinner are transposed into rows.
data meals;
length Day Meal Food $ 10;
input Day $ Meal $ Food $;
datalines;
Saturday Breakfast Yogurt
Saturday Lunch Sandwich
Saturday Dinner Steak
Sunday Breakfast Pancakes
Sunday Lunch Salad
Sunday Dinner Lasagna
;
run;

proc transpose data=meals out=meals1(drop=_name_);


by Day;
id Meal;
var Food;
run;

proc transpose data=meals1 out=meals2;


var Breakfast Lunch Dinner;
run;

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1.2 Solutions 1-87

9. Which option is needed in the PROC TRANSPOSE statement to rename


the _N AME_ column?

a. _name_=Meal
b. name=Meal
c. prefix=Meal
d. rename=Meal

146
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The NAME= option specifies the name for the column in the output table that contains the name of
the column that is being transposed to create the current row. The default name is _NAME_.
data meals;
length Day Meal Food $ 10;
input Day $ Meal $ Food $;
datalines;
Saturday Breakfast Yogurt
Saturday Lunch Sandwich
Saturday Dinner Steak
Sunday Breakfast Pancakes
Sunday Lunch Salad
Sunday Dinner Lasagna
;
run;

proc transpose data=meals out=meals1(drop=_name_);


by Day;
id Meal;
var Food;
run;

proc transpose data=meals1 out=meals2 name=Meal;


var Breakfast Lunch Dinner;
run;

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-88 Lesson 1 Lesson Quizzes

10. Which option is needed in the PROC TRANSPOSE statement to rename


the COL columns?

a. out=meals2(COL1=Day_7 COL2=Day_1)
b. out=meals2(name=(COL1=Day_7 COL2=Day_1))
c. out=meals2(rename=(COL1=Day_7 COL2=Day_1))
d. out=meals2(prefix=(COL1=Day_7 COL2=Day_1))

148
C o p yri gh t © SA S In sti tu te In c. A l l ri gh ts reserved .

The RENAME= data set option can be used to change the names of the COL columns.
data meals;
length Day Meal Food $ 10;
input Day $ Meal $ Food $;
datalines;
Saturday Breakfast Yogurt
Saturday Lunch Sandwich
Saturday Dinner Steak
Sunday Breakfast Pancakes
Sunday Lunch Salad
Sunday Dinner Lasagna
;
run;

proc transpose data=meals out=meals1(drop=_name_);


by Day;
id Meal;
var Food;
run;

proc transpose data=meals1


out=meals2(rename=(COL1=Day_7 COL2=Day_1))
name=Meal;
var Breakfast Lunch Dinner;
run;

Copyright © 2018, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.

You might also like