The following SAS DATA step is submitted:
data work.accounting;
set work.department;
length jobcode $ 12;
run;
The WORK.DEPARTMENT SAS data set contains a character variable named JOBCODE with a length of 5.
Which one of the following is the length of the variable JOBCODE in the output data set?
A. 5
B. 8
C. 12
D. The length can not be determined as the program fails to execute due to errors.
Click Comment link to get answer
A
ReplyDeleteA
ReplyDeletec
ReplyDeleteA. is correct
ReplyDeleteThis is the log error after we run this program.
/* WARNING: Length of character variable has already been set.
Use the LENGTH statement as the very first statement in the DATA STEP to declare the length
of a character variable.
*/
Thank you!
DeleteAnswer is A
ReplyDeleteNote:
ReplyDeleteIf the "length statement" would have been defined before "set statement", then the answer would be C!
ferrat
The correct answer is A:
ReplyDeletePls see program below:
data work.department;
jobcode = '12345';
run;
data work.accounting;
set work.department;
length jobcode $ 12;
jobcode = '123456789012';
run;
proc contents data=work.accounting;
run;
==============================
= Begin of SAS log
==============================
214 data work.department;
215 jobcode = '12345';
216 run;
NOTE: The data set WORK.DEPARTMENT has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.14 seconds
cpu time 0.02 seconds
217
218 data work.accounting;
219 set work.department;
220 length jobcode $ 12;
WARNING: Length of character variable jobcode has already been set.
Use the LENGTH statement as the very first statement in the DATA STEP to declare the
length of a character variable.
221 jobcode = '123456789012';
222
223 run;
NOTE: There were 1 observations read from the data set WORK.DEPARTMENT.
NOTE: The data set WORK.ACCOUNTING has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.08 seconds
cpu time 0.03 seconds
224
225 proc contents data=work.accounting;
226 run;
NOTE: PROCEDURE CONTENTS used (Total process time):
real time 0.20 seconds
cpu time 0.01 seconds
==============================
= End of SAS log
==============================
==============================
= Begin of SAS Output
==============================
Alphabetic List of Variables and Attributes
# Variable Type Len
1 jobcode Char 5
==============================
= End of SAS Output
==============================
I tried it! it's C!!
ReplyDeleteIts A because the base dataset has already the length set as 5 if you try to reset after set statement it gives you a warning.
ReplyDeleteAns is A
ReplyDeleteIt log window shown as warning but proc contets showing the length is 12.please correct me if i wrong...
ReplyDeleteLength of character variable jobcode has already been set.
ReplyDeleteUse the LENGTH statement as the very first statement in the DATA STEP to declare the
length of a character variable.
Proc contens also showed length as 5: see belwo:
The CONTENTS Procedure
Data Set Name WORK.ACCOUNTING Observations 1
Member Type DATA Variables 1
Engine V9 Indexes 0
Observation Length 5
Answer : A
ReplyDeleteReason : Sas defines the variable length in the descriptor portion of PDV through its first occurrence. In this case ,Set statement comes before Length therefore length = Length defined by the dataset being read by set statement.
If we want to change that , Length statement must come before set statement so that it gets defined as 12 in descriptor portion.
This is making sense. Thanks.
ReplyDelete