DATA Step, Macro, Functions and more

Assigning vale to a variable- inconsistent results

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

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? 


Accepted Solutions
Solution
a month ago
Super User
Super User
Posts: 9,427

Re: Assigning vale to a variable- inconsistent results

[ Edited ]
Posted in reply to riyaaora275

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

View solution in original post


All Replies
Solution
a month ago
Super User
Super User
Posts: 9,427

Re: Assigning vale to a variable- inconsistent results

[ Edited ]
Posted in reply to riyaaora275

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

Occasional Contributor
Posts: 17

Re: Assigning vale to a variable- inconsistent results

Posted in reply to riyaaora275

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

 

Hope I answered your query

Super Contributor
Posts: 324

Re: Assigning vale to a variable- inconsistent results

Posted in reply to riyaaora275

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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