in the following codes
libname sasdata "sas_library";
data test;
set data.chemist (keep =job-code);
if job_code='chem3'
then description = 'senior chmistor';
run;
if job_code is a character with a length of 6 bytes, what is the length of variable description? The answer is 14 bytes. I do not know how to get it.
@anming wrote:
in the following codes
libname sasdata "sas_library";
data test;
set data.chemist (keep =job-code);
if job_code='chem3'
then description = 'senior chmistor';
run;
if job_code is a character with a length of 6 bytes, what is the length of variable description? The answer is 14 bytes. I do not know how to get it.
First, do you really have a length of 14 and not 15? 15 is what I get when I imitate your code, assuming description is a new variables.
Second, is description already in data.chemist? If so, then the above DATA TEST step will not modify it.
Third, have you shown us all the code in this step/
And finaly, why do you expect the length of job_code to have an impact on the length of decription? After all, job_code is not part of the value assigned to description.
To take it a little further, what if you had something like
if job_tenure>10 then
then description = 'senior chmistor';
This is a case where job_tenure is not even a character variable - so there would be no way to imagine it having a length that would impact the length of description.
First, I should not have bothered asking you about the possibility of description being an incoming variable, since you had a "keep=job_code" parameter.
As to your question, SAS sees no length statement for the new variable description, but it does see that the value you assign to it contains 14 characters, so it stores the variable with a length of 14. It would not matter if a subsequent assignment required a differing number of letters, because the compiler's first encounter with variable description fixes the length. Shorter values will have trailing blanks, and longer values will be truncated.
Consider the two steps below.
data test;
set data.chemist (keep =job_code);
if job_code='chem3' then description = 'Senior Chemist';
else if job_code='???' then description='Other';
run;
data test2;
set data.chemist (keep =job_code);
if job_code='???' then description = 'Other';
else if job_code='chem3' then description='senior chemist';
run;
In DATA TEST, there is another possible value assignment ('Other' requiring 5 bytes). TEST will have description with 14 characters. But TEST2, which is logically equivalent to TEST will store description with a length of 5, because the first instance of a value assignment only takes 5 letters. It won't matter that a later assignment needs 14 - only the first 5 will be stored in the data set.
@anming wrote:
I checked it. Guess some messages should be given. There is no info about data.chemist, it is probably hard to figure out a proper answer.
In fact, it is not hard at all, because the data-step in the task contains all information necessary to answer the question.
libname sasdata "sas_library";
data test;
set data.chemist(keep= job_code);
if job_code = 'chem3' then description = 'senior chmistor';
run;
@anming wrote:
Great! my confusion is that bytes. does 14 characters of 'senior chemist' including one space mean 14 bytes? Thanks!
In most cases. All of the characters in that string are standard 7-bit ASCII characters so each one needs just one byte whatever encoding you are using. The exception is if your SAS session is using double byte encoding (an pre UTF-8 method mainly used for Asian languages) in which case the length would be 28 bytes.
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
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.