DATA Step, Macro, Functions and more

problem while concatenation using ||

Reply
New Contributor
Posts: 4

problem while concatenation using ||

[ Edited ]

Please see the below SAS code and provide a explanation on why the value of variable jobcategory is "FA" only

 

data staff;
jobcategory = 'FA';
joblevel = '1';
jobcategory = jobcategory||joblevel;
run;

I am including a Length statement after data statement as "Length jobcategory $20.", still answer is same "FA".

 

but we are getting correct value once the output variable is changed to "JOBCATEGORY1" as shown below

data staff;
length jobcategory1 $15.;
jobcategory = 'FA';
joblevel = '1';
jobcategory1 = jobcategory||joblevel;
run;

Thanks in advance

Super User
Posts: 17,840

Re: problem while concatenation using ||

This is correct, because of trailing spaces.

 

In the first example it creates jobcategory with a length of 2 and then there's no room for the remaining text.

In the second example it creates jobcategory with a length of 15 - 2 chars and trailing spaces. 

 

I would assign a length at the beginning and use a CATT() function instead as it deals with the trailing spaces and I don't have to think about them. 

 

data staff;
length jobcategory $15.;
jobcategory = 'FA';
joblevel = '1';
jobcategory = catt(jobcategory, joblevel);
run;

proc print;run;
New Contributor
Posts: 4

Re: problem while concatenation using ||

 you want to say that when i assign the length as 15 initially, it includes  traling spaces also?.

So  length is a compiling time statement?

Trusted Advisor
Posts: 1,383

Re: problem while concatenation using ||

When first occurence of a variable is assignment of a string, like:

    data out;

       var = 'STRING';

       ....

then the length of that variable is the length of the string (in this example 6 chracters);

when you try to assign a longer string it is truncated.

 

To solve it you need define a length - the maximum expected for that variable;

        data out;

             length var $10;

             var = 'STRING';  output;

             var = 'Longer one'; output;

             var = 'Too mutch long string';  output;

       run;

 

      check out and you will see that the 3rd line is truncated to 10 characters only;

Ask a Question
Discussion stats
  • 3 replies
  • 197 views
  • 1 like
  • 3 in conversation