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
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;
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?
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.