Help using Base SAS procedures

logic behind concatenating variables

Accepted Solution Solved
Reply
Contributor
Posts: 35
Accepted Solution

logic behind concatenating variables

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
Solution
‎02-22-2014 02:49 PM
Respected Advisor
Posts: 4,747

Re: logic behind concatenating variables

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

PG

View solution in original post


All Replies
Solution
‎02-22-2014 02:49 PM
Respected Advisor
Posts: 4,747

Re: logic behind concatenating variables

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

PG
Contributor
Posts: 35

Re: logic behind concatenating variables

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.

Respected Advisor
Posts: 4,747

Re: logic behind concatenating variables

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

PG
New Contributor
Posts: 3

Re: logic behind concatenating variables

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);

Respected Advisor
Posts: 3,147

Re: logic behind concatenating variables

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.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 699 views
  • 0 likes
  • 4 in conversation