ElearnSAS.com

ElearnSAS.com
SAS Learning Platform

Base SAS 47

The following SAS program is submitted:
data work.test;
First = 'Ipswich, England';
City_Country = substr(First,1,7)!!', '!!'England';
run;
Which one of the following is the length of the variable CITY_COUNTRY in the output data set?
A. 6
B. 7
C. 17
D. 25
Click Comment link to get answer

27 comments:

  1. can i have the explanaion?

    ReplyDelete
  2. Anonymous7:43 PM

    use this:
    data work.test;
    First = 'Ipswich, England';
    City_Country = substr(First,1,7)!!', '!!'England';
    yy=length(City_Country);
    run;

    yy= 16(7+2+7)

    ReplyDelete
  3. Anonymous10:12 AM

    data work.test;
    First = 'Ipswich, England';
    City_Country = substr(First,1,7)!!', '!!'England';
    run;

    proc contents data=test;
    run;

    This is the result of properties and length of variables

    Alphabetic List of Variables and Attributes

    # Variable Type Len

    2 City_Country Char 25
    1 First Char 16

    ReplyDelete
  4. Anonymous10:31 PM

    By default the variable length is determined by the length of the first argument for substr.

    So here we have 16 + 2(', ') + 7('England') = 25

    ReplyDelete
  5. Anonymous9:53 AM

    mysasworld,
    use VLENGTH rather than LENGTH

    ReplyDelete
  6. Anonymous6:15 PM

    I did not recall using these !!. I ran the code so I got the answer. So !! work like ||?

    ReplyDelete
    Replies
    1. Yes you're right, !!can be treat exactly like ||

      Delete
  7. Yes !! and || are same, some OS machines don't have pipe character for that reason SAS made esclamation mark same as pipe.

    ReplyDelete
    Replies
    1. Anonymous3:19 PM

      Thanks for clearing the confusion

      Delete
  8. Anonymous6:07 PM

    Gh4.26.11 please if you disagree,let us know your logic.

    If we run the data , we get the answer which is 25 character but what is the logic?

    What if we add Cophegn in stead of added word England.
    Just trying to elaborate Sudha.
    First variable has 16 length, asked to substract 7 letter that would be Enland and if we add Cophegn 7 letter word.
    The 'First" variable still occupies 16 length then a space and , and 7 letter 'Cophegn' added makes total 25.
    The word England taken out and added back makes little confuse.
    Is it any thing to do with the variable in inverted comma 'Ipswich, England' ? It takes the letter or character out but it does not trim the length. Without the trim it would take all the space in variable "first" plus space , and 7 letter= 25.
    Did I clear it?

    Sakar Sham

    ReplyDelete
  9. "In SAS programming, the concatenation operator is a pair of vertical bars (||). If your keyboard does not have a solid vertical bar, use two broken vertical bars (¦¦) or two exclamation points (!!).

    The length of the new variable is the sum of the lengths of the pieces or number of characters that is specified in a LENGTH statement for the new variable."

    (ref - Sas help and documentation)

    Answer is 25 because :
    length of First = 16
    length of ', ' = 2
    length of 'England' = 7

    Total length = 25. Thus Answer = D

    ReplyDelete
  10. has anyone given it a thought what would the output be if one tried to print len=length(City_Country) --> 15

    ReplyDelete
  11. Anonymous11:17 PM

    The confusion in this case is that there is a string length and a variable length. This question specifically asks about the variable length.

    data work.BaseSAS47;
    First = 'Ipswich, England';
    City_Country = substr(First,1,7)!!', '!!'England';
    leng = length(City_Country);
    vleng = vlength(City_Country);
    run;

    string length = 16
    variable length = 26

    ReplyDelete
    Replies
    1. Anonymous11:18 PM

      typo; variable length = 25

      Delete
  12. Anonymous7:25 PM

    the code is giving the following output

    First City_Country
    Ipswich, England Ipswich, England

    If the answer is D 25; is it not supposed to give
    Ipswich, England, England (tot character length 25)

    ReplyDelete
    Replies
    1. Ans:D

      Try the code snippet:
      data work.test;
      First = 'Ipswich, England';
      City_Country = subst(First,1,7)!!', '!!'England';
      run;
      proc contents data=test;
      run;

      Delete
  13. Anonymous1:33 AM

    Why is the length of First = 16? Shouldn't it be 7?

    ReplyDelete
    Replies
    1. Anonymous5:54 AM

      I thought the same thing. Because the substring fn says (First, 1, 7) meaning length 7 from position 1.

      Delete
    2. coz FIRST='Ipswich, England'=16

      Delete
  14. 25 is the correct answer.

    SAS reserves the same length as the variable it is substr'ing on or 16 positions. you then add 2 positions for the ', ' and then you add 7 positions for 'England' for a total of 25.

    There's another question coming up dealing with Substr(Scan and scan reserves 200 positions. That's what I was getting confused on.

    ReplyDelete
  15. Anonymous8:19 PM

    This is hard to believe but the correct answer is D.
    The compiler is stupid or lazy to set the length of City_Country as vlength(First)+vlength(', ')+vlength('England') although the value of City_country is 'Ipswich, England'.

    ReplyDelete
  16. what is !! function in sas ?

    ReplyDelete
  17. Sohel9:13 PM

    This is from SAS prep guide. So a space and a comma is default in scan an will always be taken into account

    To create the LastName variable to store the employee's last name, you write an assignment
    statement that contains the following SCAN function:
    LastName=scan(name,1,' ,');
    Note that a blank and a comma are specified as delimiters. You can also write the function
    without listing delimiters, because the blank and comma are default delimiters.
    LastName=scan(name,1);

    ReplyDelete
  18. Anonymous9:46 PM

    length of First = 16
    length of ', ' = 2
    length of 'England' = 7
    =25
    D

    ReplyDelete