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)
Topics:
SAS Base Questions |
13 Comments »
A
why not concatenate?
Why this is not C as for the concatenation operator?
I got it~ it’s about the length~~
Who could tell me the reason? Thanks.
please explain
can some one please explain why it can not be B??
At the beginning length of JobCategory was already set to 2. Output showed only “FA”.
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;
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;
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…
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
cuz it defined jobCategory twice .
btw. rob go grab some books before start on the questions please