BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
riyaaora275
Obsidian | Level 7

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
RW9
Diamond | Level 26 RW9
Diamond | Level 26

"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

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

ruchi11dec
Obsidian | Level 7

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

Amir
PROC Star

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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