I am trying to create a retain variables in a statement that is over 6000 characters long and getting an error - please help!
What error are you getting? Show us the ENTIRE log for the data step in which you are using RETAIN — do not show us a partial log. Copy the log as text, paste it into the window that appears when you click on the </> icon.
There is no log. Trying to order the variables on the output and there are 998 variables.
What happens if you split this long list of variables across many lines?
That error message refers to length of a single line of code. Can't exceed 6000 characters. So your Subject line is incorrect.
Use multiple Length statements. They will be processed in order of appearance in the code so
Length var1 var2 var3 8 ; Length var15 var16 var17 $ 5;
is equivalent to
Length var1 var2 var3 8 var15 var16 var17 $ 5;
But I still don't see why so many people spend so much time worrying about the order of variables in a data set...
@ballardw wrote:
But I still don't see why so many people spend so much time worrying about the order of variables in a data set...
I think it is unnecessary and a waste of effort as well, but its a rare event when I can convince someone who is determined to order the variables in a data set to NOT do that.
The reason for the order is because it is for a data submission that has this requirement.
The variables are in the dataset and need to be put out to a file in this order. Can I use a length statement even with the variables already created?
@ballardw wrote:
But I still don't see why so many people spend so much time worrying about the order of variables in a data set...
I do. Because
Yes, it's possible to overstress variable order, but it's not a worthless objective.
No need to make separate statements. Just make separate lines.
PS: I don't understand why people spend so much time making datasets with variables in some random order (or some even worse order) that makes it almost impossible to look at the data. It takes almost no effort to define the variables in a rational order.
So your SAS installation doesn't want statements longer than 6000 characters, and you apparently have some statement (maybe a LENGTH statement since that's what shows in your popup window) longer than 6000 characters.
So make multiple LENGTH statements, each no more than 6000 characters.
The message is not about the length of a VARIABLE or even the length of the value of a variable.
It is about the length of line in the file that has the code.
So when you write the code to the file split the long lines into multiple lines. SAS does not care how long the STATEMENT is, but it obviously cannot reads lines of code that have over 6000 characters (at least using whatever method you used to submit the code).
But in practical terms you should not be making lines of code that is longer than about 70 to 75 characters anyway so that HUMANS can read them.
Also your code is using a LENGTH statement, not a RETAIN statement. If the purpose of the RETAIN statement was just to force the ORDER then it must come BEFORE the variables are defined by a LENGTH or a SET or any other statement that would set the order, type and length of the variable.
How did you create that giant LENGTH statement?
If you created from a source dataset then just limit the length of the lines while writing the statement.
So for example if you have a dataset named HAVE and a metadata dataset named VARIABLES you can use the two of them to create a dataset named WANT that has the variables in the order they appear in VARIABLES with code like this:
filename code temp;
data _null_;
set variables end=eof;
file code lrecl=75 ;
if _n_=1 then put 'RETAIN ' @;
put name @ ;
if eof then put / ';' ;
run;
data want;
%include code / source2;
set have;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.