By admin | September 18, 2009

SAS Certified Base Programmer 123 Questions (59)

The following SAS program is submitted:

data work.staff;
    JobCategory= 'FA';
    JobLevel= '1';
    jobCategory= JobCategory || JobLevel;
run;

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

A. FA
B. FA1
C. FA 1
D. ‘ ‘ (missing character value)

13 comments | Add One

  1. admin - 09/18/2009 at 4:17 pm

    A

  2. Bill - 02/21/2012 at 11:37 am

    why not concatenate?

  3. jie - 03/2/2012 at 6:55 pm

    Why this is not C as for the concatenation operator?

  4. jie - 03/2/2012 at 8:39 pm

    I got it~ it’s about the length~~

  5. Crystal - 07/30/2012 at 10:48 pm

    Who could tell me the reason? Thanks.

  6. rob roy - 09/3/2012 at 4:16 am

    please explain

  7. Himanshu - 09/6/2012 at 2:31 pm

    can some one please explain why it can not be B??

  8. Khai Ming - 11/13/2013 at 5:52 pm

    At the beginning length of JobCategory was already set to 2. Output showed only “FA”.

  9. hh - 02/20/2014 at 7:36 pm

    I don’t think it’s the length because when you enter this code in SAS, jobCategory1 concatenates successfully and has a value of ‘FA1’. I guess you cannot use the same variable name when you concatenate?

    data work.staff9;
    JobCategory= ‘FA’;
    JobLevel= ‘1’;
    jobCategory1= JobCategory||JobLevel;
    run;

  10. hh - 02/24/2014 at 12:04 pm

    Got explanation from others:

    Rules:

    SAS variables get their length from their first use/mention. Without the length statement, JobCategory gets length = 2.

    SAS character variables have a fixed length. With length JobCategory $20 the value after assignment is “FA__________________”.

    The concatenate operation generates a string of length 21 which gets chopped back to length 20 when assigned to string JobCategory.

    Try this

    data work.staff;
    length JobCategory $20;
    JobCategory =’FA’;
    JobLevel =’1′;
    JobCategory = JobCategory || JobLevel;
    JobCategory = trim(JobCategory) || JobLevel;
    run;

  11. su - 04/30/2014 at 4:36 pm

    the length is specified in the first statement for the concatenation to be working the name of the new variable should be something else….. hope i explained it…

  12. Fleevi - 07/9/2014 at 6:02 pm

    Adding
    length jobcategory $3;

    and changing to
    jobCategory= trim(JobCategory) || JobLevel;

    would have the desirable result. Any excessive character would be truncated. In the original program, the concatenation would give ‘FA1’ while the length of variable jobCategory is 2 (‘FA’ appears first). So the ‘1’ in ‘FA1’ will be dropped.
    For the same reason, trim() is needed to remove trailing space so concatenation wouldn’t assign ‘FA 1’ to a 3-char long var

  13. robsocute - 09/3/2014 at 1:05 am

    cuz it defined jobCategory twice .

    btw. rob go grab some books before start on the questions please

Leave a Comment

Leave a Reply

Your email address will not be published.