DATA Step, Macro, Functions and more

how to get the max number in a column name.

Reply
Super Contributor
Posts: 673

how to get the max number in a column name.

how to get the max number in a column name.
say for instance if there are columns sls1 to sls12, how to get the number 12 from the column name sls12?
Respected Advisor
Posts: 3,799

Re: how to get the max number in a column name.

> how to get the max number in a column name.
> say for instance if there are columns sls1 to sls12,
> how to get the number 12 from the column name sls12?

I think the short answer is use VNAME function. But I assume you want a method that allows you retrieve the dimension of the enurmerated list no matter what it is. To do that you need to give a little more information on what you have and what you know.
Super Contributor
Posts: 673

Re: how to get the max number in a column name.

Posted in reply to data_null__
The reason is to rename the variables.i.e 12 months of sales we will have sls1 to sls12. after 12 months, the 13th month we will have new sales data and that will be called as sls1.
and the previous months sales will be named from there on. i.e sls1 has to be renamed sls2, sls2 has to be renamed as sls3 so on.
Valued Guide
Posts: 2,177

Re: how to get the max number in a column name.

I think you would find it easier to manage the data with the month as a date column (i.e. transposed to "narrow").
Let the reporting procedures (like REPORT and TABULATE) achieve the transpose of 12 rows into 12 columns. (easy as class or accross variables). Then your filtering is simply for sales within whatever date range is needed.
I would even encourage you to transpose any preliminary data delivered in a "wide" form, back to "narrow" to better support trend analysis.
Holding a "monthly" categorical variable as a SAS date will ensure you can keep proper chronological order ( imagine arranging MonYY month strings ) with any month-type format. Then, whether your months history is 1-12 or 2-13, is no longer important.
just my $0.02
peterC
Valued Guide
Posts: 2,177

Re: how to get the max number in a column name.

> The reason is to rename the variables.i.e 12 months
> of sales we will have sls1 to sls12. after 12 months,
> the 13th month we will have new sales data and that
> will be called as sls1.
> and the previous months sales will be named from
> there on. i.e sls1 has to be renamed sls2, sls2 has
> to be renamed as sls3 so on.

are you seeking a renaming shortcut?
from a table holding columns sls1 - sls&n which are to be renamed as sls2 - sls%eval( &n +1 )
???
(if you have SAS9.2 rename in ranges works) try[pre]
libname your (work) ;[/pre]*first create data set with columns sls1-sls12;[pre]data your.original ;
retain sls1-sls12 123 ;
run;
proc contents data= your.original noprint out= _data_ ; run; [/pre]* getting column names;[pre]proc sql noprint ;
select max( input(substr( name, 4, 3), 3.)) into :N separated by ' '[/pre]without that input function, I got the maximum of the string which was 9, but we want 12[pre]from &syslast where lowcase(substr(name,1,3)) = "sls"
;
quit ;[/pre]* finally build new table renaming columns;[pre]data new_names ;
set your.original( rename= ( sls1-sls&N = _sls1-_sls&N ) );
rename _sls1-_sls&N = sls2-sls%eval( &N +1 ) ;
run;[/pre] needed the two stage rename at the end because it seems to validate each rename within the statement.
Contributor
Posts: 29

Re: how to get the max number in a column name.

please check the vcolumn view from sashelp. This is a system created view which has an entry for every column, i mean each row represents a column in a table for all the active library in that session.

Lets say your table is sales_yrly

/*Creating SALES data */
data SLS_YRLY;
array sls{12};
do j=1 to 10;
do i=1 to dim(sls);
sls{i}= floor(ranuni(0)*60);
end;
output;
end;
drop i j;
run;

proc sql;
select COUNT(name) INTO :MAX_SLS_COL from sashelp.Vcolumn where libname='WORK'
and memname='SLS_YRLY' and UPCASE(name) like 'SLS%' ;
quit;

%PUT &MAX_SLS_COL.;

Hope this helps!
Super User
Posts: 10,020

Re: how to get the max number in a column name.

Do these variables have the same type of data?
If they are, You can use array to get it. It suited for var1-varN.
Some dummy code like this:

[pre]
array sls{*} sls:;
max_number=dim(sls);

[/pre]

Ksharp Message was edited by: Ksharp
Ask a Question
Discussion stats
  • 6 replies
  • 223 views
  • 0 likes
  • 5 in conversation