BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
anming
Pyrite | Level 9
data staff;
jobcategory='FA';
joblevel='4';
jobacategory=jobcategory||joblevel;
run;

for the code to concatenate two strings jobcategory only outputs 'FA'. if change the code to jobcategory1=jobcategory||joblevel, jobcategory1 will be assigned to 'FA4'. 

Why can not assign a new value to the variable of jobcategory through the code above? 

 

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

Because the first reference to jobcategory is the assignment of the two-character literal value "FA", SAS assigned a length of $2 to the variable.  So while the concatenation operator was honored by SAS, it only could fit the first two characters of the result into the variable.

 

But the new variable jobcategory1 will be assigned a length of $3, since that is the sum of $2 (for jobcategory) and $1 (for joblevel).  

 

BTW, if instead of 

jobcategory1=jobcategory||joblevel;

you had used one of the concatenation functions, 

jobcategory1=cat(jobcategory,joblevel);

you would get the same resulting values, but SAS would assign a length of $200 to jobcategory.

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

3 REPLIES 3
mkeintz
PROC Star

Because the first reference to jobcategory is the assignment of the two-character literal value "FA", SAS assigned a length of $2 to the variable.  So while the concatenation operator was honored by SAS, it only could fit the first two characters of the result into the variable.

 

But the new variable jobcategory1 will be assigned a length of $3, since that is the sum of $2 (for jobcategory) and $1 (for joblevel).  

 

BTW, if instead of 

jobcategory1=jobcategory||joblevel;

you had used one of the concatenation functions, 

jobcategory1=cat(jobcategory,joblevel);

you would get the same resulting values, but SAS would assign a length of $200 to jobcategory.

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Tom
Super User Tom
Super User

Don't force SAS to guess how you want your variables defined, especially character variables. Tell it explicitly what length to use.

data staff;
  length jobcategory $3 joblevel $1 ;
  jobcategory='FA';
  joblevel='4';
  jobacategory=jobcategory||joblevel;
run;
anming
Pyrite | Level 9

Note that you type 'jobacategory' instead of 'jobcategory'. If jobcategory it is strange that the code still outputs only 'FA'.

 

data staff;
  length jobcategory $3 joblevel $1 ;
  jobcategory='FA';
  joblevel='4';
  jobcategory=jobcategory||joblevel;
run;

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 993 views
  • 1 like
  • 3 in conversation