Create the length for variables

Reply
Contributor
Posts: 68

Create the length for variables

data temp;

length a 1 b 3 x;

infile 'file reference';

input a b x;

run;

What is the result?

A.The data set TEMP is created, but variable X is not created.

B. The data set TEMP is created and variable X has a length of 8.

C.The data set TEMP is not created because variable A has an valid length.

D. The data set TEMP is not created because variable A and B have invalid lengths.

The answer is C.

I couldn't understand the answer at all.

I also run the code, the result is that the data set TEMP is created with 0 observation, the length of variable a b x are 8 3 8.

SO the correct answer is B? Can anyone please kindly confirm the answer with me?

By the way, why the length of a cannot be set as 1? Thank you guys!

Regular Contributor
Posts: 217

Re: Create the length for variables

length a 1 b 3 x;

The statement above creates three numeric variables, a, b and x.  A cannot be 1 because the minimum length for a numeric variable is 3.

The statement below creates one character variable and two numeric variables.  The default length of a numeric variable is 8.

length a $ 1 b 3 x;   (equivalent to: length a $ 1 b 3 x 8Smiley Wink

Contributor
Posts: 68

Re: Create the length for variables

Hi Jiwillis, how do you know it is "a" that should be the character variable? not "b" or "X"?

Super User
Posts: 19,155

Re: Create the length for variables

Errors out for me.

data temp;

length a 1 b 3 x;

input a b x;

cards;

2 5 8

3 4 5

13 2 3

;

run;

LOG:

246  data temp;

247

248  length a 1 b 3 x;

              -      -

              352    22

1    1

     -

     352

ERROR 352-185: The length of numeric variables is 3-8.

ERROR 22-322: Expecting a numeric constant.

249

250  input a b x;

251  cards;

NOTE: The SAS System stopped processing this step because of errors.

WARNING: The data set WORK.TEMP may be incomplete.  When this step was stopped there were 0

         observations and 3 variables.

NOTE: DATA statement used (Total process time):

      real time           0.00 seconds

      cpu time            0.00 seconds

255  ;

256

257  run;

Contributor
Posts: 68

Re: Create the length for variables

Hi Reeza,

Even if I changed the length of variable a to 3, error still exists. By the way, if the data set is created with error, whether should we call the data set is created or not created? Thank you.

12   data temp;
13   length a 3 b 3 x;
                     -
                     22
1    1
     -
     352
ERROR 22-322: Expecting a numeric constant.

ERROR 352-185: The length of numeric variables is 3-8.

14   input a b x;
15   cards;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.TEMP may be incomplete.  When this step was stopped there were 0
         observations and 3 variables.
WARNING: Data set WORK.TEMP was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

19   ;


20   run;

Super User
Posts: 11,134

Re: Create the length for variables

Do you by chance have a typo for answer C and should read "A has an invalid length"?

Valued Guide
Posts: 3,208

Re: Create the length for variables

Sas is using floating point for numbers. Remember the slider.

By that avoid the misperception on counting the digits you see. They are meaningless for the numeric length. The minimum storage used in bytes is often 3 sometimes 2 never 1.

---->-- ja karman --<-----
Contributor
Posts: 68

Re: Create the length for variables

Thanks Jaap!

Contributor
Posts: 68

Re: Create the length for variables

Thanks Jaap!

However, even if I changed the length of variable a to 3, error still exists. By the way, if the data set is created with error, whether should we call the data set is created or not created? Thanks again.

Valued Guide
Posts: 3,208

Re: Create the length for variables

Chouchou, The length statement is defining: variable(s) length variable(s) length    etc. Those are always two words and the length can be numeric or string (indicated by$).
The errors are often displayed with a  letter _  at the position the parser says it is wrong. With a monospace font (old hollerith cards) this will be aligned. In your post here it is a variable font. That _22 is at the position of the semicolon ;

It is telling then numeric constant after the x is missing.  

---->-- ja karman --<-----
Contributor
Posts: 68

Re: Create the length for variables


Hi Jaap,

Thank you for your quick reply. However I still don't understand why errors come out even if I changed the length of variable a to 3?

Can you please elaborate it for me, I would be greatly appreciate your help.

Valued Guide
Posts: 3,208

Re: Create the length for variables

chouchou ,

I copied your log but only changed the character type to "courier new"

see where the error occurs not at the variable a but after x

12   data temp;
13   length a 3 b 3 x;
                     -
                     22
1    1
     -
     352
ERROR 22-322: Expecting a numeric constant.

ERROR 352-185: The length of numeric variables is 3-8

This should give no errors. It is not useful caused by floating point properties.

data temp;

length a 3 b 3 x 5 ;

---->-- ja karman --<-----
Super User
Super User
Posts: 6,845

Re: Create the length for variables

Are you sure you copied the code from the question properly?

The addition of X to the LENGTH statement without a length following it is also an error and so none of answers is 100% correct.

But don't nit-pick the answers. 

C is still the best answer as it is the first error that will cause the data step to not run. 

A can be eliminated because just the inclusion of X in the INPUT statement will cause it to be created.

D can be eliminated because there is nothing wrong with length specified for B.

The intent of the step was to read the raw data and make a dataset.  The raw data will not be read.  Also note that if a dataset named TEMP already existed then it will not be replaced because of the error. So B can be eliminated.

Ask a Question
Discussion stats
  • 12 replies
  • 2056 views
  • 8 likes
  • 6 in conversation