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
Click Comment link to get answer
A
ReplyDeleteWrong.. It's C
Deletethe variable first gets its length from author which is 15
Correct answer is 'D'.
ReplyDeleteafsal is rite...its D
ReplyDeleteYes the default length for the variable on r.h.s of scan function is 200
ReplyDeleteExactly
ReplyDeleteits 2oo
LENGTH examines the variable at run-time, trimming trailing blanks to determine the length. VLENGTH returns a compile-time constant value, which reflects the maximum length.
Deleteso depends on that the length varies.
Correct Answer: D
ReplyDeleteA is the correct answer.
ReplyDelete"If the SCAN function returns a value to a variable that has not yet been assigned a length, by default the variable is assigned a length of 200. "
Here SCAN is not returning a value to a variable, but SCAN's output is the argument for SUBSTR.
A variable that is created by SUBSTR obtains its length from the length of argument. The default length of SCAN function is 200, which is the Length of argument. So answer is 200.
ReplyDeletebut you are extracting just 1 character from the string so should'nt the length be =1?
Deleteit helps. thx.
Deletecan some one please explain why it is D
ReplyDeletethanks
its already been explained, read the earlier post.
ReplyDeletePlease submit the following code and check the length of FIRST in the SAS output. Then, you can be sure that the correct answer is D because FIRST has a length of 200.
ReplyDeletedata work.test;
Author = 'Agatha Christie';
First = substr(scan(author,1,' ,'),1,1);
run;
proc contents;
run;
I ran it got length of 15 for First. Are you sure you got 200?
Deleteanswer is D :
ReplyDeleteAlphabetic List of Variables and Attributes
# Variable Type Len
1 Author Char 15
2 First Char 200
The default character length is 200, but the question is why it takes the all 200? Not less than that why?
ReplyDeletebecause it is supervisory function, so by default length is 200
DeleteCan you show us how to get alphabetical list of variables and attributes?
ReplyDeleteJust do proc contents data=test; *( in this case);
Deleterun;
At the very end, it will show:
Alphabetic List of Variables and Attributes
# Variable Type Len
1 Author Char 15
2 First Char 200
Wow... :) Very interesting question. Neha is right, answer is 1.
ReplyDeleteFirst = substr(scan(author,1,' ,'),1,1);
First = substr(Word, Position, # of characters);
Here, Number of Characters or the Length = 1.
Therefore, the answer is A. :)
Proof = Output of the code printed:
The SAS System
Obs Author First
1 Agatha Christie A
Answer is D.
ReplyDeleteDid Proc contents to see the meta data and the lenght of variable First was 200.
Alphabetic List of Variables and Attributes
# Variable Type Len
1 Author Char 15
2 First Char 200
Why making it complicate ans is A.
ReplyDeleteresult of Scan is Agatha.
result of substr is A.
so the length of First is 1.
The length is 200
ReplyDeletedata work.test;
Author = 'Agatha Christie';
First = substr(scan(author,1,' ,'),1,1);
length=lengthc(first);
run;
proc print data=test;
run;
Answer is D
ReplyDeleteLENGTHM Function
Returns the amount of memory (in bytes) that is allocated for a character string
data work.test;
Author = 'Agatha Christie';
First = substr(scan(author,1,' ,'),1,1);
length=lengthm(first);
run;
Answer is D
ReplyDeleteIf you don't understand, try the following program
data work.test2;
Author = 'Agatha Christie';
First = substr(author,1,1);
run;
proc contents;run;
Alphabetic List of Variables and Attributes
# Variable Type Len
1 Author Texte 15
2 First Texte 15
In fact, with susbstr, SAS keep the length of the variable "author". Here, it's 15.
Then, the default length of a scan function is 200.
So, because we use a scan function inside the substr function, the length of "first" will be the same as the length for scan - -> 200
Answer is D
I hope it's clear, sorry for my english.
Its showing length as 15, then y do you say it as 200??
Delete# Variable Type Len
1 Author Char 15
2 First Char 15
looking at this question, people are saying the answer is 200. But I disagree, I say that it is 15.
ReplyDeleteHere is my reasoning: Although the default length for a character variable is automatically 200, when SAS first encounters a Character variable its length will be set by the first instance of a character variable. in this case 'Agatha Christie', which is 15.
the default length for the sub-string function is the longest character it can extract from a character variable of length n. here it is 15. so the default length of the variable First will be 15
Answer is 15. Try this code in SAS. thefox916 is right.
Delete@SAScert could you please let us know the correct answer.
ReplyDelete????
C
Deletefor SAS 9.4, it is 15.
I think for previous version it was 200.
C
ReplyDeleteThe answer used to be 200 - in versions of SAS earlier than 9.4, scan would have defaulted to a length of 200 causing the substr to default to 200 as well.
ReplyDeleteHowever, the answer in SAS version 9.4 is 15. This is a change in the new version. Scan function now defaults to the length of the string, just as substr does.
I am confident this is correct because I just got this question wrong on the SAS certification practice exam! :) I put 200 and it was incorrect. The explanation it gives is:
"As of SAS 9.4, if the SCAN function returns a value to a variable that has not yet been given a length, that variable is given the length of the first argument"
THanks Jenine for pointing this out. SAS is so unstandard and stupid. There are so many options that are named so random I seriously doubt the language designer's intelligence. Like name FSList, one can hardly know the meaning of it. This is a terrible language.
DeleteThank you again for pointing this out.
DeleteQuote "In a DATA step, if the SCAN function returns a value to a variable that has not yet been given a length, then that variable is given the length of the first argument."
Source: http://support.sas.com/documentation/cdl/en/lefunctionsref/67960/HTML/default/viewer.htm#lefunctionsrefwhatsnew94.htm
Jenine is right. I tested it with the SAS University Edition.
ReplyDeleteThat's right.
Deleteit definitely changed in 9.4!
ReplyDeleteWhile running following program on SAS9.4, proc contents result showing length 15.
ReplyDeletedata work.test;
Author = 'Agatha Christie';
First = substr(scan(author,1,' ,'),1,1);
run;
proc contents;
run;
Alphabetic List of Variables and Attributes
# Variable Type Len
1 Author Char 15
2 First Char 15
While running following program on SAS9.4, proc contents result showing length 15.
ReplyDeletedata work.test;
Author = 'Agatha Christie';
First = substr(scan(author,1,' ,'),1,1);
run;
proc contents;
run;
Alphabetic List of Variables and Attributes
# Variable Type Len
1 Author Char 15
2 First Char 15
I ran the code 14th August 2016 4:02:32 EST
ReplyDeletedata work.test;
Author = 'Agatha Christie';
First = substr(scan(author,1,' ,'),1,1);
run;
proc print;
proc contents;
Alphabetic List of Variables and Attributes
# Variable Type Len
1 Author Char 15
2 First Char 15
For anyone currently using this question for practice the answer is NOW 'C' ... yes the variable length is 15 ... it has stopped being 200 since SAS 9.3 or 9.4 ... please refer this link for conclusive proof ... (done on SAS Studio University Edition) ...
ReplyDeletehttp://gnesis.com/SAS/varlengthproof.jpg
the correct answer is C :
ReplyDeletei just tried this program below and the answer was 15, that's the correct answer because the program returs just the firt letter from the variable autor which contains 14 letter, so 200/14 = 15
data work.test;
Author = 'Agatha Christie';
First = substr(scan(author,1,' ,'),1,1);
run;
proc contents data= work.test;
run;
This is a screw up question, version changed and our old review answer still showing 200. How can we possibly get it right in exam?
ReplyDeleteIt's C. I ran the program in SAS 9.4.
ReplyDeleteLength is 15 for both variable.
Answer is C.
ReplyDeleteExplanation:Here, we need to understand how variables are created in PDV(Program Data Vector). In PDV first the variables Author will be created with the length of (15 bytes / 15 characters) because of number of letters between the strings of the assignment statement Author = 'Agatha Christie';
Since the source variable in the assignment statement First = substr(scan(Author,1,' '),1 ,1); is Author, so variable First will have the same length of (15 bytes / 15 characters). You may run it on SAS and then do one thing ...check for Test file created on work.Right click on properties and you will get the length of First variable as 15.
earlier versions can be different but sas 9.4 gives this explanation
ReplyDelete"As of SAS 9.4, if the SCAN function returns a value to a variable that has not yet been given a length, that variable is given the length of the first argument"
so it should be 15 which c
I ran the code , the length is 15 , not 200:
ReplyDelete
ReplyDeleteAlphabetic List of Variables and Attributes
# Variable Type Len
1 Author Char 15
2 First Char 15