BookmarkSubscribeRSS Feed
AdrianXavier
Calcite | Level 5

Hi everyone! Can someone help me with this question? I took a practice SAS exam a couple days ago and there was this question that is absolutely driving me insane. The question was about using a length statement before the merge statement. I have always thought that if you add the length statement before the merge then the length statement chooses the length of the column. However, I got the answer wrong because apparently SAS uses the first table in the merge statement to choose the length and not the length statement even though it was written before. I feel like I am missing something here. Can someone help?

 
12 REPLIES 12
ballardw
Super User

@Kurt_Bremser wrote:

Maxim 4: Try It.

This is the best way to put your mind at ease.


For clarity  of result you likely would want to make sure that the length statement in the code uses a different length than in either of the data set and the common variable should have different defined lengths as well.

 

And try a length in the statement 1) longer than in either set 2) shorter than in either set and possibly even 3) in between the lengths in the data sets.

MasonLogan
Calcite | Level 5

Hi everyone! Can someone help me with this question? I took a practice SAS exam a couple days ago and there was this question that is absolutely driving me insane. The question was about using a length statement before the merge statement. I have always thought that if you add the length statement before the merge then the length statement chooses the length of the column. However, I got the answer wrong because apparently SAS uses the first table in the merge statement to choose the length and not the length statement even though it was written before. I feel like I am missing something here. Can someone help?

 
PaigeMiller
Diamond | Level 26

DUPLICATE THREAD

 

DO NOT REPLY HERE

 

Reply at https://communities.sas.com/t5/SAS-Studio/Length-statement-before-merge-statement/td-p/613780 (where, by the way, a good answer has been provided)

--
Paige Miller
Tom
Super User Tom
Super User

SAS will define the variable when it first needs to. So you are right that the LENGTH statement will set the type and length of the variable. The question is either wrong or you misunderstood what point they were trying to make.

Kurt_Bremser
Super User

Practical example of "try it":

data t1;
length x1 $10;
input x1;
datalines;
aaaaaaaa
bbbbbbbb
;

data t2;
length x1 $15;
input x1;
datalines;
cccccccccc
dddddddddd
;

data test1;
length x1 $5;
merge t1 t2;
run;

Replace the length in the final data step with arbitrary values, and inspect the resulting table. You will find that the value from the length statement always takes precedence.

Please post the exam question and its answer for clarification.

ballardw
Super User

@AdrianXavier wrote:

Hi everyone! Can someone help me with this question? I took a practice SAS exam a couple days ago and there was this question that is absolutely driving me insane. The question was about using a length statement before the merge statement. I have always thought that if you add the length statement before the merge then the length statement chooses the length of the column. However, I got the answer wrong because apparently SAS uses the first table in the merge statement to choose the length and not the length statement even though it was written before. I feel like I am missing something here. Can someone help?

 

Example data.

Example code

Exact test of question and/or provided code.

 

Without concrete examples what you thought you read may not be what we think you are saying.

And some "tests" are wrong with the answers.

jrboyle
Calcite | Level 5

This question is from the SAS Certification Practice Exam: SAS 9.4 Base Programming Performance-Based Exam (Q 29) via SAS e-learning/vle:

 

The variable Name in the data set Employee has a $CHAR10. format. The variable Name in the data set Sales has a $CHAR15. format. 

The following SAS program is submitted:

data both;
   length name $ 20;
   merge sales employee;
   by id; 
run;
What is the format for the variable Name in the data set Both?
 
Select one:
A. $20 
B. $CHAR10
C. $CHAR15
D. $CHAR20
Feedback
Tom
Super User Tom
Super User

If that is the question that the original poster was talking about then the last sentence in the answer explains the confusion that the original poster has. 

 

The format attached to a variable is independent of the length used to store that variable. 

 

The only place they intersect is when SAS is trying to figure out how to define the variable from the code of your data step. If the first place the variable is referenced is in a FORMAT statement (or INFORMAT statement or the FORMAT= or INFORMAT= attribute of an ATTRIB) then SAS will make the assumption/guess that you want the variable defined to have a type that is compatible with the format/informat and that the length should match the width of the format/informat specification.

Kurt_Bremser
Super User

So the question is about a FORMAT, not a LENGTH.

This once again illustrates the importance of posting exact, precise questions.

Maxim 42 in action.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 12 replies
  • 1022 views
  • 3 likes
  • 7 in conversation