Hi Experts,
How to find maximum length variable name
@Tom said: "Try using different words to express what you want." And then @pavank replied: "I want maximum variable name and length". Essentially the same words.
@pavank When someone asks for information, or when someone asks you to clarify, repeating the same information is not helpful, and doesn't move us closer to helping you get an answer.
Is this what you are interested in? ==> Find the variable name with the most letters in the variable name <== (note: different words used)
proc contents data=sashelp.cars noprint out=_contents_;
run;
proc sql;
create table num_letters as select name from _contents_
having length(name)=max(length(name));
quit;
Are you looking to find the maximum length of a variable in your data set?
Or are you seeking to find the maximum length that you can use for a variable in a data set?
I want which variable has max length with that variable name
Is this what you want?
proc contents data=datasetname noprint out=_contents_;
run;
proc sql;
create table max_length as select * from _contents_
having length=max(length);
quit;
@pavank wrote:
I want which variable has max length with that variable name
Where? Does this question relate to a single data set or multiple data sets in a library?
For a given single data set that question is essentially meaningless as phrased as there is only on variable with a given name. If want to know the maximum length value for a given variable that makes a bit more sense but may have multiple values.
For discussion purposes let's use the data set SASHELP.CLASS since that should be available to you.
Now for the variable with the catchy name of "NAME" what would the answer to your question be?
I want maximum variable name and length as bellow
DATASET MAX_Length_variable_name length
cars : MPG_Highway 11
class: Height 6
class: Weight 6
buy : Amount 6
/*i tried using dictionary.tables but i didn't get required output*/
proc sql;
select maxvar
from dictionary.tables
where
libname='SASHELP' and memname='CARS';
quit;
If you want information about variables you look in Dictionary.columns.
None of the variables in SASHELP.CLASS have a length of 6. Height and Weight have a length of 8, the default for numeric values.
So, how do you get length of 6 for Height and Weight from SASHELP.Class?
Similarly the length of MPG_Highway in SASHELP.CARS is 8.
I have to think that you are thinking of something other than the SAS variable characteristic of Length which is the number of bytes used to store the value.
@pavank wrote:
I want maximum variable name and length as bellow
DATASET MAX_Length_variable_name length
cars : MPG_Highway 11
class: Height 6
class: Weight 6
buy : Amount 6
/*i tried using dictionary.tables but i didn't get required output*/ proc sql; select maxvar from dictionary.tables where libname='SASHELP' and memname='CARS'; quit;
@Tom said: "Try using different words to express what you want." And then @pavank replied: "I want maximum variable name and length". Essentially the same words.
@pavank When someone asks for information, or when someone asks you to clarify, repeating the same information is not helpful, and doesn't move us closer to helping you get an answer.
Is this what you are interested in? ==> Find the variable name with the most letters in the variable name <== (note: different words used)
proc contents data=sashelp.cars noprint out=_contents_;
run;
proc sql;
create table num_letters as select name from _contents_
having length(name)=max(length(name));
quit;
@pavank wrote:
I want which variable has max length with that variable name
Try using different words to express what you want.
Do you want to find then NAME of the variable in a dataset that is defined with the largest storage length? You can just query the metadata.
Do you want the find the NAME of the variable that has the longest actual value? I would assume here you are talking about only the character variables? Do you want to do this separately for each OBSERVATION? Or do you want to find the variable that has the longest value across all of the observations?
And what do you want in case of ties?
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
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.