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?
Maxim 4: Try It.
This is the best way to put your mind at ease.
@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.
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?
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)
Threads with identical questions merged.
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.
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.
@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.
As you can see in https://communities.sas.com/t5/forums/editpage/board-id/certification/message-id/729 , "practice exams" from non-SAS sources can be misleading, causing you to fail in the actual exam.
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?
The first attribute seen for a variable is the one used in the current data step. Given that the Work.Sales data set is positioned first on the MERGE statement, the variable Name would have a format of $CHAR15. in the new data set Work.Both. The LENGTH statement only gives the variable Name a predefined maximum length.
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.
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.