ElearnSAS.com

ElearnSAS.com
SAS Learning Platform

Base SAS 45

The following SAS program is submitted:
data work.test;
Author = 'Christie, Agatha';
First = substr(scan(author,2,' ,'),1,1);
run;
Which one of the following is the value of the variable FIRST in the output data set?
A. A
B. C
C. Agatha
D. ' ' (missing character value)
Click Comment link to get answer

26 comments:

  1. Anonymous9:41 AM

    Answer is 'A'

    ReplyDelete
  2. Anonymous8:27 AM

    If there is a space in between two parts of the variable 'author' then answer is D. Else answer is A

    ReplyDelete
  3. Anonymous6:23 AM

    D -
    scan(author,2,',') returns "[space]Agatha", therefore First="[space]"

    ReplyDelete
  4. Anonymous5:34 PM

    Correct answer is A.

    Even if there is a space b/w the comma and the Agatha, it does not matter because 'blank' or ;space' is also one of the several(blank . < ( + | & ! $ * ) ; ^ - / , % default delimiters.

    ReplyDelete
    Replies
    1. Robert B.12:25 PM

      The default delimiters don't matter. The delimiters are defined. So the question is whether blank is a delimiter. Here it is whether the questioner meant it to be or not. Hence A is the answer. Remove the blank from the list and D is the answer.

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

      This has blank and comma as delimiters.

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

      This just commma.

      Very hard thing to notice as you can see looking at the two. Hope nothing like that is on the base sas, too easy to make a mistake.

      Delete
  5. Anonymous9:24 PM

    Its D I compiled the program.....If there is no space then it is considered as option A

    ReplyDelete
  6. Anonymous12:08 PM

    I compiled the program too and I got A as the answer. Even if I entered several space before the A.

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

    ReplyDelete
  7. Anonymous4:40 PM

    Answer is A.
    If we dont mention space as delimiter in the scan function ,then answer would be D.

    ReplyDelete
  8. Anonymous11:04 AM

    scan function executed first,it will give result as agatha.now substr funtion looks for first position character ie,1-1 ,results A so answer is A.

    ReplyDelete
  9. Anonymous6:22 AM

    definitely A, its separated by comma. so if you scan author for 2nd word. your answer is Agatha. and once you substring the first word, you get A.

    ReplyDelete
  10. Anonymous6:32 PM

    Have you seen this?
    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

    Can you relate these two?

    Sakar Sham

    ReplyDelete
  11. Anonymous6:35 PM

    That question has unusual Answer and could not relate.

    Sakar Sham

    ReplyDelete
  12. Same answer as the previous questions. The length of the substring hasn't changed. Therefore, the answer is A.

    Output:

    Obs Author First
    1 Agatha, Christie A

    ReplyDelete
  13. Anonymous4:59 AM

    Here's the question, when I ran the coding this, I got "A" as an answer, but when I put 'First = substr(scan(author,2,','),1,1);' - without space before ',' I got "D" I don't know why, would you guys explain the reason? Thx

    ReplyDelete
    Replies
    1. the reason is that where you use a ' ,' (a space and a comma in quotes) to specify the delimiter, then SAS considers spaces and commas to be delimiters and disregards them both ... if you use only the comma to specify the delimiter then EVERYTHING after the comma to the next comma or end of string is considered to be a word by the scan function .. including spaces

      Delete
  14. Anonymous3:49 PM

    The SCAN function treats two or more contiguous delimiters, such as the parenthesis and slash below, as one delimiter. Also, leading delimiters have no effect.

    ReplyDelete
  15. Anonymous6:51 PM

    Two parts need to pay attention cuz they were unclearly mentioned in this question.
    First, in scan() function, if there is a space in ' ,' (not ','), SAS cannot find this user defined delimiter in var Author. Thus, in Author, whether this is a space after ',' doesn't matter because there is no space before ',' to be matched anyway. So SAS will always find second word "Agatha" (both ',' and space after it will be treated as default delimiters). Subtring it, we will always get 'A'. But note that the length of Author and First are 16 with space (in Author) and 15 without (in Author).
    However, if there is no space in ',' in scan() function, the second part matters, which is whether there is a space after 'Christie,' in Author. If there is a space, the scan() function will return 'space + Agatha' cuz SAS will treat ',' as delimiter only. Thus, we will get ' ' missing char value in First. In contrast, if there is no space after 'Christie,' , the scan() function will return 'Agatha' and we will get 'A' in First. Again, note that the length of Author and First are 16 with space (in Author) and 15 without (in Author).

    Thx!

    ReplyDelete
  16. Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care
    http://chennaitraining.in/qliksense-training-in-chennai/
    http://chennaitraining.in/pentaho-training-in-chennai/
    http://chennaitraining.in/machine-learning-training-in-chennai/
    http://chennaitraining.in/artificial-intelligence-training-in-chennai/
    http://chennaitraining.in/msbi-training-in-chennai/
    http://chennaitraining.in/citrix-training-in-chennai/

    ReplyDelete