09-04-2016 02:12 PM - edited 09-04-2016 02:14 PM
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
09-04-2016 02:23 PM
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;
09-04-2016 02:24 PM
When first occurence of a variable is assignment of a string, like:
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;
length var $10;
var = 'STRING'; output;
var = 'Longer one'; output;
var = 'Too mutch long string'; output;
check out and you will see that the 3rd line is truncated to 10 characters only;