- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi, I'm preparing for SAS Base exam and I am having trouble understanding the logic behind the answer below.
Correct answer is A and I also confirmed this to be true by testing the below code in SAS.
However, I cannot think of a good reason why the variable is not concatenating.
I wondered if this has to do with the length of variable JobCategory, so I inserted a line Length JobCategory $100; in the 2nd line, but I still get 'FA' instead of answer choice C when I run the code in SAS.
Could anyone shed a light on this problem please?
Question
The following SAS program is submitted:
data work.staff;
JobCategory ='FA';
JobLevel ='1';
JobCategory = JobCategory || JobLevel;
run;
Which one of the following is the following is the value of the variable JobCategory in the output dataset?
Answer Choices
A.FA
B.FA1
C.FA 1
D.''(missing character value)
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Try this:
data work.staff;
length JobCategory $20;
JobCategory ='FA';
JobLevel ='1';
JobCategory = JobCategory || JobLevel;
put JobCategory;
JobCategory = trim(JobCategory) || JobLevel;
put JobCategory;
run;
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.
Hth.
PG
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Try this:
data work.staff;
length JobCategory $20;
JobCategory ='FA';
JobLevel ='1';
JobCategory = JobCategory || JobLevel;
put JobCategory;
JobCategory = trim(JobCategory) || JobLevel;
put JobCategory;
run;
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.
Hth.
PG
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you so much for your prompt response and clear explanation!
I didn't think about trailing blanks in JobCategory variable, but now I understand why '1' got chopped off.
I also tried running your code, but it worked without put statement too.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Understanding the concatenation operator || is important for interpreting older SAS code. But for contemporary code, it has been mostly replaced with functions catx, cats and catt which provide better functionality. Make sure you understand those as well.
PG
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I tried using the following code, however, it only shows FA. I'm preparing for the base certification exam, any advise?
data work.staff;
length JobCategory $ 20.;
JobCategory ='FA';
JobLevel ='1';
JobCategory = JobCategory || JobLevel;
run;
However, the following code works instead of using || . I still do not understand why || is not working.
Catx(,JobCategory,JobLevel);
catt(JobCategory, JobLebel);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Please read the answer marked 'correct' carefully, run the code, try to understand the outcome. For catx, catt, you need to read the manual on how they process the variable content (especially on how they treat the trailing blanks) during the concatenation. Answers are all there for a prepared pair of eyes.