By admin | August 6, 2009

SAS Certified Base Programmer 50 Questions (21)

The following SAS program is submitted:

data work.AreaCodes;
    Phonenumber=3125551212;
    Code='('!!substr(Phonenumber,1,3)!!')';
run;

Which one of the following is the value of the variable Code in the output data set?

a. (  3)
b. (312)
c. 3
d. 312

7 comments | Add One

  1. admin - 08/9/2009 at 8:34 pm

    A

  2. Anno - 09/28/2009 at 2:47 am

    what happens when SUBSTR is used for numeric value?

  3. admin - 09/28/2009 at 8:10 am

    An automatic data conversion is performed whenever a numeric variable is used where SAS expects a character value. The numeric variable is written with the BEST12. format and the resulting character value is right-aligned when the conversion occurs. In this example, the value of Phonenumber is converted to character and right-aligned before the SUBSTR function is performed. Since there are only 10 digits in the value of Phonenumber, the right-aligned value begins with two blanks. Therefore the SUBSTR function picks up two blanks and a 3, and uses the BEST12. format to assign that value to Code. Then, the parentheses are concatenated before and after the two blanks and a 3.

  4. Ram - 02/19/2012 at 10:47 am

    Hey I Can understand your logic..But how did u detect that Numeric Value is Written with best12 format…Pls clear that point alone..

  5. Vaibhav - 05/7/2012 at 2:44 am

    Thanks a lot… I was confused with results when applied substr to numeric variables

  6. Evan - 10/1/2012 at 2:43 pm

    wow. good one.

  7. hh - 02/22/2014 at 9:50 am

    Very clear and complete explanation! Thanks admin!!

Leave a Comment

Leave a Reply

Your email address will not be published.