- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
While studying for base exam I encountered this question. I had thought that the length statement before the merge step would allow for a character length to be set for a variable. Not sure why the question does not reflect that. Am I missing something?
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@jcrepper wrote:
Thanks for your reply. What would be an example of a proper way to create the data with a name variable length of 20 when merging?
The variable is created with a length of 20, but only 15 characters are displayed because of the format inherited from the first dataset.
Use a separate FORMAT statement to either remove any format, or set the format to CHAR20.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The length statement does indeed create a variable with a length of $20, but that alone is not enough because the format is empty.
The subsequent merge statement will reflect the $CHAR15. format of the sales dataset to be read first.
You can see this by executing the following steps.
The variable a in dataset TEST1 has an empty format, and the variable a in dataset TEST2 has a format of date9..
As shown in results1, only when the format is empty, the format to be read later will be reflected.
data test1;
a=1;x=3;
run;
data test2;
a=1;y=2;
format a date9.;
run;
data results1;
merge test1 test2;
by a;
run;
proc print noobs;
run;
data test3;
a=100000;x=3;
format a time5.;
run;
data test4;
a=100000;y=2;
format a time8.;
run;
data results2;
merge test3 test4;
by a;
run;
proc print noobs;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Which FORMAT specification to permanently attach to a variable is a totally separate attribute of a variable than the LENGTH of the variable.
Just because you defined the variable be character with a length of 20 does not prevent you from using any of the formats listed in the question and answer with that variable.
Because the data step does not explicit attach any format specification the data step compiler will use the first non empty setting that it sees. In this case it is the format attached to the variable in the SALES dataset.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks for your reply. What would be an example of a proper way to create the data with a name variable length of 20 when merging?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@jcrepper wrote:
Thanks for your reply. What would be an example of a proper way to create the data with a name variable length of 20 when merging?
Why would you do anything different than what the posted code?
If you want to remove the format from name use a format statement.
data both;
length name $20 ;
set sales employee;
by id;
format name ;
run;
A FORMAT in SAS is special instructions for how to print the data. SAS does not need any special instructions for how to print character variables.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@jcrepper wrote:
Thanks for your reply. What would be an example of a proper way to create the data with a name variable length of 20 when merging?
The variable is created with a length of 20, but only 15 characters are displayed because of the format inherited from the first dataset.
Use a separate FORMAT statement to either remove any format, or set the format to CHAR20.