BookmarkSubscribeRSS Feed
Prerna_Sharma
Calcite | Level 5

I am writing to you today to share my solution that could potentially eliminate the need for a certain function in SAS program. As you know, the LENGTH function is commonly used in SAS to set the length of a variable or character string to a specific value. However, this function can sometimes lead to data truncation issues, which can be a hassle for SAS users.

I have come up with a debug program that allows users to enter a maximum length for a variable or character string, eliminating the default maximum limit. I believe that this solution can be very helpful for SAS users and could potentially eliminate the need for the LENGTH function in the future.

I am writing to request the correct platform to share my solution with the SAS community. I believe that my solution can benefit many SAS users and I would be delighted to share it with them. Could you please provide me with the email address where I can share my solution.

3 REPLIES 3
Kurt_Bremser
Super User

Just post your solution here, for review. If it makes sense, you can later post it in the community libraries.

 

BTW, LENGTH is not a function, it is a statement.

 

The maximum length of a character variable is 32767, the default length is 8.

 

Data truncation is usually the result of faulty processes (bad documentation, use of PROC IMPORT).

Prerna_Sharma
Calcite | Level 5

Kurt Bremser, I greatly appreciate your response. I sincerely appreciate it, and I'm just giving it a try only. As I said, I'd like to discuss an SAS solution to do rid of the LENGTH statement. Although I think I have a reliable and efficient solution, I acknowledge that there may be different opinions or approaches on this topic.I want to be open and honest about my solution's limits. Despite my rigorous testing, there is still a chance that it won't function perfectly in all circumstances. I therefore ask for your opinions and ideas to improve it.
I think that developing a healthy SAS community requires open and honest communication. 

Here are the steps we can take to eliminate the LENGTH statement in your default SAS programming and replace it with a user-defined maximum length variable: One way to eliminate the LENGTH statement in SAS programming is to use a macro variable to define the maximum length of character variables in your program.

Here are the steps you can follow

 

Here is my answer to address your concerns:

 

Firstly, I apologize for any confusion caused by my previous response.I would like to clarify that LENGTH is indeed a statement in SAS, not a function. Regarding the maximum length of a character variable, the default length is indeed 8, and the maximum length is 32,767. However, it's important to note that data truncation can occur due to faulty processes, such as bad documentation or the use of PROC IMPORT.

To eliminate the need for using the LENGTH statement in SAS programs,

 

we can create a macro variable called MAXLEN and prompt the user to enter a maximum length value. This macro variable can then be used to set the length of variables or character strings as needed.

 

Here are the steps to implement this solution:

  1. Create a macro variable called MAXLEN and prompt the user to enter a maximum length value.
  2. Determine the maximum length needed for each variable or character string in your SAS program.
  3. Replace the LENGTH statement with the user-defined maximum length variable (%let max_len = [max length value];) in your SAS program.
  4. Modify any code that references the variable length to use the user-defined maximum length variable instead.
  5. Test the modified code to ensure that it is functioning properly and that the new maximum length variable is working as expected.
  6. Once testing is complete, remove all references to the LENGTH statement from the SAS program.
  7. Communicate the changes to users and ensure that they understand how to use the new maximum length variable in their programs.
  8. Update documentation to reflect the changes made to the SAS program.
  9. Incorporate the new maximum length variable into future versions of the SAS program.
  10. Monitor the SAS program for any issues related to the new maximum length variable and make any necessary adjustments.

By following these steps, SAS users can efficiently and accurately manage data without the need for manually setting the length of variables or character strings.

 

Practical example to illustrate:  how to eliminate the LENGTH statement in SAS programs using a user-defined maximum length variable.

 

Define a macro variable called MAXLEN in your autoexec.sas file that sets the maximum length value to a default value, such as 100 :

 

%let max_len = 100;

 

Modify your SAS programs to use the user-defined maximum length variable instead of the LENGTH statement. For example, if you have a data step that includes the following LENGTH statement:

 

data mydata;

length name $20;

length age 8;

input name age;

datalines;

John 35

Jane 42 ;

run;

 

You can modify it like this:

 

data mydata;

length name $&max_len.;

length age 8;

input name age;

datalines;

John 35 Jane 42 ;

run;

 

This replaces the LENGTH statement for the "name" variable with the user-defined maximum length variable "&max_len.".

 

Modify any other code that references variable length to use the user-defined maximum length variable instead of the LENGTH statement. For example, if you have another data step that merges the "mydata" dataset with another dataset using the "name" variable, you can modify the MERGE statement like this:

 

data merged;

merge mydata otherdata;

by name;

length name $&max_len.;

run;

 

Test the modified code to ensure that it is functioning properly and that the new maximum length variable is working as expected.

 

Once testing is complete, remove all references to the LENGTH statement from your SAS programs.

Communicate the changes to other SAS users and ensure that they understand how to use the new maximum length variable in their programs.

 

By following these steps, you can eliminate the need for manually setting the length of variables or character strings in your SAS programs and improve data management efficiency and accuracy.

 

 

Kurt_Bremser
Super User

So you still use the LENGTH statement, but you replace the literal value with a macro variable. This means you have to create a separate macro variable for every character variable people might read from external data, and users need to know which macro variable to use for which dataset variable.

There is really no difference between retrieving the correct length or the correct macro variable name from a data dictionary, for insertion into the LENGTH statement which needs to be coded anyway. So I can't see an advantage here.

 

Bottom line: your assertion of eliminating the LENGTH statement was incorrect.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 3 replies
  • 496 views
  • 3 likes
  • 2 in conversation