BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hatsumi
Obsidian | Level 7

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)

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

5 REPLIES 5
PGStats
Opal | Level 21

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
hatsumi
Obsidian | Level 7

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.

PGStats
Opal | Level 21

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
sbasne
Calcite | Level 5

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

Haikuo
Onyx | Level 15

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 2045 views
  • 1 like
  • 4 in conversation