SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jcrepper
Calcite | Level 5

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?

 

length question sas.png

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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

View solution in original post

5 REPLIES 5
japelin
Rhodochrosite | Level 12

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;
Tom
Super User Tom
Super User

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.

jcrepper
Calcite | Level 5

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?

Tom
Super User Tom
Super User

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

 

Kurt_Bremser
Super User

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

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 5 replies
  • 4110 views
  • 4 likes
  • 4 in conversation