By admin | September 22, 2009

SAS Certified Base Programmer 123 Questions (73)

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

8 comments | Add One

  1. admin - 09/22/2009 at 11:07 am

    D

  2. Ed Malthouse - 08/19/2010 at 1:20 pm

    I believe the answer should be 16, which is what I get when I evaluate LENGTH(City_Country). This concatinates 3 strings, the first with length 7, the second with length 2 and the third with length 7, and 7+2+7=16.

  3. sudha - 12/18/2010 at 2:24 am

    Yes i also believed the length to be 16….but when you give proc contents and check for the variable length….it is coming as 25…….though i am also not able to find a explanation as too how 25…!!!

    if any one knows the explanation…do plz state it here

  4. Jenny G - 01/25/2011 at 6:07 pm

    The length of First is 16.
    The length of ‘ ,’ is 2.
    The length of England is 7.
    So the length of City_Country is 16+2+7=25.

  5. But Ponton - 10/12/2011 at 6:54 pm

    The Substr function always takes its length from the variable in the argument. So that implies the length from the substr function is 16.

    Now the length of ” ,” is 2. And the length of “England” is 7.

    The length of the target variable (City_Country) after concatenation is a sum of the lengths of each part. Therefore 16+2+7=25.

    I hope that helps.

  6. sun - 02/12/2012 at 5:10 am

    The ans should be 16.
    try this ↓
    you can get the value of omg as 16.

    data work.hh;
    First = ‘Ipswich, England’;
    City_Country= substr(First,1,7)!!’, ‘!!’England’;
    omg=length(City_country);
    run;

    proc print data=work.hh;
    var City_country omg;
    run;

  7. Fleevi - 07/9/2014 at 10:32 pm

    what’s more interesting is if you try hard-code (I counted the numbers of space in SAS Studio)
    hardcode=length(‘Ipswich , England’);

    It gives 25!
    Anyway, the question would be more likely to ask the len column output by the PROC contents.

  8. Fleevi - 07/9/2014 at 10:34 pm

    Well the comment didn’t display properly…… it’s
    hardcode=length(‘Ipswich_________, England’);

Leave a Comment

Leave a Reply

Your email address will not be published.