ElearnSAS.com

ElearnSAS.com
SAS Learning Platform

Base SAS 44

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

50 comments:

  1. Replies
    1. Anonymous11:15 PM

      Wrong.. It's C
      the variable first gets its length from author which is 15

      Delete
  2. Correct answer is 'D'.

    ReplyDelete
  3. afsal is rite...its D

    ReplyDelete
  4. Anonymous9:40 AM

    Yes the default length for the variable on r.h.s of scan function is 200

    ReplyDelete
  5. Replies
    1. Anonymous4:38 PM

      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.

      so depends on that the length varies.

      Delete
  6. Anonymous4:14 AM

    Correct Answer: D

    ReplyDelete
  7. Anonymous9:42 PM

    A is the correct answer.

    "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.

    ReplyDelete
  8. Anonymous7:16 PM

    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.

    ReplyDelete
    Replies
    1. Anonymous10:42 PM

      but you are extracting just 1 character from the string so should'nt the length be =1?

      Delete
    2. Anonymous7:43 PM

      it helps. thx.

      Delete
  9. Anonymous2:18 PM

    can some one please explain why it is D
    thanks

    ReplyDelete
  10. Anonymous3:49 PM

    its already been explained, read the earlier post.

    ReplyDelete
  11. Please 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.

    data work.test;
    Author = 'Agatha Christie';
    First = substr(scan(author,1,' ,'),1,1);
    run;
    proc contents;
    run;

    ReplyDelete
    Replies
    1. Anonymous4:10 PM

      I ran it got length of 15 for First. Are you sure you got 200?

      Delete
  12. Anonymous11:22 PM

    answer is D :

    Alphabetic List of Variables and Attributes

    # Variable Type Len

    1 Author Char 15
    2 First Char 200

    ReplyDelete
  13. Anonymous6:41 PM

    The default character length is 200, but the question is why it takes the all 200? Not less than that why?

    ReplyDelete
    Replies
    1. because it is supervisory function, so by default length is 200

      Delete
  14. Anonymous12:47 PM

    Can you show us how to get alphabetical list of variables and attributes?

    ReplyDelete
    Replies
    1. Just do proc contents data=test; *( in this case);
      run;
      At the very end, it will show:
      Alphabetic List of Variables and Attributes
      # Variable Type Len
      1 Author Char 15
      2 First Char 200

      Delete
  15. Wow... :) Very interesting question. Neha is right, answer is 1.

    First = 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

    ReplyDelete
  16. Anonymous6:44 AM

    Answer is D.

    Did 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

    ReplyDelete
  17. Anonymous2:45 PM

    Why making it complicate ans is A.

    result of Scan is Agatha.

    result of substr is A.

    so the length of First is 1.

    ReplyDelete
  18. Anonymous7:01 AM

    The length is 200
    data work.test;
    Author = 'Agatha Christie';
    First = substr(scan(author,1,' ,'),1,1);
    length=lengthc(first);
    run;

    proc print data=test;
    run;

    ReplyDelete
  19. Anonymous8:53 AM

    Answer is D

    LENGTHM 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;

    ReplyDelete
  20. Guillaume7:35 PM

    Answer is D

    If 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.

    ReplyDelete
    Replies
    1. Anonymous5:42 PM

      Its showing length as 15, then y do you say it as 200??
      # Variable Type Len
      1 Author Char 15
      2 First Char 15

      Delete
  21. looking at this question, people are saying the answer is 200. But I disagree, I say that it is 15.

    Here 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

    ReplyDelete
    Replies
    1. Anonymous6:13 PM

      Answer is 15. Try this code in SAS. thefox916 is right.

      Delete
  22. Anonymous4:07 PM

    @SAScert could you please let us know the correct answer.
    ????

    ReplyDelete
    Replies
    1. Anonymous3:55 PM

      C

      for SAS 9.4, it is 15.

      I think for previous version it was 200.

      Delete
  23. Jenine2:32 PM

    The 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.

    However, 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"

    ReplyDelete
    Replies
    1. Anonymous8:38 PM

      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.

      Delete
    2. Anonymous8:43 PM

      Thank you again for pointing this out.
      Quote "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

      Delete
  24. Anonymous8:54 PM

    Jenine is right. I tested it with the SAS University Edition.

    ReplyDelete
  25. Anonymous10:26 PM

    it definitely changed in 9.4!

    ReplyDelete
  26. Anonymous3:48 PM

    While running following program on SAS9.4, proc contents result showing length 15.
    data 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

    ReplyDelete
  27. Anonymous4:01 PM

    While running following program on SAS9.4, proc contents result showing length 15.
    data 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

    ReplyDelete
  28. Anonymous4:03 PM

    I ran the code 14th August 2016 4:02:32 EST

    data 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

    ReplyDelete
  29. 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) ...
    http://gnesis.com/SAS/varlengthproof.jpg

    ReplyDelete
  30. the correct answer is C :
    i 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;

    ReplyDelete
  31. 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?

    ReplyDelete
  32. It's C. I ran the program in SAS 9.4.

    Length is 15 for both variable.

    ReplyDelete
  33. Anonymous8:08 AM

    Answer is C.
    Explanation: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.

    ReplyDelete
  34. earlier versions can be different but sas 9.4 gives this explanation

    "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

    ReplyDelete
  35. I ran the code , the length is 15 , not 200:

    ReplyDelete

  36. Alphabetic List of Variables and Attributes
    # Variable Type Len
    1 Author Char 15
    2 First Char 15

    ReplyDelete