Obsidian | Level 7

## Assigning vale to a variable- inconsistent results

The below code gives the result 3

``````data test4;
x='1';
x='2';
x='3';
run;``````

But the below code gives the result 1, why is it not 111?

``````data test5;
x='1';
x='11';
x='111';
run;``````

And the below code gives the result 1. Why is it 1?

`data t;x='2';x='3';x='111';run;`

Why do we get such different results?

1 ACCEPTED SOLUTION

Accepted Solutions
Diamond | Level 26

## Re: Assigning vale to a variable- inconsistent results

"Why do we get such different results? " - because you have not read the SAS Help documentation on these core concepts.

1) You get 3 and only three output as there is an implied output at the end of the loop, as none is specified.  So X gets set to 1, then to 2, then to 3 and the row is then output.  Fix:

``````data test4;
x='1'; output;
x='2'; output;
x='3'; output;
run;``````

2) In this one, X is a new variable, and you have not set a length for the variable.  Therefore it is assigned implicitly based on the first data, which is character length 1.  So X gets set to 1, then to 1 again, as it only takes the first character, then to one again, as it only accepts 1 character.  Fix:

``````data test5;  length x \$3;
x='1';
x='11';
x='111';
run;``````

3) This question is exactly the same as 2.

Goo idea to learn the basics on these types of things, and also a very good example of why not to let the computer try to guess what you want.  Always be explicit in your definitions and program, never rely on the computer to guess anything for you.

3 REPLIES 3
Diamond | Level 26

## Re: Assigning vale to a variable- inconsistent results

"Why do we get such different results? " - because you have not read the SAS Help documentation on these core concepts.

1) You get 3 and only three output as there is an implied output at the end of the loop, as none is specified.  So X gets set to 1, then to 2, then to 3 and the row is then output.  Fix:

``````data test4;
x='1'; output;
x='2'; output;
x='3'; output;
run;``````

2) In this one, X is a new variable, and you have not set a length for the variable.  Therefore it is assigned implicitly based on the first data, which is character length 1.  So X gets set to 1, then to 1 again, as it only takes the first character, then to one again, as it only accepts 1 character.  Fix:

``````data test5;  length x \$3;
x='1';
x='11';
x='111';
run;``````

3) This question is exactly the same as 2.

Goo idea to learn the basics on these types of things, and also a very good example of why not to let the computer try to guess what you want.  Always be explicit in your definitions and program, never rely on the computer to guess anything for you.

Obsidian | Level 7

## Re: Assigning vale to a variable- inconsistent results

since you are defining x as a charaacter and there is no length statement.. so it takes the length of x as the first statement it encounters.

if your second program would be :

data test5;
x='1';
x='11';
x='311';
run;

u would have got the result 3.

And if you write it as :

data test5;
length x \$ 3.;
x='1';
x='11';
x='311';
run;

you would get 311

PROC Star

## Re: Assigning vale to a variable- inconsistent results

Hi,

A new character variable's length is determined by the first assignment (if it has not been determined explicitly, e.g. by a length statement.)

So in the data step for test5, x='1' sets the length of x to 1 character.

Try using a length statement before the assignment, e.g.:

``````data test5;
length x \$ 8;
x='1';
x='11';
x='111';
run;``````

Regards,

Amir.

Discussion stats
• 3 replies
• 917 views
• 1 like
• 4 in conversation