BookmarkSubscribeRSS Feed
MG18
Lapis Lazuli | Level 10

In the DATA step below, what is the length of the new variable, Type?

data orion.newloan; 
   set orion.records; 
   TotalPaid=sum(TotLoan+Interest); 
   if Code='1' then Type='Fixed'; 
   else Type='Variable'; 
   length Type $ 10; 
run;

 

 a.  5
 b.  8
 c.  10
 d.  It depends on the first value of Type in orion.records.

 

 

What is its answer and how ??  i think D is the correct answer.Please tell me 

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

Is there a variable called type in: 

   set orion.records; 

?  If so it will inherit the length from that dataset.  If it does not exist in that dataset then the SAS program will take the first time it encounters the variable, and set the length to the value assigned to it at that point which in this case is: 

'Fixed'

Which is 5 characters in length.  Thus in this example the answer is either:
- the length as assigned to the existing variable in orion.newloan

or

- a) 5, as that is the first appearance of type in the statements

Kurt_Bremser
Super User

It can't be D because the length is set while the data step is compiled, but the first value is available only when the data step already runs.

 

Run the step, and look at the log, as it will give you a BIG clue about what happens while the data step is compiled-

SuryaKiran
Meteorite | Level 14

Since the question says "new variable" that means it is not present in the input dataset. Since the length statement is mentioned after its length is already defined (TYPE="Fixed" length=5). For new variables if the length is not defined before then it will take the datatype from the first value ie "Fixed" and remaining values will be truncated to length 5.

 

In the log you can see a warning:

WARNING: Length of character variable Type has already been set.
Use the LENGTH statement as the very first statement in the DATA STEP to declare the length of a character variable.

Thanks,
Suryakiran

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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