> 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.
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.
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
> 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 ;
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.
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 */
do j=1 to 10;
do i=1 to dim(sls);
drop i j;
select COUNT(name) INTO :MAX_SLS_COL from sashelp.Vcolumn where libname='WORK'
and memname='SLS_YRLY' and UPCASE(name) like 'SLS%' ;