BookmarkSubscribeRSS Feed
anming
Pyrite | Level 9

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.

7 REPLIES 7
mkeintz
PROC Star

@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.

--------------------------
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

--------------------------
anming
Pyrite | Level 9
Sorry, there is a typo in my original codes. It should be "senior chemist". This is a question I copied from one exercise for SAS base certificate. I can not understand how the length of the variable "description" is defined. I can also simulate this piece of codes.
mkeintz
PROC Star

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.

--------------------------
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

--------------------------
anming
Pyrite | Level 9
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.
andreas_lds
Jade | Level 19

@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;

 

  1. The SET-statement with keep tells, that only one variable is read form data.chemist. => No type/length information for "description".
  2. The data-step has neither length nor attrib statement defining "description" => length and type are defined by the first assignment => count the number of chars to answer the question.

 

 

anming
Pyrite | Level 9
Great! my confusion is that bytes. does 14 characters of 'senior chemist' including one space mean 14 bytes? Thanks!
Tom
Super User Tom
Super User

@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.

sas-innovate-white.png

Special offer for SAS Communities members

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.

 

View the full agenda.

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
  • 7 replies
  • 1179 views
  • 2 likes
  • 4 in conversation