BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
pavank
Obsidian | Level 7

Hi Experts,

How to find maximum length variable name 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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

 

--
Paige Miller

View solution in original post

8 REPLIES 8
maguiremq
SAS Super FREQ

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?

pavank
Obsidian | Level 7

I want which variable has max length with that variable name

PaigeMiller
Diamond | Level 26

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;
--
Paige Miller
ballardw
Super User

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

pavank
Obsidian | Level 7

@ballardw

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

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:

@ballardw

 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;

 

PaigeMiller
Diamond | Level 26

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

 

--
Paige Miller
Tom
Super User Tom
Super User

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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 8 replies
  • 444 views
  • 2 likes
  • 5 in conversation